I am student, taking C programming now.
I have assignment to write function to check if inputed data is numbers. if not - it should write that, and ask to enter number again.
I wrote code, to write error message, but dont know how to make program to work on next input.. should I call the Main function again ?
double hours; //hours worked in a week
double payrate; // hourly salary
double grosspay; //total weekly payment
int k; //number of values read
printf ("Enter number of hours, and hourly pay (or press 0 to exit) \n");
while ((k = scanf ("%lf%lf", &hours, &payrate))==2)
if (isdigit(hours) !=0)
printf ("You entered invalid data, please enter number of hours \n");
if (isdigit(payrate) !=0)
printf ("You entered invalid data, please enter payrate \n");
You cannot use a double parameter as input to the isdigit()[^] function. You should read your numbers in as strings and then apply isdigit() to each character, using a loop. Rather than having all your code in the main() function, you may want to think about writing a helper function that will input the string, validate it and return the converted value.
One of these days I'm going to think of a really clever signature.
int main( )
char n ;
int no = 0 ;
printf("Enter a no " );
while( (n = (char)getche( )) != '\r') //Check for enter key
if( n >= '0' && n <= '9' )
n = n - 48 ;
no = (no * 10) + n ;
printf("\n %d " , no ) ;
return 0 ;
Last Visit: 31-Dec-99 19:00 Last Update: 20-Feb-18 8:41