Possible Duplicate:
If statement in my loops are being skipped in C
Having a weird problem. I have a program that I made where you can order drinks, chips, or fish. When you order anything, the second line is “what size do you want: (s small, m medium, l large)”. Now, you can only enter s, S, m, M, l, or L. Anything beyond that, it enters the loop and tells you to enter only one of the above 6 characters.
In my verify function, I have 3 cases, one being if you have the following as the first parameter in verify: foodSize, foodSelect, or order.
Once in the verify function, and if the choice is foodSize, it goes into this loop:
if (choice == "foodSize")
{
do {
answer = getchar();
if (answer == 'S' || answer == 's')
{ rc = 0; }
else if (answer == 'M' || answer == 'm')
{ rc = 1; }
else if (answer == 'L' || answer == 'l')
{ rc = 2; }
if (rc == -1 && answer != -1)
{
printf("Please enter S or M only: ");
while (answer != -1 && answer != '\n')
answer = getchar();
}
} while (rc == -1 && answer != -1);
}
I have the EXACT SAME CODE in foodSize as I do in the other two if’s, just switching around the printf inside the 3rd if statement within the do while to Please enter X or X only: which doesn’t affect anything. All the other loops work, regardless if I put them in different orders, or put it into if, else if, and else, or if, else if, or else if.
THE LOOP THAT HAS THE SAME CODE AND WORKS FINE (meaning it asks me for my input first, doesn’t give me error message first):
if (foodChoice == "Drinks")
{
do {
answer = getchar();
if (answer == 'S' || answer == 's')
{ rc = 1; }
else if (answer == 'C' || answer == 'c')
{ rc = 2; }
else if (answer == 'T' || answer == 'T')
{ rc = 3; }
if (rc == -1 && answer != -1)
{
printf("Please enter S, C, or T only: ");
while (answer != -1 && answer != '\n')
answer = getchar();
}
} while (rc == -1 && answer != -1);
}
Nothing works, nothing for foodSize work. Not even when I put verify("foodSize", foodChoice) FIRST in my question function:
part of my question function:
printf("\nDo you order %s? (Y/N): ", foodChoice);
verify("order", foodChoice);
printf("%s choice %s: ", foodChoice, foodOptions);
verify("foodSelect", foodChoice);
printf("What size (L - Large, M - Medium, S - Small): ");
verify("foodSize", foodChoice);
If I put verify("foodSize", foodChoice) right after the first printf it doesn’t change anything either.
the output, no matter what, always skips my get char, gives me the error message first, then prompts me for input within my foodSize loop in my verify function REGARDLESS where the loop is placed, or what order it is in.
OUTPUT:
What size (L - Large, M - Medium, S - Small): Please enter S or M only:
when instead it should be:
What size (L - Large, M - Medium, S - Small):
I should only be prompted if I don’t enter a lower case or upper case L, M, or S. Yet it prompts me regardless. I’m at wits and haven’t been able to solve this yet. I know my code looks like shite, I know some of the ways I do things in it aren’t the current standard way, but that’s the way we have to do it in school, so please don’t point out those errors. I just want to know why this loop isn’t working and I want to fix it, that’s all.
FULL SOURCE in case somebody wants to compile it and give it a run for themselves.
http://pastebin.com/raw.php?i=hjPDu4QB
Eternal great-fullness to whoever discovers why this does not work.
If the line
is working at all, then you are depending on compiler optimizations. What this is doing is comparing where in memory those two strings are located; if it is true, this is only because the compiler optimized them into two pointers to the same string. The correct way to compare strings is with
strcmp:This will actually compare the contents of the strings.