-
Continue and switch
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
-
I don't see a good reason for i-- inside your first while loop.
In this case, you appear to want your while loop (in the number==one case) to really be controlled by answer.
As to switch, switch goes like:
Code:
switch(number) {
case one: //case one goes here
break;
case two: //case two goes here
break;
//and so on
}
-
Make sure you have the right value in answer. Did you correctly read it? Your use of continue and break are correct, so try looking a little bit higher up. Try looking at the if, and think of what may happen for it to always be false - or always true!
In addition to what tabstop said about switch, I'd like to make a point that the break statement after each case is essential, or else the program will evaluate every statement for every case after the one it starts at. This will do this until the end or until it hits a break.
Man, I wish tax was just a constant.
-
Code:
if (answer == 'Y'|| 'y'){
continue;
}
else if (answer == 'N'||'n'){
break;
}
This won't do what you think it does.
'n' and 'y' are always true, so the resulting ifs will always be true.
You need to do answer == 'X' || answer == 'Y'.
And your indentation is horribly messed up. What IDE / editor do you use?
-
Another alternative to what Elysia suggested is doing something like
Code:
if(toupper(answer) == 'Y')
But I won't start suggesting too much until you learn to post code that is user readable. To be honest, I like to have my IDE print '\t' characters. However, I always convert to spaces when I post code from my projects. One of these days when I am really bored I will write a firefox plug-in that reparses copied and pasted source code.
-
Actually, I wasn't suggesting a solution, but merely pointing out why it doesn't work as expected and how to fix that little problem.
It was related to how you would use OR with an if with multiple conditions.
-
I suppose... "Dude your code is f**ked" is not necessarily a suggestion to some, but if someone told me that, I would take it as constructive criticism. But I am optimistic like that.
-
Thank you so much, all of you I was'nt expecting to see so many replies, have been busy so taking on board what you have said im now going back to my program and look at the suggestions given. Apologise about my code I am using Xcode again this is a new IDE for me so learning this aswell as C I can't find at the moment where to format the text? many thanks again you've been very helpful
-
It is possible to format the text yourself. One tab for every {.
But there are perfectly good other IDEs out there, too.
Visual Studio is the best one I know to format code for you.
-
Many thanks
Thanks again Elysia, I will download this and use what you have suggested until then. I use Xcode as I am following a book, it recommended using this IDE plus I have a mac book. I have read the information you mentioned this was very interesting and will continue to use this throughout future exercises.
-
Well, Visual Studio is for Windows only.
I don't know of IDEs for Mac, but perhaps this may help: http://cpwiki.sourceforge.net/IDE
Remember indentation "rules" will also work, regardless of IDE.