y/n char loop c++

I was asked to do a program that calculates and displays an employee's gross pay starting from his name up to his days of works and absences, the program must include a loop (for, do, or do while). After that, the user has a choice whether to input another employee (where he will enter char y/n).

I don't know what is wrong with my codes but here it is.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

<#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main ()
{
    
string x;        
float lec;
float tlec;
float lab;
float tlab;
float xlec;
float txlec;
float xlab;
float txlab;
float ablec;
float tablec;
float ablab;
float tablab;
float tab;
float g = 473;
float gp;
char a;
int z;

cout << "ONE BALL UNIVERSITY" << endl;

for (;int z=1;)
{
    cout << "ENTER YOUR NAME: ";
    getline (cin, x);
    cout << "WELCOME " << x << endl;
    
    cout << "EARNINGS" << endl << "LECTURE" << endl;
    cout << "LECTURE: ";
    cin >> lec;
    tlec = lec * g;
    
    cout << "LABORATORY: ";
    cin >> lab;
    tlab = (g*.80) * lab;
    
    cout << "EXCESS: " << endl;
    cout << "LECTURE: ";
    cin >> xlec;
    txlec = (g*1.25) * xlec;
    
    cout << "LABORATORY: ";
    cin >> xlab;
    txlab = (g*xlab);
    
    cout << "ABSENCES:" << endl;
    cout << "LECTURE: ";
    cin >> ablec;
    tablec = (g*ablec);
    
    cout << "LABORATORY: ";
    cin >> ablab;
    tablab = (g*.8) * ablab;
    
    tab = tablec + tablab;
    gp = (tlec + tlab + xlec + xlab) - tab;
    
    cout << "GROSS PAY: "
    << gp << endl << endl;
    
    cout << "ENTER ANOTHER EMPLOYEE? <Y/N> ";
    cin >> a;
    
    if ( a == 'y' || a == 'Y')
    {
         z=1;
}
    else
    z++;
}
    


system ("pause");
return (0);
}
Last edited on
the for loop is wrong.
for loop (classic) syntax is

for(optional one time statements; optional condition; optional per loop statements)

as in
for(int x = 0; x < 1000; x++) //create and set x to zero once, until x is 1000, increment x every time in the loop.

your loop says
for(nothing; create integer and set it to 1 which is evaluated as true as a conditional expression; do nothing)

you probably mean this:
do
{
everything except z related statements
} while(toupper(a) != 'Y');

or doing it with a for loop (not really the best choice here)

for(int z = 0; z!= 1; )
{
stuffs
if ( a == 'y' || a == 'Y')
{
z=1;
}
//else //this is wrong removed
//z++;

}
My friend helped me and I figured it out. I used do while loop with limits and it worked out well. Thank you, jonnin!
int z was not necessary.

Just use an infinity loop such as


1
2
3
4
5
6
7
8
9
for(;;)
{
   ...
   stuffs
   ...
   cin >> a;
   if ( a == 'y' || a == 'Y')
      break;
}


or
1
2
3
4
5
6
7
8
9
while(1)
{
   ...
   stuffs
   ...
   cin >> a;
   if ( a == 'y' || a == 'Y')
      break;
}


or
1
2
3
4
5
6
7
do
{
   ...
   stuffs
   ...
   cin >> a;
} while (!( a == 'y' || a == 'Y'));


They are equivalent.
I was keeping it overly simple as he appears to be new at it.
This is probably the cleanest for loop approach, but its a do-while problem:

for(a='n'; a!='y' && a!='Y';)

the infinite loop & break technique is important to see and know, but I avoid it unless its the only way (here, it isnt). This is just a personal style thing.
Last edited on
Topic archived. No new replies allowed.