What is wrong with my while loop?

I have this project do tonight. I cannot figure out what is wrong with my program. My first prompt tells me to enter "ID" and when I do the while loops ends and my program ends? Can someone help me?

Here is my program



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



//Cosimo Vilardo assignment #2
//Program to compute information about employee's
#include <iostream>
using namespace std;
int main()
{
double pay, hours, rate, week_pay, over_time, bonus, net_pay;
int id, man_stat, number_of_employees=0;



cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);

cout<<"Enter employee ID >> ";
cin>>id;

while(id==-1){
number_of_employees++;


cout<<"Enter employee hours worked >> ";
cin>>hours;
cout<<"Enter employee rate of pay >> ";
cin>>rate;
cout<<"Enter 1 for Manager or 0 for employee >> ";
cin>>man_stat;

if(hours<=40)
week_pay=hours*rate;
else{
over_time=hours-40;
week_pay=(40*rate)+over_time*rate;
}

if(man_stat=1)
bonus=week_pay*.10;
else
bonus=week_pay+25;

net_pay=week_pay+bonus;

cout<<"employee "<<id<<" worked "<<hours<<" hours at $"
<<rate<<" per hour ";

if(man_stat=1)
cout<<"manager"<<endl;
else
cout<<"not a manager"<<endl;

cout<<"weekly pay $"<<week_pay<<" bonus $"<<bonus<<" net pay $"<<net_pay<<endl<<endl;

cout<<"Enter employee ID >> ";
cin>>id;
}

cout<<"We processed "<<number_of_employees<<" employees"<<endl;

system("pause");
return 0;
}
Last edited on
I think you meant to use != instead of == in your while loop. The way you have it now, your loop will never run unless the employee ID is -1. Remember: a while loop runs as long as the condition remains true.

-Albatross
Last edited on
thanks a lot! that makes it work now. One more problem... no matter what i type in this part of the program i always get the out put "manager"

here it is

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


if(man_stat=1)
cout<<"manager"<<endl;
else
cout<<"not a manager"<<endl;
you're setting man_stat to 1, and that's why it's always outputing "manager." I would assume that you would want to check it to see if it's one not to set it equal to it.

So what you want is:
man_stat == 1
weird?! its still not working
Huh, that's weird. I don't really know what's wrong with it then. But perhaps you could put curly braces around it like this.

1
2
3
4
5
6
7
8
if (man_stat == 1)
{
   std::count<< "manager" << endl;
}
else
{
   std::cout<< "not manager" << endl;
}
still not working. it could be a bug with DEVc++
Did you fix the = to == problem on both of your if statements? I tested it just now and it appears to be working. Could you show us your current code and input/output?
i got the whole program to work! thanks guys/gals!
here is the code for those who are curious




//Program to compute information about employee's
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
double pay, hours, rate, week_pay, over_time, bonus, net_pay;
int id=0, man_stat, number_of_employees=0;
ofstream myfile("screenOutput.txt"); //file declaration

cout.setf(ios::fixed,ios::floatfield);
cout.precision(2);

cout<<"Enter employee ID >> ";
cin>>id;

while(id!=-1){ //enter -1 to end program
number_of_employees++;


cout<<"Enter employee hours worked >> ";
cin>>hours;
cout<<"Enter employee rate of pay >> ";
cin>>rate;
cout<<"Enter 1 for manager or 0 for employee >> ";
cin>>man_stat;

myfile<<"employee "<<id<<" worked "<<hours<<" hours at $"
<<rate<<" per hour ";

if(man_stat==1) //prints out manager status
myfile<<" manager"<<endl;
else
myfile<<" not a manager"<<endl;

if(hours<=40) //Computes weekly pay and over time hours
week_pay=hours*rate; //for each employee
else{
over_time=hours-40;
week_pay=(40*rate)+over_time*1.5*rate;
}

if(man_stat==1) //Computes the bonus for each employee
bonus=week_pay*.10;
else
bonus=25;

net_pay=week_pay+bonus; //Computes netpay. Weekly pay x bonus

myfile<<"weekly pay $"<<week_pay<<" bonus $"<<bonus<<" net pay $"
<<net_pay<<endl<<endl;

cout<<"Enter employee ID >> ";
cin>>id;
}

myfile<<"We processed "<<number_of_employees<<" employees"<<endl;

myfile.close();
system("pause");
return 0;
}

Forgot to mention this before, but in the future, please use [code][/code] tags to format your code.
Topic archived. No new replies allowed.