Loop sentient value help

I have did a code for school lately and feel most things are correct but after it will correctly run and do all functions it won't end the loop. Any help with my while statement would be appreciated since I can't seem to figure it out alone for now. The sentient values to stop loop should be 0,99 or greater, and any negative number.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 // my name
// CPT-168-
// Loop Gross Pay

#include <iostream>
#include <iomanip>	
using namespace std;

int main()
{
	system("color f0");
	cout<<fixed<<setprecision(2);
	cout<<"\t\t**********************************"<<endl;
	cout<<"\t\t*     my name                    *"<<endl;
	cout<<"\t\t*     CPT-168-                   *"<<endl;
	cout<<"\t\t*   Loop Gross Pay               *"<<endl;
	cout<<"\t\t**********************************"<<endl<<endl;

	//declare variables
	double hr    =0.0;
	double hw    =0.0;
	double gp    =0.0;
	double bn    =0.0;
	double yw    =0.0;

	//enter input items
	while ( (yw!=0) || (yw<0) || (yw<99) )
	{
	cout << "Enter number of years worked or (0,99 or higher, negative number) to exit: ";
	cin >>yw;
	cout << "Enter hours worked: ";
	cin >> hw;
	cout << "Enter hourly rate: ";
	cin>> hr;
	cout<<endl;

	//calculate totals
	if (hw > 40)
		gp=(40*hr) + ((hw-40) * (hr*1.5));
	else 
		gp= hw*hr;
	if (yw>= 1 && yw <=5)
		bn=gp*.05;
	else
		if (yw>=6 && yw<=9)
		bn=gp*.1;
		else
			if (yw>10)
			bn=gp*.2;
			else
			if (yw=10)
			bn=gp*.15;
	

	//display 
	cout<<"Your Gross Pay is: $"<<gp<<endl;
	cout<<"Your Bonus is: $"<<bn<<endl<<endl;
	}
	cout << "Thank You!" << endl;
	
	system("pause");
	return 0;
}//end of main function
Last edited on
The "Or" function is ||, not |. | is the bitwise operator of "Or," which doesn't set a true-false flag depending on whether any read true- rather, it takes two sets of data and computes, in binary, the combination of both (so 001 | 010 becomes 011). Anyway, just add the second | next to each, and maybe a few excess parentheses around each evaluation, and you should be good to go.
Not sure how I overlooked that but thanks for the feedback! However after i changed it and updated original post to show it will still run fine but when i enter 0 it still prompts me for hours worked so maybe i have some error in my logic style instead.
1
2
3
	cin >>yw;
	cout << "Enter hours worked: ";
	cin >> hw;
There is nothing in your code which could prevent next line execution. You should check for sentiel values and break your loop if needed.
I was able to get the code fixed. thanks for the feedback.
( (yw!=0) || (yw<0) || (yw<99) ) is always true. yw!=0 is true for all values except 0. yw<99 is true when yw is 0. So when you "or" these two together you always get "true".

Also, if yw<99 then it will always be less than 0, so yw<0 is not necessary.

Dave
Topic archived. No new replies allowed.