You are viewing this site as a guest. You can still ask questions and help others! Join our Dev2Dev IT Community to receive your own blog, share your knowledge, and much more.

Checking against character entry

2 replies [Last post]
dazzer33's picture
User offline. Last seen 11 years 30 weeks ago. Offline
Joined: 12/05/2008
Posts: 1

Hi, all.

I hope you can help. Presently I have an assignment to produce a programme in 'C' that will calculate either the mode, mean, median or range of 5 entered numbers. I have the program up and working and at present the 5 numbers are entered and stored in an array before selecting which calculation to carry out.
My problem is safeguarding against a character entry. Presently I have

for (x=0;x<=4;x++)
{
printf("Enter number %d: ",x+1); //Acquire input and store input and store in array
scanf("%s",&check); //scan in as string
num[x]=atoi(check); //resets a character to 0, prevents an invalid loop
}
This will scan in the entries and if a character is entered will set it to 0. I know there must be away to check that the entry is a digit or is not a character but my attempts to use ISALPHA ISDIGIT have come to nothing. My initial thoughts were to use something along the lines of

if (isalpha(check))
{
printf("\n you have entered a character, try again");
x--;
system("PAUSE");
}

and include this after the atoi check has been scanned into the array. needless to say I have not got it to work and am unsure if I am heading in the right direction, after many hours with my head in the books it is time to ask for help.

Regards.

}

Nitin's picture
User offline. Last seen 5 years 9 weeks ago. Offline
Joined: 06/30/2008
Posts: 73
Re: Checking against character entry

Your question is about C in PeopleSoft community but its OK.

I have long back worked on C (in my college days). So I have a faint memory of Scanf().
But if i m remembering correctly you can put the acceptable value in Scanf().
e.g. Scanf("123blahblah",&para);

Thanks
Nitin Bhatia

Nitin's picture
User offline. Last seen 5 years 9 weeks ago. Offline
Joined: 06/30/2008
Posts: 73
Re: Checking against character entry

U can go thru this example also to resolve your problem
http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_055.htm

Thanks
Nitin