0

Write a C Program to Calculate the commission of a salesman considering three regions X,Y,Z depending on the sales amount as follows:

FOR AREA CODE - X

| sales amount | Commission |
| --------     | --------   |
| <1000        | 10%        |
| <5000        | 12%        |
| >=5000       | 15%        |

-----------------------------

FOR AREA CODE - Y

| sales amount | Commission |
| --------     | --------   |
| <15000       | 10%        |
| <7000        | 12%        |
| >=7000       | 15%        |

-----------------------------

FOR AREA CODE - Z

| sales amount | Commission |
| --------     | --------   |
| <12000       | 10%        |
| <5600        | 12%        |
| >=6500       | 15%        |

-----------------------------

I take the input as a character for the area code and read it, then I take the input for the sales amount and commission to calculate the commission as a float and read, after that, I check the condition like this -

    float amt, comission;
    char area;

    printf("Area code is - 'X','Y' & 'Z'");
    printf("\nEnter Your Area Code: ");
    scanf("%c", &area);
    printf("Enter Amount: ");
    scanf("%f", &amt);

    if (area == 'X' && amt < 1000)
    {
        printf("You got 10%% Commission");
        comission = ((100 - 10) * amt) / 100;
        printf("After Commision The Slaes amount is: %6.2f", comission);
        if (amt >= 1000 && amt < 5000)
        {
            printf("You got 12%% Commission");
            comission = ((100 - 12) * amt) / 100;
            printf("After Commision The Slaes amount is: %6.2f", comission);
        }

        if (amt >= 5000)
        {
            printf("You got 15%% Commission");
            comission = ((100 - 15) * amt) / 100;
            printf("After Commision The Slaes amount is: %6.2f", comission);
        }
    }

same codes for Y and Z using else if() condition. But I did not get the expected output. after read the values my program automatically terminates.

  • 4
    Your compiler should be complaining about that code. If it doesn't then enable more warnings. And treat warnings as errors that needs to be fixed. – Some programmer dude Jan 30 '23 at 15:49
  • 1
    You shouldn't be editing the question correcting what someone suggested you to do as an answer, since that generates confusion and invalidates the answer. – mikyll98 Jan 30 '23 at 16:01
  • 3
    It's not a good SO etiquette to edit the question after it has received answers / comments, and invalidate them all. – Harith Jan 30 '23 at 16:06
  • Tip, add a space `scanf("%c", &area);` --> `scanf(" %c", &area);` to not save whitespace characters. – chux - Reinstate Monica Jan 30 '23 at 18:16

1 Answers1

1

You're missing a reference operator (&) in the second scanf():

scanf("%f", amt); // <--- missing &

However, as suggested by Some programmer dude, you should be using compiler options to display warnings and errors. For example, if you just use -Wall or -Wall -Werror (supposing you're compiling with gcc) the compiler already suggests you what's wrong with your code.

  • with -Wall option you get a warning and the compilation succeeds but the program won't work as expected:
<source>:12:13: warning: format '%f' expects argument of type 'float *', but argument 2 has type 'double' [-Wformat=]
   12 |     scanf("%f", amt);
      |            ~^   ~~~
      |             |   |
      |             |   double
      |             float *
<source>:12:5: warning: 'amt' is used uninitialized [-Wuninitialized]
   12 |     scanf("%f", amt);
      |     ^~~~~~~~~~~~~~~~
<source>:5:11: note: 'amt' was declared here
    5 |     float amt, comission;
      |           ^~~
  • with -Wall -Werror, the warnings are treated as errors and the compilation doesn't even succeeds:
<source>:12:13: error: format '%f' expects argument of type 'float *', but argument 2 has type 'double' [-Werror=format=]
   12 |     scanf("%f", amt);
      |            ~^   ~~~
      |             |   |
      |             |   double
      |             float *
<source>:12:5: error: 'amt' is used uninitialized [-Werror=uninitialized]
   12 |     scanf("%f", amt);
      |     ^~~~~~~~~~~~~~~~
<source>:5:11: note: 'amt' was declared here
    5 |     float amt, comission;
      |           ^~~
cc1: all warnings being treated as errors

I suggest you to have a look at this thread, which gives you a "general smattering" on which gcc flags are useful the most.

mikyll98
  • 1,195
  • 3
  • 8
  • 29