-
Problem with while loop
I'm having a problem with my while loop, it won't stop prompting for input. I'm using a while loop and want to bail out when the user just presses 'enter' (\0). Perhaps somebody could give me a hint as to what I'm doing wrong?
Code:
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
struct Employee
{
char firstName[35];
char lastName[35];
char hireDate[11];
char SSN[12];
int Hours;
float payRate;
};
float PayCalc(int, float);
int main(void)
{
const int max_count(50);
Employee Emps[max_count];
int count(0);
while(Emps[count].firstName != '\0')
{
cout << "Employee's first name: ";
cin >> Emps[count].firstName;
cout << Emps[count].firstName << "'s last name is: ";
cin >> Emps[count].lastName;
cout << Emps[count].firstName << "'s hire date is: ";
cin >> Emps[count].hireDate;
cout << Emps[count].firstName << "'s social security number: ";
cin >> Emps[count].SSN;
cout << "Number of hours that " << Emps[count].firstName << " worked: ";
cin >> Emps[count].Hours;
cout << Emps[count].firstName << "'s pay rate: ";
cin >> Emps[count].payRate;
count++;
cin.ignore(100, '\n');
}
for(int a(0); a < count; a++)
{
cout << Emps[a].lastName << ", " << Emps[a].firstName << setw(20) << Emps[a].hireDate << setw(5);
cout << Emps[a].SSN << setw(5) << Emps[a].Hours << setw(5) << PayCalc(Emps[a].Hours, Emps[a].payRate) << endl;
}
cout << right << count << endl;
return 0;
}
float PayCalc(int a, float b)
{
return float(a) * b;
}
I hope that this looks right on the screen, it's my first time to post and I am trying to abide by the forum bylaws. Anyhow, I'm not looking for the actual code, I'd like to figure this out...However, if somebody could help me in seeing what I'm doing wrong then I would greatly appreciate it.
-
Code:
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
struct Employee
{
char firstName[35] = { ' ' };
char lastName[35] = { ' ' };
char hireDate[11] = { ' ' };
char SSN[12] = { ' ' };
int Hours;
float payRate;
};
float PayCalc(int, float);
int main(void)
{
const int max_count(50);
Employee Emps[max_count];
int count(0);
while(!isspace(Emps[count].firstName[0]))
{
cout << "Employee's first name: ";
cin >> Emps[count].firstName;
cout << Emps[count].firstName << "'s last name is: ";
cin >> Emps[count].lastName;
cout << Emps[count].firstName << "'s hire date is: ";
cin >> Emps[count].hireDate;
cout << Emps[count].firstName << "'s social security number: ";
cin >> Emps[count].SSN;
cout << "Number of hours that " << Emps[count].firstName << " worked: ";
cin >> Emps[count].Hours;
cout << Emps[count].firstName << "'s pay rate: ";
cin >> Emps[count].payRate;
count++;
cin.ignore(100, '\n');
}
for(int a(0); a < count; a++)
{
cout << Emps[a].lastName << ", " << Emps[a].firstName << setw(20) << Emps[a].hireDate << setw(5);
cout << Emps[a].SSN << setw(5) << Emps[a].Hours << setw(5) << PayCalc(Emps[a].Hours, Emps[a].payRate) << endl;
}
cout << right << count << endl;
return 0;
}
float PayCalc(int a, float b)
{
return float(a) * b;
}
That may work.
-
Unfortunately, that didn't work. I've used a character input to determine whether to continue through the loop. What I want to do is have a null character input terminate the loop. Perhaps a 'switch' statement? Thank you for the help though....
-
cin always waits for a character before the program continues so just inputting a null character wont work.
Just make it like this in the end of the loop:
Code:
char yesno[3]; //ill make it three so the prog wont bail out if the user does something wrong
cout << "\n\nContinue? (y or n) ";
cin >> yesno;
if(strcmp(yesno,"y")==0) {
exit(0);
}