|
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 ?
int main()
{
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)
return hours;
else
printf ("You entered invalid data, please enter number of hours \n");
if (isdigit(payrate) !=0)
return payrate;
else
printf ("You entered invalid data, please enter payrate \n");
if (hours==0 || payrate==0)
{
printf ("Good Bye. \n\n");
system("PAUSE");
return 0;
}
double grosspay;
grosspay = calculateGrossPay(hours, payrate);
printf ("Hours worked: %.2f\n", hours);
printf ("Hourly Rate: %.2f\n\n", payrate);
printf ("Your weekly payment is: $ %.2f \n\n\n", grosspay);
|
|
|
|
|
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.
|
|
|
|
|
i dont really know how to do that yet... Ok, i will try..
is there a general format of that function somewhere?
|
|
|
|
|
Mikerush7 wrote: is there a general format of that function somewhere? Assuming you mean isdigit() , I put the link in my previous message.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Mikerush7 wrote: while ((k = scanf ("%lf%lf", &hours, &payrate))==2)
If scanf succeeds then you need no other check.
Mikerush7 wrote: should I call the Main function again
No. You may call scanf (if you are allowed to use it) many times inside your main function.
Veni, vidi, vici.
|
|
|
|
|
Mikerush7 wrote: while ((k = scanf ("%lf%lf", &hours, &payrate))==2) Without a full understanding of assignments and expressions, mixing the two in one statement is highly discouraged.
Mikerush7 wrote: return hours; While I'm not sure who you'd be returning hours worked to or why, the compiler should have at least warned you about loss of data.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
DavidCrow wrote: the compiler should have at least warned you Given the number of syntax errors elsewhere I don't think a compiler has ever seen this sample.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
thank you everybody for help. I will keep working on it. my teacher is not helping, so I am on my own..
|
|
|
|
|
Build your logic based on the following example.
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 ;
}
else{
printf("\nInvalid. ");
}
}
printf("\n %d " , no ) ;
return 0 ;
}
|
|
|
|