Sagnol
Sagnol

Reputation: 15

If statement in while loop not working

For the past 3 days, I have been having program trying to fix this code. Can someone here please help. The program doesn't print the last statement if a negative number is imputed. If a negative number is imputed, instead of printing "Terminating the program...", it jumps to print "Enter the first number:" before quitting.

int main(void)
{
    int choose, number1, number2, total_hold;
    do
    {
        printf("\n");
        printf("1: sum of two numbers \n");
        printf("2: difference of two numbers\n");
        printf("3: product of two numbers\n");
        printf("<0:terminate the program\n");
        printf("Select calculation:");
        scanf("%d", &choose);

        printf("Enter the first number:");
        scanf("%d", &number1);

        printf("Enter the second number:");
        scanf("%d", &number2);

        if (choose == 1)
        {
            total_hold = sumOfNumbers(number1, number2);
            displayS(number1, number2, total_hold);
        }
        else if (choose == 2)
        {
            total_hold = differenceOfNumbers(number1, number2);
            displayD(number1, number2, total_hold);
        }

        else if (choose == 3)
        {
            total_hold = MultiplicationOfNumbers(number1, number2);
            displayM(number1, number2, total_hold);
        }
        else if (choose < 0)
        {
            printf("Terminating the program...");
            break;
        }

    }
    while (choose > 0);

    return 0;
}

Upvotes: 1

Views: 103

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310920

Statements

printf("Enter the first number:");
scanf("%d", &number1);

printf("Enter the second number:");
scanf("%d", &number2);

are executed indpendently of the value entered in varaible choose because the last is tested after these statements.

The more correct design of the program could look like (without testing)

#include <stdio.h>

int main(void)
{
    while ( 1 )
    {
        enum { Exit = 0, Addition = 1, Difference = 2, Multiplication = 3 };

        unsigned int choose;
        int number1, number2, total_hold;

        printf( "\n" );
        printf( "%d: sum of two numbers \n", Addition );
        printf( "%d: difference of two numbers\n", Difference );
        printf( "%d: product of two numbers\n", Multiplication );
        printf( "%d: terminate the program\n", Exit );
        printf( "Select calculation:" );

        scanf( "%u", &choose );

        if ( choose == Exit )
        {
            printf( "Terminating the program..." );
            break;
        }

        if ( choose <= Multiplication )
        { 
            printf( "Enter the first number:" );
            scanf( "%d", &number1 );

            printf( "Enter the second number:" );
            scanf( "%d", &number2 );
        }

        switch ( choose )
        {
        case Addition:        {
            total_hold = sumOfNumbers(number1, number2);
            displayS(number1, number2, total_hold);
            break;

        case Difference:
            total_hold = differenceOfNumbers(number1, number2);
            displayD(number1, number2, total_hold);
            break;

        case Multiplication:
            total_hold = MultiplicationOfNumbers(number1, number2);
            displayM(number1, number2, total_hold);
            break;

        default:
            printf("Invalid selection. Try again.\n");
            break;
        }
    }

    return 0;
}

Upvotes: 1

Eugenio Mir&#243;
Eugenio Mir&#243;

Reputation: 2428

You're executing three scanf instructions before ANY if statement. If you want to check if choose variable is lower than 0 you can do it BEFORE the other two scanf calls like this

int main(void)
{
    int choose, number1, number2, total_hold;
    do
    {
        printf("\n");
        printf("1: sum of two numbers \n");
        printf("2: difference of two numbers\n");
        printf("3: product of two numbers\n");
        printf("<0:terminate the program\n");
        printf("Select calculation:");
        scanf("%d", &choose);

        if (choose < 0)
        {
            printf("Terminating the program...");
            break;
        }

        printf("Enter the first number:");
        scanf("%d", &number1);

        printf("Enter the second number:");
        scanf("%d", &number2);

        if (choose == 1)
        {
            total_hold = sumOfNumbers(number1, number2);
            displayS(number1, number2, total_hold);
        }
        else if (choose == 2)
        {
            total_hold = differenceOfNumbers(number1, number2);
            displayD(number1, number2, total_hold);
        }

        else if (choose == 3)
        {
            total_hold = MultiplicationOfNumbers(number1, number2);
            displayM(number1, number2, total_hold);
        }

    }
    while (choose > 0);

    return 0;
}

Upvotes: 1

barak manos
barak manos

Reputation: 30126

Place this:

if (choose < 0)
{
    printf("Terminating the program...");
    break;
}

Immediately after you scanf("%d", &choose).

In addition to that, you may as well change while (choose > 0) to while (1).

Upvotes: 2

Manjunath N
Manjunath N

Reputation: 1375

You are using a

do{
statement 1;
statement 2;
...
...
statement n;
}while(condition);

Hence in do-while loop all the statement are executed at-least once and then the condition is checked.

Better to use while loop instead of do-while

Upvotes: 1

Related Questions