Loop(Specifying amount of employees user can input)

Here's what I have so far.....I still can't figure out what I'm doing wrong. I've been working on this problem for a week now. Very new to C++ :

#include<iostream.h>
main(){
int numberofemployees;
int employeeid, hoursworked;
float hourlyrate,grosspay,taxamount,netpay;
const float TAXRATE = 0.10;
it counter = 0;
cout<<"Please enter the number of employees:";
cin>>n;
while(counter < n){
cout<<"ENTER THE EMPLOYEE ID:";
cin>>employeeid;
cout<<"ENTER THE HOURS WORKED:";
cin>>hoursworked;
cout<<"ENTER THE HOURLY RATE:";
cin>>hourlyrate;
grosspay=hoursworked*hourlyrate;
taxamount=grosspay*TAXRATE;
netpay=grosspay-taxamount;
cout<<"THE HOURS WORKED ARE "<<hoursworked<<endl;
cout<<"THE GROSSPAY IS "<<grosspay<<endl;
cout<<"THE TAX AMOUNT IS "<<taxamount<<endl;
cout<<"THE NETPAY IS "<<netpay<<endl;
}//WHILE
system("PAUSE");
return 0;

}//MAIN
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
#include<iostream.h>
int main()
{
    int numberofemployees;
    int employeeid, hoursworked;
    float hourlyrate,grosspay,taxamount,netpay;
    const float TAXRATE = 0.10;
    int counter = 0;
    cout<<"Please enter the number of employees:";
    cin>>n;
    while(counter < n)
    {
        cout<<"ENTER THE EMPLOYEE ID:";
        cin>>employeeid;
        cout<<"ENTER THE HOURS WORKED:";
        cin>>hoursworked;
        cout<<"ENTER THE HOURLY RATE:";
        cin>>hourlyrate;
        grosspay=hoursworked*hourlyrate;
        taxamount=grosspay*TAXRATE;
        netpay=grosspay-taxamount;
        cout<<"THE HOURS WORKED ARE "<<hoursworked<<endl;
        cout<<"THE GROSSPAY IS "<<grosspay<<endl;
        cout<<"THE TAX AMOUNT IS "<<taxamount<<endl;
        cout<<"THE NETPAY IS "<<netpay<<endl;
    }//WHILE
    system("PAUSE");
    return 0;
}//MAIN 


On line 8, it should be int counter = 0;. Probably just a typo, right?

In your while loop, you have the expression counter<0, but where in the body of the loop do you increment counter? If counter starts at 0 and never changes value, it will always be less than the value of "n", and your while loop is infinite.
Last edited on
I'm sorry I just don't understand I've only been programming for two weeks now and this is all new to me. This is what I have now and it still isn't executing, what am I doing wrong???

#include<iostream.h>
int main()
{
int numberofemployees;
int employeeid, hoursworked;
float hourlyrate,grosspay,taxamount,netpay;
const float TAXRATE = 0.10;
int counter = 0;
cout<<"Please enter the number of employees:";
cin>>n;
while(counter < n)
{
cout<<"ENTER THE EMPLOYEE ID:";
cin>>employeeid;
cout<<"ENTER THE HOURS WORKED:";
cin>>hoursworked;
cout<<"ENTER THE HOURLY RATE:";
cin>>hourlyrate;
grosspay=hoursworked*hourlyrate;
taxamount=grosspay*TAXRATE;
netpay=grosspay-taxamount;
cout<<"THE HOURS WORKED ARE "<<hoursworked<<endl;
cout<<"THE GROSSPAY IS "<<grosspay<<endl;
cout<<"THE TAX AMOUNT IS "<<taxamount<<endl;
cout<<"THE NETPAY IS "<<netpay<<endl;
}//WHILE
system("PAUSE");
return 0;
}//MAIN
Basically, you need to add a line of code to the body of the while loop that increases the value of counter at the end of each iteration. After 1 iteration, counter =1, after the next one, counter =2, and so on.

There are several ways to do this, but the most common way is ++variable.

EDIT: You also forgot to declare the variable "n"

EDIT: iostream.h has been depricated for a while now. Try using <iostream> instead and add the line using namespace std; underneath it.
Last edited on
Ok now where do I declare the variable "n" and how does it look? After you answer this I think I can figure it out
What is the variable int numberofemployees; for and where is it used in your code?

What is the variable int n; for and where is it used in your code?

Last edited on
I'm completely lost. Is there any way you can show me how a finished product looks like that actually executes so I can see where I made my mistakes and compare?
I guess I don't need int numberofemployees; ?
You declare int numberofemployees; but you don't use it. From the variable name, I assume you intended to use it as the value to which int counter; is compared.

You don't declare "n", but you use it as the value to which int counter; is compared.

Do you see now? You need to decide whether to use "numberofemployees" or "n" as the variable, declare it, and use it in the test expression of the while loop

Last edited on
You have to declare a variable before it is used in your code like the variable n; that you called but never defined it.




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
#include <iostream>
using namespace std;


int main()
{

string employeeid; // the reason i changed this to a string is if you ever want to do string comparisons with employeeids.
int hoursworked;
float hourlyrate,grosspay,taxamount,netpay;
float TAXRATE = 0.10;
int counter = 0;
int n; // number of employees.

cout<<"Please enter the number of employees:";
cin>>n;


    while(counter < n)
{
    cout<<"ENTER THE EMPLOYEE ID:";
    cin>>employeeid;
    cout<<"ENTER THE HOURS WORKED:";
    cin>>hoursworked;
    cout<<"ENTER THE HOURLY RATE:";
    cin>>hourlyrate;
    grosspay=hoursworked*hourlyrate;
    taxamount=grosspay*TAXRATE;
    netpay=grosspay-taxamount;
    cout<<"THE HOURS WORKED ARE "<<hoursworked<<"\n";
    cout<<"THE GROSSPAY IS "<<grosspay<<"\n";
    cout<<"THE TAX AMOUNT IS "<<taxamount<<"\n";
    cout<<"THE NETPAY IS "<<netpay<<"\n";
    ++counter;
}//WHILE
cin.ignore();
cin.get();//same as sytem pause but takes less resources and better code.
return 0;
}//MAIN




what ide are you using most will tell you when you have undeclared & unused variables?
code::blocks is a good one to use.
Last edited on
I get it now. This is my first programming course so it took me a while to understand but it finally clicks. Thank you guys for helping
3B) Expand your Payroll program so that it repeats for as many employees are in the input file.

Data typed and saved under employee.in


Use:



#include
#include
using namespace std;
…….
ifstream fin("employee.in");
………
while(fin>>employeeid>>hoursworked>>hourlyrate){…….…..}//end loop

This is the next part of the problem. I'm gonna try and do it myself but feel free to contribute if you have any tips for me. Once this part is done my assignment is totally finished.
Topic archived. No new replies allowed.