Hi All
I am new to c programming and trying to figure out why I cannot break out of a loop. I have used an if statement and want to use the continue and break. Now I know if I just use the break it will terminate and if I just use the continue yes it continues. My question is should I just be using one or the other(continue or break) but then how to I continue ot break? or is it better for me to rename the while. I have tried a switch statement and that doesn't work what ever statement I put first it does this should be telling me something but i cant think why. I'm just confused now, was just thinking about using a for loop? any pointers would be great many thanks for reading and you time.
Code:#include <stdio.h> #include <stdlib.h> int one = 1, two = 2, three = 3, i = 3, number; //Declare and Initialise Global Variables float grossPay, pay, netPay; const float TAX = 4.800; //Use constant as these values will never change const float BONUS = 0.25; // Bonus payment to add to grosspay when tax is deducted char answer; int main (void) //Main Function { printf("\n\t\tANNUAL GROSS PAY MAIN MENU\n"); //prepare menu list for user input printf("\t\t1. Display Conversion Table Goss Pay\n"); printf("\t\t2. Process Pay Value\n"); printf("\t\t3. Exit\n\n"); printf("\n\t\tEnter Option"); scanf ("%d",&number); while (number > i) // while loop to ensure user enters the correct number, if not throw invalid option { i--; printf ("\n\t\tINVALID OPTION"); printf("\n\t\tEnter Option " ); scanf ("%1s", number); } if (one == number) { printf("\n\t\t CONVERSION TABLE\n\n"); } // list screens, use if statement to switch between screens else if (two == number) { printf("\n\t\t COMPANY PAYROLL RECORDS\n\n"); } else if (three == number) { printf("\n\t\tEND OF PROGRAM\n\n"); } //Calculate GrossPay while (number == one) { printf("\n\t\t GROSS_PAY"); printf("\n\t\t Enter Gross Pay " ); scanf("%f", &grossPay); pay = (grossPay - TAX); //Calculate gross pay leaving remainder netPay = (pay * BONUS) + pay; // use remainder * bonus and add pay printf("\n\t\t NET_PAY"); printf("\n\t\tYour Net Pay %.3f\n", netPay); printf ("\n Would you like to continue [Y/N] "); scanf("%c", &answer); getchar(); if (answer == 'Y'|| 'y'){ continue; } else if (answer == 'N'||'n'){ break; } } //End of While return 0; } // End of Program



LinkBack URL
About LinkBacks



