Home > Software engineering >  Yes/No loop using while loop in C with a math operation program
Yes/No loop using while loop in C with a math operation program

Time:05-04

First of all, I am a total beginner to both C (and any programming) and Stack Overflow, so sorry if something like this has been asked before.

So I've been having trouble with my math operations code in the loop part. When I type in N or Y, the program acts as if I typed in a different option and says I have to type in Y or N.

Here's the code. Sorry if it's a jumbled mess, this is all I know so far.

#include <stdio.h>
int main() {
    while (1) {
        int choice1, choice2, num1, num2;
        printf("\n  [1] Addition\n  [2] Subtraction\n  [3] Multiplication\n  [4] Division\n");
        printf("\n Pick a choice: ");
        scanf("%d", &choice1);
        printf("\n Give a number: ");
        scanf("%d", &num1);
        printf("\n Give another number: ");
        scanf("%d", &num2);
        switch (choice1) {
            case 1:
            printf("Sum is %d", num1 num2);
            break;
            case 2:
            printf("Difference is %d", num1-num2);
            break;
            case 3:
            printf("Product is %d", num1*num2);
            break;
            case 4:
            printf("Quotient is %d", num1/num2);
            break;
            default:
            printf("Please select a valid operation");
        }
            printf("\n Try another operation [y/n]: ");
            scanf("%d", &choice2);
            if (choice2 == 'y' || choice2 == 'Y') {
                printf("Retrying...");
                break; }
            else if (choice2 == 'n' || choice2 == 'N') {
                printf("Exiting...");
                break; }
            else {
                printf("Pick y or n only");
                break;
            }
    }
    return 0;
}

CodePudding user response:

the issue here is that in the if statement you are using break which breaks the flow, instead you should be using continue

major changes: (I've added comment line to make the changes more obvious for you)

     int check = 1; //added a check variable
     while (check) { //used check variable for while loop
printf("\n Try another operation [y/n]: ");
            scanf("%c", &choice2); //changed %d to %c as the input is a char
            if (choice2 == 'y' || choice2 == 'Y') {
                printf("Retrying...");
                continue; } //changed break with continue
            else if (choice2 == 'n' || choice2 == 'N') {
                printf("Exiting...");
                check= 0; } //updated check so next iteration fails and terminates the loop
            else {
                printf("Error wrong input");
                check= 0; //there cannot be a 2nd try using if else statement
            }

there will be few more changes which I'll mark in the code below

#include <stdio.h>
int main() {
    int check = 1; //added a check variable
    while (check) { //used check variable for while loop
        int choice1, choice2, num1, num2;
        printf("\n  [1] Addition\n  [2] Subtraction\n  [3] Multiplication\n  [4] Division\n");
        printf("\n Pick a choice: ");
        scanf("%d", &choice1);
        printf("\n Give a number: ");
        scanf("%d", &num1);
        printf("\n Give another number: ");
        scanf("%d", &num2);
        switch (choice1) {
            case 1:
            printf("Sum is %d", num1 num2);
            break;
            case 2:
            printf("Difference is %d", num1-num2);
            break;
            case 3:
            printf("Product is %d", num1*num2);
            break;
            case 4:
            printf("Quotient is %d", num1/num2);
            break;
            default:
            printf("Please select a valid operation");
        }
            printf("\n Try another operation [y/n]: ");
            scanf("%c", &choice2); //changed %d to %c as the input is a char
            if (choice2 == 'y' || choice2 == 'Y') {
                printf("Retrying...");
                continue; } //changed break with continue
            else if (choice2 == 'n' || choice2 == 'N') {
                printf("Exiting...");
                check= 0; } //updated check so next iteration fails and terminates the loop
            else {
                printf("Error wrong input");
                check= 0; //there cannot be a 2nd try using if else statement
            }
    }
    return 0;
}

for more detail, you can read the docs

CodePudding user response:

I think you will have to do some major changes as shown below

#include <stdio.h>
int main() {
    while (1) {
        int choice1, num1, num2;
        char choice2;
        printf("\n  [1] Addition\n  [2] Subtraction\n  [3] Multiplication\n  [4] Division\n");
        printf("\n Pick a choice: ");
        scanf("%d", &choice1);
        printf("\n Give a number: ");
        scanf("%d", &num1);
        printf("\n Give another number: ");
        scanf("%d", &num2);
        switch (choice1) {
            case 1:
            printf("Sum is %d", num1 num2);
            break;
            case 2:
            printf("Difference is %d", num1-num2);
            break;
            case 3:
            printf("Product is %d", num1*num2);
            break;
            case 4:
            printf("Quotient is %d", num1/num2);
            break;
            default:
            printf("Please select a valid operation");
        }
            printf("\n Try another operation [y/n]: ");
            scanf("%s", choice2);

            if (choice2 == 'y' || choice2 == 'Y')
            {
                scanf("%d", &choice1);
            }
            else;
            {
                printf("Exiting...");
            }

    }
    return 0;
}

  • Related