eveo
eveo

Reputation: 2833

If statement in my loops are being skipped in C

In function verify, I have a loop called "size" which is identical to the 3rd loop in "foodSelect" except that it works differently for some reason. It doesn't prompt me for input first, it goes straight to the if inside of it and asks What size (L - Large, M - Medium, S - Small): Please enter S, M, or L only:. It displays the error first, then prompts me again for input. It's weird.

Source (because it's long): http://pastebin.com/raw.php?i=KxrMAXaU

or source here:

#include <stdio.h>
#include <string.h>

void menu();
void question (char choice[]);
void output(char *foodChoice, char *foodSelect, char *foodSize, int *foodOrderNum, float *foodSubtotal);
int verify(char *choice, char *foodChoice);

void menu() {
/*
printf("\n");
*/
    printf("\nWelcome to Sunny FISH & CHIPS!\n\n");
    printf("########     Fish :     Haddock(K) Large(L) | $5.00\n");
    printf("# FOOD #                Halibut(T) Large(L) | $4.00\n");
    printf("########     Chips:     Cut(C)     Large(L) | $2.00\n");
    printf("                        Ring(R)    Large(L) | $3.00\n");
    printf("                                            | \n");
    printf("##########   Soft Drinks(S)        Large(L) | $2.00\n");
    printf("# DRINKS #   Coffee(C)             Large(L) | $1.75\n");
    printf("##########   Tea(T)                Large(L) | $1.50\n");
    printf("---------------------------------------------\n");
    printf("Note: Medium price: 80%% of large.\n");
    printf("       Small price: 60%% of large.\n");
    printf("TAX is 10%%.\n");
    printf("More than 5 fish, 10%% discount on drink.\n");
    printf("Every 10 fish purchased, get 1 free softdrink.\n");
    printf("  - size of drink is according to size of fish\n");
    printf("----------------------------------------------\n\n");
}

int verify(char *choice, char *foodChoice) 
{
    int answer, rc = -1;
    if (choice == "order") 
    {
        do {
            answer = getchar();
            if (answer == 'Y' || answer == 'y')
            { rc = 1; }
            else if (answer == 'N' || answer == 'n')
            { rc = 0; }
            if (rc == -1 && answer != -1) 
            {
                printf("Please enter y or n only: ");
                while (answer != -1 && answer != '\n')
                answer = getchar();
            } 
        } while (rc == -1 && answer != -1);
    }
    if (choice == "foodSelect") 
    {
        do {
            answer = getchar();

                if (foodChoice == "Fish")
                {
                    do {
                        answer = getchar();
                        if (answer == 'K' || answer == 'k')
                        { rc = 1; }
                        else if (answer == 'T' || answer == 't')
                        { rc = 0; }
                        if (rc == -1 && answer != -1) 
                        {
                            printf("Please enter K or T only: ");
                            while (answer != -1 && answer != '\n')
                            answer = getchar();
                        }
                    } while (rc == -1 && answer != -1);
                }
                if (foodChoice == "Chips")
                {
                    do {
                        answer = getchar();
                        if (answer == 'C' || answer == 'c')
                        { rc = 1; }
                        else if (answer == 'R' || answer == 'r')
                        { rc = 0; }
                        if (rc == -1 && answer != -1) 
                        {
                            printf("Please enter C or R only: ");
                            while (answer != -1 && answer != '\n')
                            answer = getchar();
                        } 
                    } while (rc == -1 && answer != -1);
                }
                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);
                }   
        } while (rc == -1 && answer != -1);
    }
    if (choice == "size") 
    {
        do {
            answer = getchar();
            if (answer == 'S' || answer == 's')
            { rc = 1; }
            else if (answer == 'M' || answer == 'm')
            { rc = 2; }
            else if (answer == 'L' || answer == 'l')
            { rc = 3; }
            if (rc == -1 && answer != -1) 
            {
                printf("Please enter S, M, or L only: ");
                while (answer != -1 && answer != '\n')
                answer = getchar();
            } 
        } while (rc == -1 && answer != -1);
    }
}

void question (char *choice) {

    char *choiceYesNo;
    char *foodOptions;
    char *foodChoice;
    char *foodSelect;
    char *foodSize;
    int *foodOrderNum;
    float *foodSubtotal;

    switch (choice[0]) {
        case 'f':
            foodChoice = "Fish";
            foodOptions = "(K- Haddock, T- Halibut)";
            break;
        case 'c':
            foodChoice = "Chips";
            foodOptions = "(C- Cut, R- Ring)";
            break;
        case 'd':
            foodChoice = "Drinks";
            foodOptions = "(S- Softdrink, C- Coffee, T- Tea)";
            break;
    }

    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("size", foodChoice);
    printf("How many orders do you want? (>=0): ");
        scanf("%d", &foodOrderNum);
    output(foodChoice, foodSelect, foodSize, foodOrderNum, foodSubtotal);
}

void output(char *foodChoice, char *foodSelect, char *foodSize, int *foodOrderNum, float *foodSubtotal) {

    printf("\nYou ordered %s: %c - SIZE: %c   amount ordered: %d, subtotal price: %.2lf\n\n", 
    foodChoice, foodSelect, foodSize, foodOrderNum, foodSubtotal);

}



int main() {

    //menu();

    question("drinks");


}

Upvotes: 1

Views: 1467

Answers (3)

Daniel Fischer
Daniel Fischer

Reputation: 183873

Apart from the fact that you really shouldn't compare the strings with == (although in this case it is unfortunately not unlikely to work because you're only using string literals), your trouble is because the entry of choices needs the return key to be pressed. Thus when you obtain one character for the choice from the input buffer, there is still a newline left in it. Place a

printf("answer = %d\n",answer);

after the first answer = getchar(); in the loop. That will tell you that you have gotten a newline '\n' (10) [or possibly a carriage return '\r' (13), but because of the following, I believe that's not the case] from the input buffer. In the foodSelect loop, you have an answer = getchar(); before the if-statements, and one immediately at the beginning of the do-while loops, so that clears the input buffer. But in the foodSize branch you haven't.

After each choice, clear the input buffer

while((answer = getchar()) != -1 && answer != '\n);

Upvotes: 1

Attila
Attila

Reputation: 28762

You cannot compare strings with ==. As strings are essentially pointers (to an array of characters), you are comparing pointers (the actual addresses in memory where the characters are stored). In order to compare the strings character-by-character, you need to use an appropriate function, the default choice being strcmp.

Also, as Greg Hewgill mentioned, if you use getchar, you will get the skipping behavior described in your question. The reason is that in order for the user to give control back to your code from getchar(), they need to press enter (this is how the console input works). The key enter will be one of the characters read (i.e. the second if the user inputted "y"), so the comparison on the character will fail as well when getchar returns with the code for the enter key.

Upvotes: 2

Greg Hewgill
Greg Hewgill

Reputation: 992737

I don't recommend the use of getchar() for interactive programs. You don't appear to handle the newline characters which are going to come in after everything you type. Use fgets() instead.

To clarify the above, when you press something like YEnter, you have entered two characters into standard input. The first call to getchar(), will return 'Y', and the next call will return '\n' (the newline character). Your code does not expect this following newline character and may appear to "skip" calls to getchar(), when really it is returning more characters you entered.

If you use fgets(), then you get the entire line that the user typed, including the newline, all at once. You (usually) won't have to worry about extra data waiting around in the input buffer.

Upvotes: 4

Related Questions