Thread: How to use if else statement?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    How to use if else statement?

    EXERCISE 3
    Write a program to calculate a salary of a salesperson based on the unit of items sold.
     Get the staff name, salary and the units sold.
     Get the bonus percentage for sales as shown in the following table, based on the units sold.
    o The bonus amount is the bonus percentage of the salary.
     Calculate the total salary for the salesperson.
     Display the name, salary, units sold, bonus percentage, bonus amount and total salary.
     Design your own format of an appropriate output for this program.


    Hello guys. I need help to solve this kind of question. I have done my own programming but it seems to have some errors. I have tried and searched all the possible solutions to solve this matter but the outcomes are still the same. I would appreciate if someone can help me to solve this kind of problem. Thanks in advance.


    Below is the code that I have done. I hope you guys can fix it or give me a clue/hint on how to solve this question.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    void main()
    {
        int unit;
        float salary;
        char staff[20] , bonus;
    
    
        printf("\n-------------------------------------");
        printf("\n               BONUS                 ");
        printf("\n-------------------------------------");
    
    
        printf("\nEnter the staff name : ");
        gets (staff);
        printf("\nEnter the salary : RM ");
        scanf("%f", &salary);
        printf("\nEnter the units sold : ");
        scanf("%d", &unit);
    
    
        salary = unit * bonus;
    
    
        if (unit > 100 || unit < 200){
        strcpy(bonus, "0.02");
        }
        else if (unit > 201 || unit < 300){
        strcpy(bonus, "0.04");
        }
        else if (unit > 300){
        strcpy(bonus, "0.06");
        }
    
    
        salary = unit * bonus;
    
    
        printf("\nStaff name : %s ", staff);
        printf("\nSalary : %f ", salary);
        printf("\nUnits Sold : %d ", unit);
        printf("\nBonus Percentage : %.2f ", bonus);
        printf("\nBonus Amount : ");
        printf("\nTotal Salary : RM");
    
    
        return 0;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why is bonus a char? Seems to me a bonus should be a float, probably. You're trying to strcpy strings like "0.02" into bonus (by the way strcpy only works into strings, not single chars!)... wouldn't that be easier as [b]bonus = 0.02;[b]? This way the multiplication unit * bonus might actually work.

    You seem to be printing bonus as a float, in fact: %.2f.

    Your test conditions have some edge cases they don't handle. For example, if precisely 200 or 201 is entered, none of the bonuses will apply. Try <= and >=. Also it should probably be and's, not or's: every single number is either greater than 100 OR less than 200!

    Remember you can avoid redundant boolean expressions by turning
    Code:
    if(x >= 20 && x <= 30) ...
    else if(x >= 10 && x <= 20) ...
    else if(x >= 0 && x <= 10)
    into (say)
    Code:
    if(x >= 20) ...
    else if(x >= 10) ...
    else if(x >= 0)
    or some such. (With an extra check for > 30 if you needed to.)

    Other comments:
    Last edited by dwks; 12-07-2012 at 10:02 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    10
    I've edited the codes but now I keep getting errors like "expected expression before '>' token".

    I thought this is an expression
    Code:
    if (unit => 100 && unit <= 200)
    By the way, can I know what does this "[b] means?
    Code:
    [b]bonus = 0.02;[b]
    
    This is the code I've edited so far.
    Code:
    #include <stdio.h>
    
    
    int main()
    {
        int unit;
        float salary, bonus;
        char staff;
    
    
        printf("\n-------------------------------------");
        printf("\n               BONUS                 ");
        printf("\n-------------------------------------");
    
    
        printf("\nEnter the staff name : ");
        scanf("%c", &staff);
        printf("\nEnter the salary : RM ");
        scanf("%f", &salary);
        printf("\nEnter the units sold : ");
        scanf("%d", &unit);
    
    
        salary = unit * bonus;
    
    
        if (unit => 100 && unit <= 200) {
        bonus =0.02;
        }
        else if (unit => 201 && unit <= 300){
        bonus =0.04;
        }
        else (unit => 300){
        bonus =0.06;
        }
    
    
        salary = unit * bonus;
    
    
        printf("\nStaff name : %c ", staff);
        printf("\nSalary : %f ", salary);
        printf("\nUnits Sold : %d ", unit);
        printf("\nBonus Percentage : %.2f ", bonus);
        printf("\nBonus Amount : ");
        printf("\nTotal Salary : RM ");
    
    
        return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    => should be >=

    And else has no condition.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    In the if...else if...else if... construction the idea is that the final "else" can be used to handle "exceptional" cases. For example
    Code:
        if (unit => 100 && unit <= 200) {
            bonus =0.02;
        }
        else if (unit => 201 && unit <= 300){
            bonus =0.04;
        }
        else if (unit => 300){
            bonus =0.06;
        }
        else {
            printf("Invalid unit entered. Program will exit.\n");
            exit(127);
        }
    That's just one possibility

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    And if unit is less than 100, what is bonus then?

    What's the use of line 24 when you have line 38?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what -> mean in if statement?
    By byebyebyezzz in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2011, 11:34 AM
  2. OR statement in C
    By Keylac in forum C Programming
    Replies: 8
    Last Post: 11-26-2009, 09:09 AM
  3. Statement inside a statement.
    By JOZZY& Wakko in forum C Programming
    Replies: 15
    Last Post: 11-05-2009, 03:18 PM
  4. What does this statement mean?
    By starcatcher in forum C Programming
    Replies: 3
    Last Post: 02-03-2008, 10:55 AM
  5. Help with if statement
    By viryonia in forum C Programming
    Replies: 1
    Last Post: 02-11-2003, 09:52 PM