bbmewcat
bbmewcat

Reputation: 13

The menu of a program appeared twice

I have written the following code that required the user to choose from a menu.

#include <stdio.h>
char start(void);
char first(void);
float firstn(void);
int main(void)
{
    int choice;
    while((choice=start())!='q')
    {
        switch(choice)
        {
            case'a':
                firstn();
                printf("yeah\n");
                break;
            case's':
                firstn();
                printf("yeah\n");
                break;
            case'm':
                firstn();
                printf("yeah\n");
                break;
            case'd':
                firstn();
                printf("yeah\n");
                break;
        }
    }
    printf("Bye.");
    return 0;
}
char start(void)
{
    int ch;
    printf("Enter the operation of your choice:\n");
    printf("a. add           s. subtract\n");
    printf("m. multiply      d. divide\n");
    printf("q. quit\n");
    ch=first();
    while(ch!='a' and ch!='s' and ch!='m' and ch!='d' and ch!='q' and ch!='\n')
    {
        printf("Please respond with a, s, m, d or q.\n");
        ch=first();
    }
    return ch;
}
char first(void)
{
    int c;
    c=getchar();
    while (getchar()!='\n')
        continue;
    return c;
}
float firstn(void)
{
    float first;
    char ch;
    printf("Enter first number:");
    while(scanf("%f",&first)!=1)
    {
        while((ch=getchar())!='\n')putchar(ch);
            printf(" is not an number.");
        printf("Please enter a number, such as 2.5, -1.78E8, or 3:");
    }
    return first;
}

However, when the program is run, the menu will display two times like this:

Enter the operation of your choice:
a. add           s. subtract
m. multiply      d. divide
q. quit
a
Enter first number:3
yeah
Enter the operation of your choice:
a. add           s. subtract
m. multiply      d. divide
q. quit
a
Enter the operation of your choice:
a. add           s. subtract
m. multiply      d. divide
q. quit
a
Enter first number:3
yeah
Enter the operation of your choice:
a. add           s. subtract
m. multiply      d. divide
q. quit

As shown above, the program works correctly in the first loop. It receives the character from the user, then ask for a number. However, in the second loop, the program has gone wrong. The menu has appeared, but no matter what the user entered, the menu will not receive the character and the menu appeared again. Only after the menu appeared two times, the menu receive the character. How can I solve this problem?

Upvotes: 1

Views: 214

Answers (1)

Graeme Cole
Graeme Cole

Reputation: 276

Your firstn() function consumes the number, but not the newline on the end. This means the newline on the end of "3" gets consumed by the next call to first(). It doesn't recognise that as one of the options so it asks you the question again.

Try adding:

while (getchar() != '\n');

to the end of your firstn() function just before the return statement.

Upvotes: 1

Related Questions