0

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Something tells me `if(Salary<=10000 && Salary> 5000)` was what you had in mind. And whatever references suggested you `fflush(stdin)`, burn it. Invoking `fflush` on non-output streams invokes *undefined behavior*. That isn't how you eat `stdin` through any pending newlines. – WhozCraig Aug 21 '22 at 11:41
  • [using fflush(stdin);](https://stackoverflow.com/questions/2979209/using-fflushstdin) – Ed Heal Aug 21 '22 at 11:51
  • @WhozCraig sorry, i am new, so what do you mean by that?: (Invoking fflush on non-output streams invokes undefined behavior. That isn't how you eat stdin through any pending newlines.) – Caesar Dicax Aug 21 '22 at 11:54
  • You need to read all input - some you can just ignore. Also consider error conditions and how to handle that – Ed Heal Aug 21 '22 at 12:25

2 Answers2

1

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thank you so much..this is amazing! Just some questions if i may: 1- How to use getchar( ) twice in a code without using ffpurge or fflush? 2- In this line: scanf( " %c", &key );..why there is a space before [%c]? 3- Do you recommend some sources to learn from in-depth? Thank you – Caesar Dicax Aug 21 '22 at 12:26
  • @CaesarDicax You could use getchar twice checking whether a new line character is read. As for the leading space in the format string then I already wrote what it means. – Vlad from Moscow Aug 21 '22 at 12:53
0

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.

David
  • 146
  • 1
  • 9
  • I get it, thank you just a small question: what if i am using a single digit number, should it be enclosed in single quotes? e.g: var>'5' or var>5? – Caesar Dicax Aug 21 '22 at 11:51
  • 2
    Quick note, multiple characters in single quotes are not char arrays. They are multibyte chars, they have type `int` and implementation defined behaviour, if I recall correctly. – aulven Aug 21 '22 at 11:53
  • 1
    @CaesarDicax for ASCII characters (including numbers), surrounding with single quotes means that it's being treated like a character. It returns the ASCII value of it, for example, `'5' == 53`. If you intend to use `5` as "five the numeric value", you shouldn't put it in any sort of quotes. If you intend to use it as "5 the ASCII character", you should enclose it in single quotes. – aulven Aug 21 '22 at 12:03