Home > OS >  I have a problem with if statement (it skips all conditions and doesn't wait for user input)
I have a problem with if statement (it skips all conditions and doesn't wait for user input)

Time:08-21

I have been studying C language for some time now, and I have a certain bug that I can't seem to be able to fix.

As part of my revision, I started coding a very simple app for stock market.

Whenever I build and run the program, the program doesn't wait for user input to enter (Y) or (N), and skips checking [If] and [else if], and instead executes [else] condition.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int Salary;
    char key;

    printf("Enter your salary: \n");
    scanf("%d",&Salary);
    printf("Your salary = %d.\n", Salary);

    if(Salary<='10000'&&Salary>'5000')
        {
            printf("Congratulations, You are a Platinum-Tier Client!\n");
            printf("Do You want to view our specifically-tailored offers for you?\n");
            printf("Type \"Y\" for Yes, and \"N\" for No.\n\n\n");
            key=getchar();
            fflush(stdin);
            if(key=='Y')
            {
                printf("Here are our offers for you, enjoy!\n\n");
                printf("Kindly Type the number of the offer you want then press Enter.\n");
                printf("1- Real Estates.\n");
                printf("2- Stocks.\n");
                printf("3- Bonds.\n");
                printf("4- STRIPS.\n");
                printf("5- Funds.\n");
            }

            else if(key=='N')
            {
                printf("Well! Have a nice day!\n");
                printf("Kindly press Enter to exit!\n");
            }

            else
            {
                printf("Wrong choice!\n");
                printf("Type \"Y\" for Yes, and \"N\" for No.\n\n\n");
            }
        }

    else if(Salary<='5000'&&Salary>='3000')
        {
            printf("Congratulations, You are a Gold-Tier Client\n");
            printf("Do You want to view our specifically-tailored offers for you?\n");
            printf("Type \"Y\" for Yes, and \"N\" for No.\n\n\n");
            key=getchar();
            if(key=='Y')
            {
                printf("Here are our offers for you, enjoy!\n\n");
                printf("Kindly Type the number of the offer you want then press Enter.\n");
                printf("1- Real Estates.\n");
                printf("2- Stocks.\n");
                printf("3- Bonds.\n");
                printf("4- STRIPS.\n");
                printf("5- Funds.\n");
            }

            else if(key=='N')
            {
                printf("Well! Have a nice day!\n");
                printf("Kindly press Enter to exit!\n");
            }

            else
            {
                printf("Wrong choice!\n");
                printf("Type \"Y\" for Yes, and \"N\" for No.\n\n\n");
            }
        }

    else if(Salary<'3000'&&Salary>'1')
        {
            printf("Congratulations, You are a Silver-Tier Client\n");
            printf("Do You want to view our specifically-tailored offers for you?\n");
            printf("Type \"Y\" for Yes, and \"N\" for No.\n\n\n");
            key=getchar();
            if(key=='Y')
            {
                printf("Here are our offers for you, enjoy!\n\n");
                printf("Kindly Type the number of the offer you want then press Enter.\n");
                printf("1- Real Estates.\n");
                printf("2- Stocks.\n");
                printf("3- Bonds.\n");
                printf("4- STRIPS.\n");
                printf("5- Funds.\n");
            }

            else if(key=='N')
            {
                printf("Well! Have a nice day!\n");
                printf("Kindly press Enter to exit!\n");
            }

            else
            {
                printf("Wrong choice!\n");
                printf("Type \"Y\" for Yes, and \"N\" for No.\n\n\n");
            }
         }
    else
        {
           printf("Sorry! No offers for you!\n");
        }
    return 0;
}

The result is always (If I write 5000 for example):

Enter your salary:
5000
Your salary = 5000.
Congratulations, You are a Silver-Tier Client
Do You want to view our specifically-tailored offers for you?
Type "Y" for Yes, and "N" for No.


Wrong choice!
Type "Y" for Yes, and "N" for No.

What am I missing here?

Thank you for your help, and sorry if the question is kind of lame.

CodePudding user response:

You are using multibyte integer character constants as for example in this if statement

if(Salary<='10000'&&Salary>'5000')

values of which are implementation defined. Instead use integer constants like

if ( 5000 < Salary && Salary <= 10000 )

After this if statement the next if statement can be written like

else if ( 3000 <= Salary )

instead of

else if(Salary<='5000'&&Salary>='3000')

and so on for other if statements.

Using the function fflush for input stream

fflush(stdin);

has undefined behavior. Remove that statement.

This call of getchar

key=getchar();

reads the new line character '\n' stored in the input buffer after pressing the Enter key when a value of Salary was entered.

Instead use scanf the following way

scanf( " %c", &key );

Pay attention to the leading space in the format string. It allows to skip white space characters.

CodePudding user response:

So just from a quick look, try removing the ' ' from if(Salary<='10000'&&Salary>'5000') so it's if(Salary<=10000 && Salary>5000) When you surround a number with ' ' it's no longer an integer, it's an array of chars, or a string. You're comparing Salary to the ascii value of the string '5000' not the integer number 5000.

  • Related