• 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?
 
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
 
phoffricCommented:
  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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.