1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Solving for percentage Rates.

Discussion in 'C++' started by Prof. Krauf, Jul 1, 2009.

  1. Prof. Krauf

    Prof. Krauf New Member

    Joined:
    Jun 20, 2009
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    I'm attempting a code here. I'm still a little scrappy and I'm not quite sure what I should be using here. I'm trying to create a code where the user inputs a name for an employee and a sales rate. Each employee gets payed 2,500 monthly, but Based on the sales rate they should find out their commission rate and it's dependent on this.
    [FONT=&quot]$0.01–$5,000 8 percent[/FONT]
    [FONT=&quot]$5,000.01–$10,000 10 percent[/FONT]
    [FONT=&quot]$10,000.01 and above 12 percent[/FONT]

    I just need a little help here I think I need a case or switch statement, but I'm not even sure where to begin or place the needed codes.

    My code
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    int main()
    {
    //declare variables
    char lastname[50];
    int comission
    //get employee name
    cout << "Enter the Employee's last name: ";
    cin.getline (lastname, 50);
     int salary = 2500
    //get sales rate
    case '1'
    if commission 5000 <
    
    
    return 0;
    }
    
    
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    What's that ??

    For your question ... It's better to use nested Ifs than using Switch Case. Case checks for a particular value, but you need to check a range.

    So, do it something like this :
    Code:
    .
    .
    if( Salary > 10000 )
          Commission = 12;
    else if( Salary > 5000 )
          Commission = 10;
    else
          Commission = 8;
    .
    .
    
     
  3. Prof. Krauf

    Prof. Krauf New Member

    Joined:
    Jun 20, 2009
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Like this?

    So it should be something like this?

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
          //declare variables
          char lastname[50];
          char firstname[50]
          int comission;
    
          //get employee name
          cout << "Enter the Employee's first and last name last name: ";
          cin >> getline (lastname, 50);
          int salary = 2500;
          
          //get sales rate
          double commissionRate = 0.0;
    
          if(commission <= 5000)
                commisionRate  = 0.08;
          else if(commission <= 10000 && commission > 5000)
                commissionRate = 0.10;
          else
                commissionRate = 0.12;
    
          cout << "firstname" << "lastname" << "commissionRate" <<
    
          return 0;
          system ("pause")
    }
     
    Last edited by a moderator: Jul 2, 2009
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    No problem, I did it for you :)

    As your post count is low, you cannot edit your posts. Keep posting and soon you will have edit button.
     
  5. Prof. Krauf

    Prof. Krauf New Member

    Joined:
    Jun 20, 2009
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    So should the code look like that? I think I need to add endline after the commissionRate.
     
  6. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    No !!! There are so many errors in your program. Let me highlight and explain :

    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
          //declare variables
          char lastname[50];
          char firstname[50]     [COLOR="Red"][B]// Missing ';'[/B][/COLOR]
          int comission;
    
          //get employee name
          cout << "Enter the Employee's first and last name last name: ";
    [COLOR="Red"][B]      // you only get the lastname in this case !![/B][/COLOR]
          cin >> getline (lastname, 50);
          int salary = 2500;
          
          //get sales rate
          double commissionRate = 0.0;
    
    [COLOR="Red"][B]      // You are using "commission" without assigning a value to it first ![/B][/COLOR]
          if(commission <= 5000)
                commisionRate  = 0.08;
          else if(commission <= 10000 && commission > 5000)
                commissionRate = 0.10;
          else
                commissionRate = 0.12;
    
    [COLOR="Red"][B]      // LOL, firstname, lastname etc.. should not be in quotes, 'cuz they are vars.
          // And where is the ';' ??
    [/B][/COLOR]      cout << "firstname" << "lastname" << "commissionRate" <<
    
          return 0;
    
          [COLOR="Red"][B]// You are pausing after returning from main() !![/B][/COLOR]
          system ("pause")      [COLOR="Red"][B]// And where is ';' ??[/B][/COLOR]
    }

    The correct program can be like this : (Run it and tell me if it's useful)
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
          //declare variables
          char lastname[50];
          char firstname[50];
          double commissionRate;
          int commission;
    
          //get employee name
          cout << "Enter the Employee's first and last name last name: ";
          cin >> firstname >> lastname;
          int salary = 2500;
    
          //get sales rate
          cout << "Please enter sales rate : ";
          cin >> commission;
    
          if(commission <= 5000)
                commissionRate  = 0.08;
          else if(commission <= 10000 && commission > 5000)
                commissionRate = 0.10;
          else
                commissionRate = 0.12;
    
          cout << "\nNAME = " << firstname << " " << lastname << "\nSALARY = " << salary;
          cout << "\nSALES = " << commission << "\nCOMMISSION RATE = " << commissionRate;
    
          getchar();
          cout << "\n\nPress any key to exit ...\n";
          getchar();
    
          return 0;
    }
    
    NOTE that, I changed system("pause") to manual a pause, which has the benefit that it's not OS specific.
     
    Last edited: Jul 2, 2009
    shabbir likes this.
  7. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    What about having edit button for all post ;) ?
     
  8. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    That's true for the MODs (I'm one of 'em!).
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,276
    Likes Received:
    364
    Trophy Points:
    83
    Users cannot edit all posts because there is no point in allowing to edit some old threads / posts and also new members are not allowed for spam reasons.

    Mods are exceptions.
     
  10. Prof. Krauf

    Prof. Krauf New Member

    Joined:
    Jun 20, 2009
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    All right I appreciate the help.
     
  11. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    My pleasure :)
     

Share This Page