# Need help with small program to calculate a salary

• 01-10-2004
Guti14
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.

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; }```
• 01-10-2004
joshdick
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.
• 01-10-2004
Guti14
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?

• 01-11-2004
Salem
> 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;       }   }```
• 01-11-2004
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
• 01-11-2004
Salem
• 01-11-2004
WaltP
Quote:

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.
• 01-11-2004
Salem
Should probably compare sales1 with -1, not salary
• 01-11-2004
Guti14
I must have been really tired not to have seen that mistake :rolleyes:

But anyway, that didn't do the trick either. What could be missing that could make such a little program work :mad:
• 01-11-2004
Salem
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.
• 01-11-2004
Guti14
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; }```
• 01-11-2004
Salem
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
• 01-11-2004
Omnius
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; }```
• 01-11-2004
Guti14
Well, I did this, and now it seems to run to specifications.
```#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; }```