Thread: Need help with small program to calculate a salary

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Need help with small program to calculate a salary

    Hi,

    This program must used a while loop to calculate a salesperson's grosse income for one week.
    The output has to looklike this -


    Enter sales in dollars ( -1 to end ): 5000.00
    Salary is: $650.00

    Enter sales in dollars ( -1 to end ): 6000.00
    Salary is: $740.00

    Enter sales in dollars( -1 to end ): -1


    My program doesn't display the salary when Iv'e inputted the sales.
    Can anyone give me some advice please?

    Code:
    #include<iostream>
    #include<conio>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
       double   sales1;
       double   sales2;
       double   salary;
       double   fixed = 200;
    
    
       cout << "Enter sales in dollars ( -1 to end ): " << endl;
       cin >> sales1;
    
       while( sales1 != -1 ) {
         sales2 = sales1 / 100 * 9;
         salary = sales2 + fixed;
       }
    
          if( salary != -1 ) {
             cout << "Salary is: " << salary;
          }
    
       getch();
       return 0;
    }

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I don't understand your algorithm for determining the salary, but I do know that you have an error in your logic.

    In order for the while loop to end, sales1 must be -1. Therefore, the subsequent if statement will never be executed, because that output is only provided when sales1 does not equal -1.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    For example, a person who sells $5000 worth of chemicals in a week receives $200 plus 9 percent of $5000, or a total of $650.

    So I was thinking that the salesperson enters their sales as sales1. that's then read in.
    sales1 then goes into a calculation that is stored in sales2. this calulates who much pay the salesperson gets by dividing by 100 times 9. then add this(sales2) to fixed($200) which is what each person gets per week, and store it in salary.

    When I input 5000 for example, the cursor just moves to the next line without displaying something.

    And one more thing, does the if statement really need to be there?

    Thanks for any further advice.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > the cursor just moves to the next line without displaying something.
    Well your while loop doesn't contain enough code - like some input and output

    Try something like this
    Code:
       while( sales1 != -1 ) {
         cout << "Enter sales in dollars ( -1 to end ): " << endl;
         cin >> sales1;
         sales2 = sales1 / 100 * 9;
         salary = sales2 + fixed;
          if( salary != -1 ) {
             cout << "Salary is: " << salary;
          }
       }
    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
    Aug 2003
    Posts
    71
    Hi Salem

    Thanks for your advice. But alas, it still doesn't output anything. What your'e solution did was when I inputed 5000.00 fo example and pressed enter, it just asked for input again, not displaying "Salary is: 650"
    Iv'e set sales1, sales2, salary all to zero, but that didn't do the trick either.
    Any other ideas?

    Thanks

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, post your latest code
    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.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Guti14
    Hi Salem

    Thanks for your advice. But alas, it still doesn't output anything. What your'e solution did was when I inputed 5000.00 fo example and pressed enter, it just asked for input again, not displaying "Salary is: 650"
    Iv'e set sales1, sales2, salary all to zero, but that didn't do the trick either.
    Any other ideas?

    Thanks
    The help Salem gave you was absolutely correct. What you neglected to do was think about what he suggested and how it affects what you previously wrote. His response was not designed to completely fix your problem, but with thought allow you to fix it.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Should probably compare sales1 with -1, not salary
    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.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    I must have been really tired not to have seen that mistake

    But anyway, that didn't do the trick either. What could be missing that could make such a little program work

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I suggest you goto www.psychic-programming-network.com, because they're the only people who'll be able to help you if you keep replying without posting your latest code.
    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.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Code:
    #include<iostream>
    #include<conio>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
       double   sales1;
       double   sales2;
       double   salary;
       double   fixed = 200;
    
    
       cout << "Enter sales in dollars ( -1 to end ): " << endl;
       cin >> sales1;
    
       while( sales1 != -1 ) {
         sales2 = sales1 / 100 * 9;
         salary = sales2 + fixed;
       }
    
          if( sales1 != -1 ) {
             cout << "Salary is: " << salary;
          }
    
       getch();
       return 0;
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your "latest" code looks identical to your original code except for
    21c21
    < if( salary != -1 )
    ---
    > if( sales1 != -1 )


    Your while loop still does not contain any cin or cout statements, so yeah it will still loop forever if you don't type in -1 the first time

    Oh sod it, I'm done talking to you
    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.

  13. #13
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Take a look at the minor differences between your code and this:

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
       double   sales1;
       double   sales2;
       double   salary;
       double   fixed = 200;
    
       cout << "Enter sales in dollars ( -1 to end ): " << flush;
       cin >> sales1;
    
       while( sales1 != -1 ) {
         sales2 = sales1 / 100 * 9;
         salary = sales2 + fixed;
         cout << "Enter sales in dollars ( -1 to end ): " << flush;
         cin >> sales1;
       }
    
       cout << "Salary is: " << salary;
    
       cin.ignore();
       cin.get();
       return 0;
    }

  14. #14
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Well, I did this, and now it seems to run to specifications.
    Thanks for your help anyway

    Code:
    #include<conio>
    #include<iostream>
    using namespace std;
    
    int main()
    {
       double   sales1;
       double   sales2;
       double   salary;
       double   fixed = 200;
    
       cout << "Enter sales in dollars ( -1 to end ): ";
       cin >> sales1;
    
       while( sales1 != -1 ) {
         sales2 = sales1 / 100 * 9;
         salary = sales2 + fixed;
    
         cout << "Salary is: " << salary << endl << endl;
    
         cout << "Enter sales in dollars ( -1 to end ): ";
         cin >> sales1;
       }
    
       getch();
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 12:53 PM
  2. Program to Calculate Molar Mass
    By ebrayer in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 03:20 PM
  3. Help with small simple program
    By Sonor in forum C Programming
    Replies: 5
    Last Post: 04-02-2005, 07:40 AM
  4. Help writing small program
    By guitarhead2000 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2004, 12:42 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM