String: White Spaces within a do while loop

I'm creating a program that calculates the income of employee/s. Whenever I try and input a name with a space, it turns into an infinite loop. How can I fix this? Try and run the program below.



#include<iostream>
#include<conio.h>
#include<windows.h>
#include<iomanip>
#include<string>
#include<ctime>
#include<fstream>
#include<cctype>
#include<cstring>
#include<cstdlib>

using namespace std;

void gotoxy(int x, int y)
{
COORD screen;
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
screen.X = x; screen.Y = y;
SetConsoleCursorPosition(output,screen);
}
void login(){
char option, choice;
char username[]="blabscore";//username to type
char password[]="1234567"; //password to type
char uname[20];
char pass[20];
bool account=false;
int cnt=0;

while(1){
system("cls");
cout<<"\t\t****************\n";
cout<<"\t\t*PAYROLL SYSTEM*\n";
printf("\t\t*USERNAME: ");
cin >> uname;

cout << "\t\t*PASSWORD: ";
int i=0;
while(1)
{
pass[i]=getch();
if(pass[i]==13)
{
pass[i]='\0';
break;
}
cout << "*";
i++;
}

if(!strcmp(uname,username) && !strcmp(pass,password)) {
account=true;
break;
}

else

{
cnt++;
if(cnt==3)
break;
cout << "\nIncorrect username or password\n";
cout << "No of try: "<<cnt <<endl;
system("pause>0");
}
}
if(account) {
cout << "\n\n\t\t*Access Granted\n";
} else
{
cout << "\n\n\t\t*Incorrect input 3 times\n";
}
}


int main(){
login();
char slct;
time_t rawtime;
time(&rawtime);
ofstream JAMES;
double min=(475.00);
double ratePhr=(59.38);
string empName;
int days;
int hours, hours2, hours3;
int num=1;

double regOT=ratePhr*1.25;
double SpecHOT=ratePhr*1.3;
double regHol=ratePhr*2.00;
int x=3;
system("cls");
cout<<"LOADING PLEASE WAIT!\n";
for (x; x>=0;x--){
cout<<".";
Sleep(1000);
}
system("cls");
system("color 4E");
do {
JAMES.open("z:\\payroll_record.txt", ios_base :: app);
cout<<"Company Name\n";
cout<<" Payroll \n";
cout<<"Employee Number: " <<num<< endl;
JAMES<<"Employee Number: "<< num<< endl;
cout<<"Name of Employee: ";
cin>>empName[45];
JAMES<<"Employee: "<<empName<<endl;
cout<<"Days Present: ";
cin>> days;
JAMES<<"Days Present: "<< days<< endl;
cout<<"Regular Overtime(per hour): ";
cin>>hours;
JAMES<<"Regular Overtime: "<<hours<<endl;
cout<<"Special Holiday Overtime(per hour): ";
cin>>hours2;
JAMES<<"Special Holiday Overtime(per hour): "<<hours2<<endl;
cout<<"Regular Holiday Overtime(per hour): ";
cin>>hours3;
JAMES<<"Regular Holiday Overtime(per hour): "<<hours3<<endl;
double totalPay=((days*min))+((regOT*hours))+((SpecHOT*hours2))+((regHol*hours3));
cout<<empName<<" total income: "<<totalPay<<endl;
cout<<"Date: " << ctime (&rawtime)<< endl;
JAMES<<"Date: "<< ctime (&rawtime)<< endl;
JAMES.close();
cout<<"Press Y to calculate another or N to exit: ";
cin>> slct;

if (slct == 'Y' || slct == 'y'){
}
main;
if (slct == 'N' || slct == 'n'){
exit (0);
}
num++;
} while (1);
system("pause>0");

}
Last edited on
Topic archived. No new replies allowed.