Tuesday, July 14, 2009

Help with my C program. How do I ask the user whether they want to start again?

What line of code would I use to ask the user if they wanted to start over from the beginning? I want to put this line of code at the end of the program.





#include %26lt;stdio.h%26gt;





int main()


{


float e, d, a, b, c;


d=40;


e=6.25;


printf("Wage Calculater\n");


Start:


printf("\nEnter the amount of hours worked: ");


scanf("%f",%26amp;a);


if (a%26gt;d){


printf("\n\n ***INVALID***\n\n");


printf("Only 40 hours are possible in a week");


goto Start;


return 1;


}


Mid:


printf("\nNow enter your hourly rate: ");


scanf("%f",%26amp;b);


if (b%26lt;e){


printf("\n\n ***INVALID***\n\n");


printf("Entered value was less then minimum wage\n");


goto Mid;


return 1;


}


c=a*b;


printf("\nYour salary for this week is:\n");


printf(" \n ***%f***\n",c);


system ("pause");


return 0;


}

Help with my C program. How do I ask the user whether they want to start again?
do a looping statement..





do{


/* you should place your codes here that you want to be repeated*/


do{


printf("do you want to try again? Y/N");


ans = getch();


}while(toupper(ans) != 'Y' %26amp;%26amp; toupper(ans) != 'N');


}while (toupper(ans)=='Y');





//make a variable ans
Reply:C has a powerful feature called recursion. (You may know that).





In the end of the program add


printf("Continue?(Y/N) :");


fflush(stdin); /* this is required. try without this */


opt = getch(); /* you must declare char opt */


if toupper(opt) = 'Y' /* #include %26lt;ctype.h%26gt; */


main(); /* this is recursion. main() is called inside main() */





Why do you use goto statements? They cause the system to abruptly jump. Use do while instead. (This loop executes at least once. If the answer matches the criteria, control is terminated out of the loop. Otherwise it loops again)

bouquet

No comments:

Post a Comment