Reputation: 2833
EDIT2: Fixed it myself by replacing answer = getchar();
on line 38 and 56 with scanf("%d", &answer);
and it fixed itself, but I don't know why.
Fixed version:
Do you order Fish? (Y/N): g
Please enter Y or N only: g
Please enter Y or N only: g
Please enter Y or N only: g
Please enter Y or N only: y
Fish choice (K- Haddock, T- Halibut): x
Please enter K or T only: x
Please enter K or T only: x
Please enter K or T only: x
Please enter K or T only: k
What size (L - Large, M - Medium, S - Small): x
Please enter S, M, or L only: x
Please enter S, M, or L only: x
Please enter S, M, or L only: x
Please enter S, M, or L only: l
How many orders do you want? (>=0): 2
EDIT:
full source in case you want to compile and see what's up: http://pastebin.com/raw.php?i=83y0SLHQ
okay so everything below actually works, that's fine, now there is a new problem:
OUTPUT:
Do you order Fish? (Y/N): y
Please enter Y or N only: n
Fish choice (K- Haddock, T- Halibut): s
Please enter K or T only: K
What size (L - Large, M - Medium, S - Small): x
Please enter S, M, or L only: L
How many orders do you want? (>=0): 2
PROBLEM: notice how if I enter y
as an option, it tells me to enter Y or N again even though it is valid? It shouldn't do that. All other loops work except the first one with the (Y/N).
I did some debugging, and I printed the value of rc
after the first line and its ASCII value is 10
, so that means my answer
value is actually reading in \n
and then only after a second prompt, it reads in the correct ASCII value of y
and makes the comparison.
Why is it taking in \n
first?
I have a program where I have to verify the users input. If the user inputs say "y", "Y", "n", or "N", they pass the validation. If they enter anything but that, the program outputs:
please enter Y or N only
.
I made a verify
function for this. Values are passed to it like so:
If I want it to only verify characters YNyn, I would do:
verify("YNyn", 2);
YNyn are the characters I want to verify, 2 is the number of uppercase characters.
In my verify function, I see if my second parameter is equal to 2 or 3. (it would be 3 if I had verify("SMLsml", 3);
the SML being small medium large).
I added in printf
to see what the ASCII values of the first parameter in my verify function were, and they all corresponded correctly.
OUTPUT: this means that Y is 89, N is 78, y is 121, n is 110. Value after the - is value I entered (the Y). -1 is the value of variable rc
.
Do you order Fish? (Y/N): Y
Y:89 N:78 y:121 n:110 - answer:89 rc:-1
PROBLEM: if statements dont validate so rc is not set to 1.
if ((answer == validAnswers[0])
|| (answer == validAnswers[1])
|| (answer == validAnswers[2])
|| (answer == validAnswers[3]))
{ rc = 1; }
I also tried:
if (answer == validAnswers[0]
|| answer == validAnswers[1]
|| answer == validAnswers[2]
|| answer == validAnswers[3]) {rc = -1 }
but to no avail, I've tried reformatting the if statement into one line as well, didn't work.
This means that if the value I entered corresponds to the ASCII value of Y, N, y, or n, then variable rc
should be set to 1. Unfortunately it does not do this, the problem lies within the if statement.
As you can see above, in 89 78 121 110 - 89 -1
, the last value (variable rc
), the value is -1. This means that my if statements are omitted. Why are they omitted and not setting rc to -1?
VERIFY FUNCTION
int verifyLoop(char *validAnswers, int numChars) {
int answer, rc = -1;
if (numChars == 2) {
answer = getchar();
do {printf("\n%d %d %d %d - %d %d\n", validAnswers[0],validAnswers[1],validAnswers[2],validAnswers[3], answer, rc);
answer = getchar();
if ((answer == validAnswers[0])
|| (answer == validAnswers[1])
|| (answer == validAnswers[2])
|| (answer == validAnswers[3]))
{ rc = 1; }
if (rc == -1 && answer != -1)
{
printf("Please enter %c or %c only: ", validAnswers[0], validAnswers[1]);
while (answer != -1 && answer != '\n')
answer = getchar();
}
} while (rc == -1 && answer != -1);
}
else if (numChars == 3) {
answer = getchar();
do {printf("\n%d %d %d %d %d %d - %d\n", validAnswers[0],validAnswers[1],validAnswers[2],validAnswers[3],validAnswers[4],validAnswers[5], answer);
answer = getchar();
if (answer == validAnswers[0]
|| answer == validAnswers[1]
|| answer == validAnswers[2]
|| answer == validAnswers[3]
|| answer == validAnswers[4]
|| answer == validAnswers[5])
{ rc = 1; }
if (rc == -1 && answer != -1)
{
printf("Please enter %c, %c, or %c only: ", validAnswers[0], validAnswers[1], validAnswers[2]);
while (answer != -1 && answer != '\n')
answer = getchar();
}
} while (rc == -1 && answer != -1);
}
}
Upvotes: 4
Views: 160
Reputation: 25740
The reason that it reports -1 and continues with no errors is because it is actually working. :)
However, it reports -1 because your printf is at the top of the loop, and prints the value before the if statement and the setting of rc to 1.
Move your printf
to just above the end of the loop to see the correct values.
Upvotes: 3
Reputation: 393
There is a getchar after the print, then you are not comparing with the value of the first getchar, but from the second one.
Upvotes: -1