By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,207 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,207 IT Pros & Developers. It's quick & easy.

Trouble with the While Statement

P: 12
First up thanks to the guys who pointed out my error last week.

Now I was wondering If I could get some help with this problem because again I have fallen into trouble. Basically im using the while statement to loop a program but dont know how to loop back to the request of Enter hours worked or (-1 to end) : 39. I cant put it in the while statement as it contains the condition that activates the while statement, if you see what I mean?

Im trying to get a process like so

Sample screen input/ouput

Enter hours worked or (-1 to end) : 39
Enter hourly rate of worker (00.00) : 10.00
Salary is 390.00

Enter hours worked or (-1 to end) : 40
Enter hourly rate of worker (00.00) : 10.00
Salary is 400.00

Enter hours worked or (-1 to end) : 41
Enter hourly rate of worker (00.00) : 10.00
Salary is 415.00

Enter hours worked or (-1 to end) : -1

But im getting this....

Enter hours worked or (-1 to end) : 39
Enter hourly rate of worker (00.00) : 10.00
Salary is 390.00

Enter hourly rate of worker : 10.00
Salary is 390.00 (it uses my original value of hours and i dont know how to allow the user to input a new value of hours worked for a new worker.

Heres my program...



Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cmath>
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7.  
  8. int main ()
  9. {
  10. double hours;
  11. double Pay_per_hour;
  12. double Total_Pay;
  13.  
  14. cout << "Enter hours worked or -1 to end: ";
  15. cin >> hours;
  16.  
  17. while (hours != -1)
  18. {
  19.    cout << "Enter hourly rate of the worker ";
  20.    cin >> Pay_per_hour;
  21.  
  22.    if (hours <= 40)
  23.       Total_Pay = hours * Pay_per_hour;
  24.    else 
  25.       Total_Pay = ( (hours - 40) * 15 ) + 400;
  26.  
  27.    cout << "Salary is  " << Total_Pay << endl;
  28.  
  29. }
  30.  
  31.  
  32. return 0;
  33.  
  34. } // end main
Nov 27 '07 #1
Share this Question
Share on Google+
4 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Your hours prompt and input need to be inside your while loop as well.
Nov 27 '07 #2

P: 28
You can move your prompt to hours in the loop and use a break statement to come out of loop. Like this...

Expand|Select|Wrap|Line Numbers
  1. //cout << "Enter hours worked or -1 to end: ";
  2. //cin >> hours;
  3. // move the above two inside the loop.
  4.  
  5. while (1) // make loop unconditional.
  6. {
  7.    cout << "Enter hours worked or -1 to end: ";
  8.    cin >> hours;
  9. //  ... and put a check for hours
  10.    if (hours < 0)
  11.       break;// break will take you out of loop.
  12.    cout << "Enter hourly rate of the worker ";
  13.    cin >> Pay_per_hour;
  14.  
  15.    if (hours <= 40)
  16.       Total_Pay = hours * Pay_per_hour;
  17.    else
  18.       Total_Pay = ( (hours - 40) * 15 ) + 400;
  19.  
  20.    cout << "Salary is  " << Total_Pay << endl;
  21.  
  22. }
  23.  
  24.  
Nov 27 '07 #3

P: 12
Your hours prompt and input need to be inside your while loop as well.
Thanks, but the problem I then have is when I click -1, the program does not terminate but still asks me the hourly rate of the worker and then works out his/her wage. When the user clicks -1 I want the program to terminate straight away but I cant do it.
Nov 27 '07 #4

P: 12
You can move your prompt to hours in the loop and use a break statement to come out of loop. Like this...

Expand|Select|Wrap|Line Numbers
  1. //cout << "Enter hours worked or -1 to end: ";
  2. //cin >> hours;
  3. // move the above two inside the loop.
  4.  
  5. while (1) // make loop unconditional.
  6. {
  7.    cout << "Enter hours worked or -1 to end: ";
  8.    cin >> hours;
  9. //  ... and put a check for hours
  10.    if (hours < 0)
  11.       break;// break will take you out of loop.
  12.    cout << "Enter hourly rate of the worker ";
  13.    cin >> Pay_per_hour;
  14.  
  15.    if (hours <= 40)
  16.       Total_Pay = hours * Pay_per_hour;
  17.    else
  18.       Total_Pay = ( (hours - 40) * 15 ) + 400;
  19.  
  20.    cout << "Salary is  " << Total_Pay << endl;
  21.  
  22. }
  23.  
  24.  
Cheers mohammad, that was what I was struggling with. Thanks again

I'd never come across the while(1) statement, so hadnt a chance
Nov 27 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.