0

This program asks the user to enter a char and it navigates him to enter two numbers and perform a certain task, the problem is after the switch process the program asks the user to enter another char but it doubles shows twice in the output, it seems that after I tap the key to add a char and make it go into the switch it does the assignment well but after it it ask of me two times in a row to add another character.

image

void main()
{
    char ch;
    double sum , num1, num2, maxNumber, minNumber;
    printf("Please enter a character: ");
    ch = getchar();

    while ((ch != 'q') || (ch != 'Q'))
    {
        switch (ch)
        {
            case 'a': case'A': // Average number

                printf("Please enter two numbers: ");
                scanf("%lf%lf", &num1, &num2);
                sum = (num1 + num2) / 2;
                printf("The average number between %.1lf and %.1lf is %.1lf\n", num1, num2, sum);
                if (sum - (int)sum == 0)
                    printf("The number is an integer\n");
                else
                    printf("The number is not an integer\n");
                break;

            case'*': // Multiply number

                printf("Please enter two numbers: ");
                scanf("%lf%lf", &num1, &num2);
                sum = num1 * num2;
                printf("The multiplied number between %.1lf and %.1lf is %.1lf\n", num1, num2, sum);
                break;

            case'm':// Show minimum number

                printf("Please enter two numbers: ");
                scanf("%lf%lf", &num1, &num2);
                num1 > num2 ? (minNumber = num2) : (minNumber = num1);
                printf("The minimum number is %.1lf\n", minNumber);
                break;

            case'M':// Show maximum number

                printf("Please enter two numbers: ");
                scanf("%lf%lf", &num1, &num2);
                num1 > num2 ? (maxNumber = num1) : (maxNumber = num2);
                printf("The minimum number is %.1lf\n", maxNumber);
                break;

            case'^':// Perform power between the numbers

                printf("Please enter two numbers: ");
                scanf("%lf%lf", &num1, &num2);
                sum = pow(num1, num2);
                printf("The powered number is %.1lf\n", sum);

                break;

            case'q': case'Q':
                printf("The program has ended");
                break;

                default:
                break;
        }

        printf("Please enter a character: ");
        scanf("%c", &ch);
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Tommy Mei
  • 13
  • 2
  • 5
    Unrelated: `(ch != 'q') || (ch != 'Q')` will *always* be true. Even if it's one, it, by definition, it cannot be the other. And if its' neither, then both expressions are true. That should be `&&`. – WhozCraig Nov 28 '19 at 21:19
  • I believe this has your answer. [Program doesn't wait for input](https://stackoverflow.com/questions/8464620/program-doesnt-wait-for-user-input-with-scanfc-yn) – Kai Nov 28 '19 at 21:20
  • Quoted from that answer: "When you enter the second string and hit the ENTER key, a string and a character are placed in the input buffer, they are namely: the entered string and the newline character.The string gets consumed by the scanf but the newline remains in the input buffer." – Kai Nov 28 '19 at 21:22

0 Answers0