473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Trouble with the While Statement

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
4 1980
sicarie
4,677 Expert Mod 4TB
Your hours prompt and input need to be inside your while loop as well.
Nov 27 '07 #2
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
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
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

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

Similar topics

0
by: lowellturner | last post by:
We recently had to reload an app to a Linux server from its Window counterpart. I've tried setting the lower case global in my.cnf to al three values, but none work. Previously, this worked fine....
3
by: Jason | last post by:
I am having trouble using the CONTAINS function in sql server(enterprise manager). I am typing the following: Select * FROM mytable WHERE CONTAINS(myfield,'mystring') This returns the...
4
by: Driesen via SQLMonster.com | last post by:
Hi guys I having trouble with this sproc. I get the following error when testing: Server: Msg 245, Level 16, State 1, Procedure UTL_CompletenessCheckLoan, Line 231 Syntax error converting the...
0
by: cwbp17 | last post by:
I'm having trouble updating individual datagrid cells. Have two tables car_master (columns include Car_ID, YEAR,VEHICLE) and car_detail (columns include Car_ID,PRICE,MILEAGE,and BODY);both tables...
7
by: Gary Paris | last post by:
I have a bound datagrid on my windowsform. I have the following declarations in the global module of the project: Public eDS As DataSet Public eDA As OleDb.OleDbDataAdapter In the Load...
9
by: Nathan Sokalski | last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action that I want to perform on each of the Array values is: Private Function AddQuotes(ByVal value As String) As String Return...
0
by: AceX | last post by:
I'm having trouble with this if statement: if($line=$ignore { $num--; } $ignore is defined as the string "ig"
2
blazedaces
by: blazedaces | last post by:
I'm designing a GUI for autonomous robot interaction, but that's not so important. To figure out the ins and outs of a certain aspect of PyQt I usually just do what I did when I learning Qt, I grab...
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.