EDIT2 . This is fixed by replacing answer = getchar(); to lines 38 and 56 on scanf("%d", &answer); , and he corrected himself, but I do not 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 if you want to compile and see what: http://pastebin.com/raw.php?i=83y0SLHQ
ok, so everything below works fine, now a new problem arises:
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 will prompt me to enter Y or N again, even if it is valid? He should not do that. All other loops work except the first with (Y / N).
I did some debugging and I printed the rc value after the first line and its ASCII value is 10 , so that means my answer value is actually reading in \n and only after the second request, it reads the correct ASCII y value and makes a comparison.
Why does it take \n first?
OLD POST
I have a program in which I have to check the user input. If user inputs say "y", "Y", "n" or "N", they pass the test. If they enter anything but this, the program displays:
please enter Y or N only .
I made the verify function for this. Values ββare passed to him as follows:
If I want it to only check for YNyn characters, I would do:
verify("YNyn", 2);
YNyn are the characters I want to check, 2 is the number of characters in uppercase.
In my check function, I see if my second parameter is equal to 2 or 3. (it would be 3 if I had verify("SMLsml", 3); SML, small average size).
I added to printf to find out what the ASCII values ββof the first parameter in my validation function were, and they all corresponded correctly.
OUTPUT : this means that Y is 89, N is 78, y is 121, n is 110. The value after the value is I entered (Y). -1 is the value of the rc variable.
Do you order Fish? (Y/N): Y Y:89 N:78 y:121 n:110 - answer:89 rc:-1
PROBLEM : if statements are not checked, therefore 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 tried reformatting the if statement to one line, it didn't work.
This means that if the value entered matches the ASCII value for Y, N, y or n, then the variable rc must be set to 1. Unfortunately, this is not the case, the problem is that if the statement.
As you can see above, in 89 78 121 110 - 89 -1 , the last value ( rc variable), the value is -1. This means that my if statements are omitted. Why do they go down and don't set rc to -1?
FUNCTION CHECK
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); } }