Hi, i just don't know why does this program is wrong,
the problem is if I choose 1 and 3 . after I choose choice no.1, I enter the employee id and name nothing happens after that, same as choice no.3 which I will enter the employee id to find.
I hope someone can help me and guide me to finish this program..thanks....

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<sstream>
#include<iomanip>

#include <cstring>

#include <time.h>
#include<cmath>

using namespace std;

void append();
void find();
void read();
void whattodo();
void loop();
void about();
void mainfind();

int main()
{


system("Color 00");

whattodo();
getch();
return 0;
}
void whattodo()
{

int choice;

loop:

char dateStr [9];
char timeStr [9];

cout<<"\n";
cout<<"\t\t\t\t- - PAYROLL - - "<<endl;


_strdate( dateStr);
printf( "\t\t\t\t\t\t\t\t %s \n", dateStr);
_strtime( timeStr );
printf( "\t\t\t\t\t\t\t\t %s \n", timeStr);

cout << "\n\n\n";

cout<<"\t\t\t\t ^-^ MENU ^-^";
cout<<"\n\n\nEnter your choice: "<<endl;
cout<<endl;
cout<<"1) Add Record"<<endl;
cout<<"2) View files"<<endl;
cout<<"3) Find a record"<<endl;
cout<<"4) Exit"<<endl;
cout<<""<<endl;
cin>>choice;
cout<<""<<endl;
if (choice==1)
{
system("cls");
append();
cout<<endl;
loop();
}
else if (choice==2)
{
system("cls");
read();
cout<<endl;
loop();
}
else if(choice==3)
{
system("cls");
mainfind();
loop();
}
else if(choice==4)
{
system("cls");
cout<<"Bye.";
}
else
{
cout<<"Invalid Input! Try again."<<endl;
goto loop;
}

system ("cls");
}

void append()
{ 
char month[10];
int days;
float mins=0,tmin=0,totalh=0,total=0,sum=0;
float overtime=0,otrate=0,otpay=0,s=0;
float deduct=0,drate=0;
float s1=0,s2=0,s3=0,st=800;
float a,b,h,m;
double salary=0.00; 
string id;
char dateStr [9];
char timeStr [9];


cout<<"\n";
cout<<"\t\t\t\t- - Payroll - -"<<endl;
cout<<"\n";

_strdate( dateStr);
printf( "\t\t\t\t\t\t\t\t %s \n", dateStr);
_strtime( timeStr );
printf( "\t\t\t\t\t\t\t\t %s \n", timeStr);

cout<<"\n\n\n";
cout<<"\nType Employee's I.D no.: ";
cin >> id;
cout<<"\nType Employee's name : ";
string nn,ff,f,n;
{
getline(cin,ff,' ');
stringstream(ff)>>f;
}
{
getline(cin,nn);
stringstream(nn)>>n;
}

cout<<"Month: ";
cin>>month;



cout<<"\nType No. of Days: ";
cin>>days;

cout<<"\n\n\n";

system("pause");
system("cls");

cout<<"\n";
cout<<"\n";
cout<<"\t\t\t\t- - Payroll - -"<<endl;
cout<<"\n";

_strdate( dateStr);
printf( "\t\t\t\t\t\t\t\t %s \n", dateStr);
_strtime( timeStr );
printf( "\t\t\t\t\t\t\t\t %s \n", timeStr);
cout<<"\n\n\t\t\t\t ";
cout<<month<<" 1"<<" to " <<days<<"\n\t";
for (int day=1;day<=days;day++)
{


cout<<month<<' '<<day;
cout<<"\n\n\tTime in: ";
{


string hh,mm;
getline (cin,hh,':');

stringstream(hh) >> h;
if(h>23)
{

cout<<"\nThe Maximum hours is 23";
break;


}

getline (cin,mm);

stringstream(mm) >> m;
if(m>59)
{
cout<<"\nthe maximum minute is 59.";
break;
}

}
cout<<"\n\tTimeout: ";
{
string hh,mm1;
getline (cin,hh,':');

stringstream(hh) >> b;
if(b>23)
{

cout<<"\nthe maximum hour is 23.";
break;


}

getline (cin,mm1);
stringstream(mm1) >> a;
if(m>59)
{
cout<<"\nthe maximum minute is 59.";
break;
}

}

cout<<"\n\t";

mins=a+m;
tmin=mins/60;
totalh=b-h;
total=totalh+tmin;

sum=sum+total;

if(total>8){

overtime=total-8;

otrate=overtime*50;
otpay=otrate+800;
s=total/8;

}
else if(total<8)
{

deduct=8-total;
drate=deduct*40;

}
s1=s*800;
s2=s1-drate;
s3=s2+otpay;
salary=s3+st;

}

cout<<"\n\tTotal Hours: ";
cout<<sum;
cout<<"\n\n";

system("pause");
system("cls");

cout<<"\n";
cout<<"\t\t\t\t- - Payroll - -"<<endl;
cout<<"\n";

_strdate( dateStr);
printf( "\t\t\t\t\t\t\t\t %s \n", dateStr);
_strtime( timeStr );
printf( "\t\t\t\t\t\t\t\t %s \n", timeStr);

cout<<"\n\n\nEmployee's I.D no. : ";
cout<<id;
cout<<"\nEmployee's Name : ";
cout<<f<<" "<<n;
cout<<"\nTotal Hours Work : ";
cout<<sum;
cout<<"\nOvertime Work : "<<overtime;
cout<<"\nOvertime Rate : "<<otrate;
cout<<"\nUndertime Worked : "<<deduct;
cout<<"\nUndertime Deduction : "<<drate;
cout<<"\nSalary : "<<salary;
ofstream fout;
fout.open("data.txt",ios::app);
fout<<"\n";
fout<<"\t\t\t\t- - Payroll - -"<<endl;
fout<<"\n";

fout<<"\t\t\t\t\t\t\t";
fout<< dateStr;
fout<<"\n\t\t\t\t\t\t\t";

fout<< timeStr;
fout<<"\t\t";

fout<<"Employee's I.D no. : "<<id<< "\n" 
<<"Employee's Name : "<< f<< " " << n << "\n" 
<<"Total Hours Worked : "<<sum<<"\n"
<<"Overtime Work : "<<overtime<<"\n"
<<"Overtime Pay : "<<otrate<<"\n"
<<"Undertime Worked : "<<deduct<<"\n"
<<"Undertime Deduction : "<<drate<<"\n"
<<"Salary : "<<salary;

fout.close();
fout.open("myRecord.txt",ios::app); // open for find

fout << setiosflags(ios::fixed | ios :: showpoint)
<< setprecision(2);
fout<<id<<","<<f<<" "<<n<<","<<sum<<","<<overtime<<","<<
otrate<<","<<deduct<<","<<drate<<","
<<","<<salary<<"," << endl;
fout << fixed;

fout.close( ); //close file
;
}

void read()
{
string line;
ifstream x ("data.txt");

if (x.is_open())
{
while(!x.eof())
{

cout<<endl;
getline(x,line);
cout<<line<<endl;

}
x.close();
}
else
cout<<"Can't open the file."<<endl;
}
void find()
{
ifstream data("myRecord.txt");
string item,line;
int x=0;
int y=0;
string id;

cout<<endl;
cout<<"Enter Employee Id: ";
cin>>id;
system("pause");
system("cls");
cout<<""<<endl;

while(!data.eof())
{
getline(data,line);
string item_array[10];
stringstream stream(line);
x=0,y=0;

while(getline(stream,item,','))
{
item_array[x]=item;
x++;
item_array[y]=item;
y++;
}

if(item_array[0]==id)
{
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Employee's ID no. : " <<right<<" "<<item_array[0]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Employee's Name : " <<right<<" "<<item_array[1]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Total Hours Worked : " <<right<<" "<<item_array[2]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Overtime Work : " <<right<<" "<<item_array[3]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Overtime Pay : " <<right<<" "<<item_array[4]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Undertime Work : " <<right<<" "<<item_array[5]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Undertime Deduction : " <<right<<" "<<item_array[6]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Salary : " <<right<<" "<<item_array[8]<<endl;

}
}
data.close();
}
void loop()
{
string choice;
cout<<""<<endl;
cout<<"Do you want to make another choice? [Y/N]: ";
cin>>choice;
system("cls");
cout<<""<<endl;
if (choice=="y"||"Y")
{
whattodo();
}
else if(choice=="n"||"N")
{
cout<<"Bye.";
}
}
void about()
{


cout<<""<<endl;
loop();
}

void mainfind()
{
find();

string id;
cout<<"\n\n\t - - Just type back to go to Menu. - -"<<endl;
cout<<"\n\n\t - - Press any key to find another Profile. - -"<<endl;
cin>>id;
system("cls");

if (id=="back")
{
whattodo();
}
else
{
mainfind();
find();

}
}

Bold Text Here

Hard to help when the program works.

The interface is a bit awkward in places, but it functioned for me. I entered month, number of days, then start and end time. It did some calculations and displayed the worker's info. Did this for two workers, it found both.

Some indentation would really help reading this code.

Edited 3 Years Ago by vmanes

Borther i have tests your code and identified some of problems
first let me give you working solution

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<sstream>
#include<iomanip>
#include <cstring>
#include <time.h>
#include<cmath>
using namespace std;
void append();
void find();
void read();
void whattodo();
void loop();
void about();
void mainfind();
int main()
{
system("Color 00");
whattodo();
getch();
return 0;
}
void whattodo()
{
int choice;
loop:
char dateStr [9];
char timeStr [9];
cout<<"\n";
cout<<"\t\t\t\t- - PAYROLL - - "<<endl;
_strdate( dateStr);
printf( "\t\t\t\t\t\t\t\t %s \n", dateStr);
_strtime( timeStr );
printf( "\t\t\t\t\t\t\t\t %s \n", timeStr);
cout << "\n\n\n";
cout<<"\t\t\t\t ^-^ MENU ^-^";
cout<<"\n\n\nEnter your choice: "<<endl;
cout<<endl;
cout<<"1) Add Record"<<endl;
cout<<"2) View files"<<endl;
cout<<"3) Find a record"<<endl;
cout<<"4) Exit"<<endl;
cout<<""<<endl;
cin>>choice;
cout<<""<<endl;
if (choice==1)
{
system("cls");
append();
cout<<endl;
loop();
}
else if (choice==2)
{
system("cls");
read();
cout<<endl;
loop();
}
else if(choice==3)
{
system("cls");
mainfind();
loop();
}
else if(choice==4)
{
system("cls");
cout<<"Bye.";
}
else
{
cout<<"Invalid Input! Try again."<<endl;
goto loop;
}
system ("cls");
}
void append()
{ 
char month[10];
int days;
float mins=0,tmin=0,totalh=0,total=0,sum=0;
float overtime=0,otrate=0,otpay=0,s=0;
float deduct=0,drate=0;
float s1=0,s2=0,s3=0,st=800;
float a,b,h,m;
double salary=0.00; 
string id;
char dateStr [9];
char timeStr [9];
cout<<"\n";
cout<<"\t\t\t\t- - Payroll - -"<<endl;
cout<<"\n";
_strdate( dateStr);
printf( "\t\t\t\t\t\t\t\t %s \n", dateStr);
_strtime( timeStr );
printf( "\t\t\t\t\t\t\t\t %s \n", timeStr);
cout<<"\n\n\n";
cout<<"\nType Employee's I.D no.: ";
cin >> id;
cout<<"\nType Employee's name : ";
string nn,ff,f,n;
char name[300];

cin.getline(name,300);
cout<<"Month: ";
cin>>month;
cout<<"\nType No. of Days: ";
cin>>days;
cout<<"\n\n\n";
//system("pause");
//system("cls");
cout<<"\n";
cout<<"\n";
cout<<"\t\t\t\t- - Payroll - -"<<endl;
cout<<"\n";
_strdate( dateStr);
printf( "\t\t\t\t\t\t\t\t %s \n", dateStr);
_strtime( timeStr );
printf( "\t\t\t\t\t\t\t\t %s \n", timeStr);
cout<<"\n\n\t\t\t\t ";
cout<<month<<" 1"<<" to " <<days<<"\n\t";
for (int day=1;day<=days;day++)
{
cout<<month<<' '<<day;
cout<<"\n\n\tTime in: ";
{
string hh,mm;
getline (cin,hh,':');
stringstream(hh) >> h;
if(h>23)
{
cout<<"\nThe Maximum hours is 23";
break;
}
getline (cin,mm);
stringstream(mm) >> m;
if(m>59)
{
cout<<"\nthe maximum minute is 59.";
break;
}
}
cout<<"\n\tTimeout: ";
{
string hh,mm1;
getline (cin,hh,':');
stringstream(hh) >> b;
if(b>23)
{
cout<<"\nthe maximum hour is 23.";
break;
}
getline (cin,mm1);
stringstream(mm1) >> a;
if(m>59)
{
cout<<"\nthe maximum minute is 59.";
break;
}
}
cout<<"\n\t";
mins=a+m;
tmin=mins/60;
totalh=b-h;
total=totalh+tmin;
sum=sum+total;
if(total>8){
overtime=total-8;
otrate=overtime*50;
otpay=otrate+800;
s=total/8;
}
else if(total<8)
{
deduct=8-total;
drate=deduct*40;
}
s1=s*800;
s2=s1-drate;
s3=s2+otpay;
salary=s3+st;
}
cout<<"\n\tTotal Hours: ";
cout<<sum;
cout<<"\n\n";
system("pause");
system("cls");
cout<<"\n";
cout<<"\t\t\t\t- - Payroll - -"<<endl;
cout<<"\n";
_strdate( dateStr);
printf( "\t\t\t\t\t\t\t\t %s \n", dateStr);
_strtime( timeStr );
printf( "\t\t\t\t\t\t\t\t %s \n", timeStr);
cout<<"\n\n\nEmployee's I.D no. : ";
cout<<id;
cout<<"\nEmployee's Name : ";
cout<<name;
cout<<"\nTotal Hours Work : ";
cout<<sum;
cout<<"\nOvertime Work : "<<overtime;
cout<<"\nOvertime Rate : "<<otrate;
cout<<"\nUndertime Worked : "<<deduct;
cout<<"\nUndertime Deduction : "<<drate;
cout<<"\nSalary : "<<salary;
ofstream fout;
fout.open("data.txt",ios::app);
fout<<"\n";
fout<<"\t\t\t\t- - Payroll - -"<<endl;
fout<<"\n";
fout<<"\t\t\t\t\t\t\t";
fout<< dateStr;
fout<<"\n\t\t\t\t\t\t\t";
fout<< timeStr;
fout<<"\t\t";
fout<<"Employee's I.D no. : "<<id<< "\n" 
<<"Employee's Name : "<< f<< " " << n << "\n" 
<<"Total Hours Worked : "<<sum<<"\n"
<<"Overtime Work : "<<overtime<<"\n"
<<"Overtime Pay : "<<otrate<<"\n"
<<"Undertime Worked : "<<deduct<<"\n"
<<"Undertime Deduction : "<<drate<<"\n"
<<"Salary : "<<salary;
fout.close();
fout.open("myRecord.txt",ios::app); // open for find
fout << setiosflags(ios::fixed | ios :: showpoint)
<< setprecision(2);
fout<<id<<","<<f<<" "<<n<<","<<sum<<","<<overtime<<","<<
otrate<<","<<deduct<<","<<drate<<","
<<","<<salary<<"," << endl;
fout << fixed;
fout.close( ); //close file
;
}
void read()
{
string line;
ifstream x ("data.txt");
if (x.is_open())
{
while(!x.eof())
{
cout<<endl;
getline(x,line);
cout<<line<<endl;
}
x.close();
}
else
cout<<"Can't open the file."<<endl;
}
void find()
{
ifstream data("myRecord.txt");
string item,line;
int x=0;
int y=0;
string id;
cout<<endl;
cout<<"Enter Employee Id: ";
cin>>id;

system("cls");
cout<<""<<endl;
while(!data.eof())
{
getline(data,line);
string item_array[10];
stringstream stream(line);
x=0,y=0;
while(getline(stream,item,','))
{
item_array[x]=item;
x++;
item_array[y]=item;
y++;
}
if(item_array[0]==id)
{
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Employee's ID no. : " <<right<<" "<<item_array[0]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Employee's Name : " <<right<<" "<<item_array[1]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Total Hours Worked : " <<right<<" "<<item_array[2]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Overtime Work : " <<right<<" "<<item_array[3]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Overtime Pay : " <<right<<" "<<item_array[4]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Undertime Work : " <<right<<" "<<item_array[5]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Undertime Deduction : " <<right<<" "<<item_array[6]<<endl;
cout<<""<<endl;
cout<<setfill(' ')<<left<<setw(10)<<"Salary : " <<right<<" "<<item_array[8]<<endl;
}
}
data.close();
}
void loop()
{
string choice;
cout<<""<<endl;
cout<<"Do you want to make another choice? [Y/N]: ";
cin>>choice;
system("cls");
cout<<""<<endl;
if (choice=="y"||"Y")
{
whattodo();
}
else if(choice=="n"||"N")
{
cout<<"Bye.";
}
}
void about()
{
cout<<""<<endl;
loop();
}
void mainfind()
{
find();
string id;
cout<<"\n\n\t - - Just type back to go to Menu. - -"<<endl;
cout<<"\n\n\t - - Press any key to find another Profile. - -"<<endl;
cin>>id;
system("cls");
if (id=="back")
{
whattodo();
}
else
{
mainfind();
find();
}
}

system("pause");
was causing some problems that is removed now its working fine
and also changes getline(cin,ff,' '); was acepting name until space is entered so that is why it was not moving forward hope it helps

  1. Don't use goto statements (bad, bad, bad! except in very RARE situations).
  2. Using recursion (calling your own function from within your function) can be very useful. Computing a Fibbonacci sequence is a good example of this.
  3. Don't list ALL of your code here (340+ LOC is counter-productive). Instead, just try to show the code that you are having problems with.

Edited 3 Years Ago by rubberman

This article has been dead for over six months. Start a new discussion instead.