while loop will not stop

hey i have a problem with a code

this first code it will not stop

/*
Loop Program

CPT-168-A02
*/
#include <iostream>
using namespace std;
int main()
{

cout<<"/t/t/t**********************************"<<endl;
cout<<"/t/t/t* *"<<endl;
cout<<"/t/t/t* Loop Program *"<<endl;
cout<<"/t/t/t* CPT-168-A02 *"<<endl;
cout<<"/t/t/t**********************************"<<endl<<endl;

//Declare Variables
double HrsWrkd = 0.0;
double HrlyRate = 0.0;
double GrossPay = 0.0;
double BnS = 0.0;
int YrsWrkd = 0;

// enter input items
cout<<"Enter number of years worked: ";
cin>>YrsWrkd;
cout<<"Enter hours worked: ";
cin>>HrsWrkd;
cout<<"Enter hourly rate: ";
cin>>HrlyRate;
while (YrsWrkd != 99)
{
if (HrsWrkd > 40)
GrossPay = (40 * HrlyRate) + (HrsWrkd - 40) * 1.5 * HrlyRate;
else
GrossPay = HrsWrkd * HrlyRate;
if (YrsWrkd >= 1 && YrsWrkd <= 5)
BnS = GrossPay * 0.05;
else
if (YrsWrkd >= 6 && YrsWrkd <= 9)
BnS = GrossPay * 0.10;
else
if (YrsWrkd == 10)
BnS = GrossPay * 0.15;
else
if (YrsWrkd > 10)
BnS = GrossPay * 0.20;
else
cout<<"Invalid Number of Years Was Entered"<<endl;
//display output
cout<<"\nYour Gross Pay is: $"<<GrossPay<<endl;
cout<<"Your Bonus Is: $"<<BnS<<endl<<endl;
cout<<"THANK YOU"<<endl<<endl;
} //end while


return 0;
} //end of main function





this second code it will not show the final gross pay


/*
Loop Program

CPT-168-A02
*/
#include <iostream>
using namespace std;
int main()
{

cout<<"/t/t/t**********************************"<<endl;
cout<<"/t/t/t* *"<<endl;
cout<<"/t/t/t* Loop Program *"<<endl;
cout<<"/t/t/t* CPT-168-A02 *"<<endl;
cout<<"/t/t/t**********************************"<<endl<<endl;

//Declare Variables
double HrsWrkd = 0.0;
double HrlyRate = 0.0;
double GrossPay = 0.0;
double BnS = 0.0;
int YrsWrkd = 0;

// enter input items
cout<<"Enter number of years worked: ";
cin>>YrsWrkd;
cout<<"Enter hours worked: ";
cin>>HrsWrkd;
cout<<"Enter hourly rate: ";
cin>>HrlyRate;
while (YrsWrkd != 99)
{
if (HrsWrkd > 40)
GrossPay = (40 * HrlyRate) + (HrsWrkd - 40) * 1.5 * HrlyRate;
else
GrossPay = HrsWrkd * HrlyRate;
if (YrsWrkd >= 1 && YrsWrkd <= 5)
BnS = GrossPay * 0.05;
else
if (YrsWrkd >= 6 && YrsWrkd <= 9)
BnS = GrossPay * 0.10;
else
if (YrsWrkd == 10)
BnS = GrossPay * 0.15;
else
if (YrsWrkd > 10)
BnS = GrossPay * 0.20;
else
cout<<"Invalid Number of Years Was Entered"<<endl;
} //end while

//display output
cout<<"\nYour Gross Pay is: $"<<GrossPay<<endl;
cout<<"Your Bonus Is: $"<<BnS<<endl<<endl;
cout<<"THANK YOU"<<endl<<endl;
return 0;
} //end of main function
I don't see YrsWrkd being changed anywhere aside from by the initial std::cout... what do you have that while loop there for anyway?

-Albatross
iv tweaked it a litlle now

it is supposed to be enter the number of years worked or 99 to exit

here is the revised code

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
/*
    Loop Program
    
    CPT-168-A02
*/
#include <iostream>
using namespace std;
int main()
{

    cout<<"/t/t/t**********************************"<<endl;
    cout<<"/t/t/t*                                *"<<endl;
    cout<<"/t/t/t*       Loop Program             *"<<endl;
    cout<<"/t/t/t*       CPT-168-A02              *"<<endl;
    cout<<"/t/t/t**********************************"<<endl<<endl;

    //Declare Variables
    double HrsWrkd = 0.0; 
    double HrlyRate = 0.0; 
    double GrossPay = 0.0; 
    double BnS = 0.0;
    int YrsWrkd = 0;

    // enter input items
    do
    {
    cout<<"Enter number of years worked or 99 to exit: ";
    cin>>YrsWrkd;
    cout<<"Enter hours worked: ";
    cin>>HrsWrkd;
    cout<<"Enter hourly rate: ";
    cin>>HrlyRate;
     
    if (HrsWrkd > 40)
        GrossPay = (40 * HrlyRate) + (HrsWrkd - 40) * 1.5 * HrlyRate;
    else
        GrossPay = HrsWrkd * HrlyRate;
    if (YrsWrkd >= 1 && YrsWrkd <= 5)
        BnS = GrossPay * 0.05;
    else
        if (YrsWrkd >= 6 && YrsWrkd <= 9)
            BnS = GrossPay * 0.10;
        else 
            if (YrsWrkd == 10)
                BnS = GrossPay * 0.15;
            else
                if (YrsWrkd > 10)
                    BnS = GrossPay * 0.20;
                else
                    cout<<"Invalid Number of Years Was Entered"<<endl;
    //display output
    cout<<"\nYour Gross Pay is: $"<<GrossPay<<endl;
    cout<<"Your Bonus Is: $"<<BnS<<endl<<endl;
    }
    while (YrsWrkd != 99);
    cout<<"THANK YOU"<<endl<<endl;

    return 0;
}   //end of main function 
now when i hit 99 it does not break the loop
Last edited on
Here, i fixed it up 4 u, i used the character o instead of a number, enjoy your code and you have quite a lot of weak spots in your programming, don't keep using endl, use \n for new line =):
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
/*
    Loop Program
    
    CPT-168-A02
*/
#include <iostream>
using namespace std;
int main()
{

    cout << "/t/t/t**********************************\n";
    cout << "/t/t/t*                                *\n";
    cout << "/t/t/t*       Loop Program             *\n";
    cout << "/t/t/t*       CPT-168-A02              *\n";
    cout << "/t/t/t**********************************\n\n";

    //Declare Variables
    float HrsWrkd = 0.0;
    float HrlyRate = 0.0;
    float GrossPay = 0.0;
    float BnS = 0.0;
    int YrsWrkd = 0;
    char o, O;

    // enter input items
    do
    {
    cout << "Enter number of years worked or o to exit: ";
    cin >> YrsWrkd;
    cout << "Enter hours worked: ";
    cin >> HrsWrkd;
    cout << "Enter hourly rate: ";
    cin >> HrlyRate;
     
    if (HrsWrkd > 40)
        GrossPay = (40 * HrlyRate) + (HrsWrkd - 40) * 1.5 * HrlyRate;
    else
        GrossPay = HrsWrkd * HrlyRate;
    if (YrsWrkd >= 1 && YrsWrkd <= 5)
        BnS = GrossPay * 0.05;
    else
        if (YrsWrkd >= 6 && YrsWrkd <= 9)
            BnS = GrossPay * 0.10;
        else 
            if (YrsWrkd == 10)
                BnS = GrossPay * 0.15;
            else
                if (YrsWrkd > 10)
                    BnS = GrossPay * 0.20;
                else
                    cout << "Invalid Number of Years Was Entered\n";
    //display output
    cout << "\nYour Gross Pay is: $" << GrossPay << endl;
    cout << "Your Bonus Is: $" << BnS << "\n\n";
    }
    while (YrsWrkd != o || YrsWrkd != O);
    cout << "THANK YOU\n\n";

    return 0;
}   //end of main function  

A pretty nice program XD
good sample
Topic archived. No new replies allowed.