• C

Passing in as a string and then testing to see if it is an integer

I'm trying to pass in a variable as a string and then test it to see if it is an integer, if it is then I want to store it into an array of structures. I am a little lost as to how to set all this up.
#include <ctype.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <stdio.h>

struct customers
{
	int cust_id;
	char cust_name[20];
	static char states [50][3];
	char discount;
	double balance_due;
	int order_out;
};
struct myRecords
{
	customers rec[20];
};

using namespace std;

int is_int(char prompt[50]);

int main()
{
	int cust_id;
	myRecords table, *point_table;
	point_table = &table;

	cust_id = is_int("Please enter a customer id: ");

}//end of main

/**************************************
*Function to test if it is an integer *
**************************************/
int is_int(char prompt[50], struct myRecords* table)
{
	int x = 0;
	int y = 0;
	int i;
	x < strlen(prompt);
	if(!isdigit(prompt[x]))
		return 0;
	else
		gets(prompt);
		i = atoi(prompt);
		i = table->rec[y].cust_id;
		return 1;
}

Open in new window

reesee324Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffric\Commented:
  x < strlen(prompt);
    -- this is an expression; no value is changed
 -- did you mean:
   x =strlen(prompt);

Here is a discussion on testing whether a string is a number:
    http://forums.devarticles.com/c-c-help-52/checking-if-string-is-a-number-or-not-57342.html
0
SuperdaveCommented:
What I think you actually meant by that line is something like:
for ( x=0; x < strlen(x); x++) {

Note that you need to increment x or you would have an infinite loop.
But that's not very efficient because it will calculate strlen each time through the loop.  So the better way to do it is:

for ( x=0; prompt[x]; x++) {

Also the four lines after else should be in curly braces, normally, if you're expecting it to group them the way the indentation indicates.  (C is so non-Pythonic it won't even do that for you automatically.)  In this particular case it happens to not matter; you could leave out the "else" entirely.  
0
reesee324Author Commented:
This is what I have now but I keep getting an external error. My other problem is I don't understand how to store that variable into cust_id in the structure if it is a digit. I also don't know if I'm calling it right in main and I need to pass in the whole records table and I think that is causing the error but I don't know. Any help would be appreciated.

I also understand that the is digit returns 1 for true and 0 for false but what to do with it after that I guess? I'm trying to explain this as best as I can! Thanks for the help so far!
#include <ctype.h>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <stdio.h>

struct customers
{
	int cust_id;
	char cust_name[20];
	static char states [50][3];
	char discount;
	double balance_due;
	int order_out;
};
struct myRecords
{
	customers rec[20];
};

using namespace std;

int is_int(char prompt[50]);

int main()
{
	int cust_id;
	myRecords table, *point_table;
	point_table = &table;

	cust_id = is_int("Please enter a customer id: ");

}//end of main

/**************************************
*Function to test if it is an integer *
**************************************/
int is_int(char prompt[50], struct myRecords* table)
{
	int y = 0;
	int i;
	int len = strlen(prompt);
	for (i=0; prompt[i]; i++)
	{
		if (!isdigit(prompt[i]))
			return 0;
		else
			return 1;
	}
}

Open in new window

0
SuperdaveCommented:
You could use the atoi function to convert your string to an integer.

Also, delete lines 47 and 48.  If you always return out of the for loop it won't loop.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.