简体   繁体   中英

do while loop is not looping

I have this program that doesnt want to loop after pressing any char that is not N or n.

The code in C:

#include <stdio.h>

int main()
{

    float num1, num2;
    char op;
    char choice;

    do {
        printf("Enter num1: ");
        scanf("%f", &num1);

        getchar();

        printf("Enter Operator (+, -, *, /): ");
        scanf("%c", &op);

        printf("Enter num2: ");
        scanf("%f", &num2);

        if(op == '+'){
            printf("%f", num1 + num2);
        } else if(op == '-'){
        printf("%f", num1 - num2);
        } else if(op == '/'){
        printf("%f", num1 / num2);
        } else if(op == '*'){
        printf("%f", num1 * num2);
        } else {
        printf("Invalid Operator");
        }

        printf("\nDo you want to enter another pair of numbers? (Y/N): ");
        scanf(" %c", &choice);

        if (choice == 'n')
            {
                choice = 'N';
            }

    } while (choice != 'N');

    return 0;
}

The result: The program runs the do part of the loop but then after the last question it always says something like this: "Process returned -1 (0xFFFFFFFF) execution time: 3.296 s" and it doesn't loop anymore.

Can you help me to loop the program for more caluclations?

I have this program that doesnt want to loop after pressing any char that is not N or n.

The code in C:

#include <stdio.h>

int main()
{

    float num1, num2;
    char op;
    char choice;

    do {
        printf("Enter num1: ");
        scanf("%f", &num1);

        getchar();

        printf("Enter Operator (+, -, *, /): ");
        scanf("%c", &op);

        printf("Enter num2: ");
        scanf("%f", &num2);

        if(op == '+'){
            printf("%f", num1 + num2);
        } else if(op == '-'){
        printf("%f", num1 - num2);
        } else if(op == '/'){
        printf("%f", num1 / num2);
        } else if(op == '*'){
        printf("%f", num1 * num2);
        } else {
        printf("Invalid Operator");
        }

        printf("\nDo you want to enter another pair of numbers? (Y/N): ");
        scanf(" %c", &choice);

        if (choice == 'n')
            {
                choice = 'N';
            }

    } while (choice != 'N');

    return 0;
}

The result: The program runs the do part of the loop but then after the last question it always says something like this: "Process returned -1 (0xFFFFFFFF) execution time: 3.296 s" and it doesn't loop anymore.

Can you help me to loop the program for more caluclations?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM