Link to home
Start Free TrialLog in
Avatar of reesee324
reesee324

asked on

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

Avatar of phoffric
phoffric

  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
SOLUTION
Avatar of Superdave
Superdave
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of reesee324

ASKER

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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial