Making some sort of intelegent table :D
Mar 4, 2016 at 5:15pm UTC
Hello guys,
I have written a code to get information of 10 employees including
First Name,Last Name,National Code,Starting to work Time and finishing time
it works properly , but I need to organize them in a table, it's quiet complicated for me because I've made a class for Time and another class for all information of employee,which includes two objects from "time" class
like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class time
{
private :
unsigned hh,mm,ss;
public :
void timeInput();
void timeOutput();
};
class employee
{
private :
string fName,lName,nCode; // nCode= National Code
time s,f; //Starting time and finishing time
.
.
.
};
Later when I was trying to COUT those two functions of my time class I realised I can't COUT functions so I solved it in another way but now I'm trying to make a table and I can't use "setw()" to organize functions in my table
I tried to show data with this but those 2 functions between "cout"s in line 5 and line 7 are making it hard and they don't have same width
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
void employee::allData()
{
cout<<"|" <<setw(15)<<left<<fName<<"|" <<setw(15)<<left<<lName
<<"|" <<setw(15)<<left<<nCode<<"|" ;
s.timeOutput();
cout<<"|" ;
f.timeOutput();
cout<<"|" <<endl;
}
int main ()
{
employee e[10];
for (int i=0;i<1;i++)
{...}
for (int i=0;i<1;i++)
{
e[i].allData();
}
cin.ignore();
cin.get();
return 0;
}
Last edited on Mar 4, 2016 at 5:28pm UTC
Mar 4, 2016 at 6:05pm UTC
You haven't shown your code for your timeOutput function. That would be the correct place to set the width of the individual time fields.
You can cout your time object, but you must override the << operator.
In time.h after line 7:
friend ostream & operator << (ostream & os, const time & t);
In time.cpp:
1 2 3 4
ostream & time::operator << (ostream & os, const time & t)
{ os << setw(2) << t.hh << ":" << t.mm << ":" << t.ss;
return os;
}
Mar 4, 2016 at 6:40pm UTC
thank you @AbstractionAnon but It causes some errors and I have no idea about them
I thought it would be messy to show those codes but here is my whole codes if you want
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
class time
{
private :
unsigned hh,mm,ss;
public :
void timeInput();
void timeOutput();
};
void time::timeInput()
{
cout<<"Hour:" ;
cin>>hh;
while (hh>=24)
{
cout<<"invalid \"Hour\" number!Put a valid number:" ;
cin>>hh;
}
cout<<"Minute:" ;
cin>>mm;
while (mm>=60)
{
cout<<"invalid \"minute\" number!Put a valid number:" ;
cin>>mm;
}
cout<<"Second:" ;
cin>>ss;
while (ss>=60)
{
cout<<"invalid \"second\" number!Put a valid number:" ;
cin>>ss;
}
}
void time::timeOutput()
{
cout<<"\t" <<hh<<":" <<mm<<":" <<ss;
}
class employee
{
private :
string fName,lName,nCode; // nCode= National Code
time s,f; //Starting time and finishing time
public :
void information();
void data();
void allData();
};
void employee :: information ()
{
cout << "Enter employee's First Name: " ;
getline(cin,fName);
cout << "Enter employee's last Name: " ;
getline(cin,lName);
cout<<"Enter the time employee started to work at: " <<endl;
s.timeInput();
cout<<"Enter the time employee finished work at: " <<endl;
f.timeInput();
cout<<"Enter the employee's National Code: " ;
cin>>nCode;
}
void employee::data()
{
cout<<"\nEmployee: \t\t" <<fName<<"-" <<lName<<endl;
cout<<"\nNational Code: \t\t" <<nCode<<endl;
cout<<"\nStarted to work at: " ;
s.timeOutput();
cout<<"\n\nFinished work at: " ;
f.timeOutput();
}
void employee::allData()
{
cout<<"|" <<setw(15)<<left<<fName<<"|" <<setw(15)<<left<<lName
<<"|" <<setw(15)<<left<<nCode<<"|" <<s.timeOutput();
cout<<"|" ;
f.timeOutput();
cout<<"|" <<endl;
}
int main ()
{
employee e[10];
for (int i=0;i<1;i++)
{
cout<<"----------------->Employee #" <<i+1<<"<-----------------" <<endl;
e[i].information();
system("cls" );
cout<<"----------------->Employee #" <<i+1<<"<-----------------" <<endl;
e[i].data();
cout<<endl;
system("pause" );
system("cls" );
}
for (int i=0;i<1;i++)
{
e[i].allData();
}
cin.ignore();
cin.get();
return 0;
}
Mar 4, 2016 at 7:01pm UTC
Line 77: timeOutput() is a void function. You can't cout a void.
If you implement the overload as I suggested, then you can cout a time object:
1 2
//line 77
<<"|" <<setw(15)<<left<<nCode<<"|" <<s // <- cout the time object
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
class time
{
private :
unsigned hh,mm,ss;
public :
void timeInput();
friend ostream & operator << (ostream & os, const time & t);
};
void time::timeInput()
{
cout<<"Hour:" ;
cin>>hh;
while (hh>=24)
{
cout<<"invalid \"Hour\" number!Put a valid number:" ;
cin>>hh;
}
cout<<"Minute:" ;
cin>>mm;
while (mm>=60)
{
cout<<"invalid \"minute\" number!Put a valid number:" ;
cin>>mm;
}
cout<<"Second:" ;
cin>>ss;
while (ss>=60)
{
cout<<"invalid \"second\" number!Put a valid number:" ;
cin>>ss;
}
}
ostream & time::operator << (ostream & os, const time & t)
{ os << setw(2) << t.hh << ":" << t.mm << ":" << t.ss;
return os;
}
class employee
{
private :
string fName,lName,nCode; // nCode= National Code
time s,f; //Starting time and finishing time
public :
void information();
void data();
void allData();
};
void employee :: information ()
{
cout << "Enter employee's First Name: " ;
getline(cin,fName);
cout << "Enter employee's last Name: " ;
getline(cin,lName);
cout<<"Enter the time employee started to work at: " <<endl;
s.timeInput();
cout<<"Enter the time employee finished work at: " <<endl;
f.timeInput();
cout<<"Enter the employee's National Code: " ;
cin>>nCode;
}
void employee::data()
{
cout<<"\nEmployee: \t\t" <<fName<<"-" <<lName<<endl;
cout<<"\nNational Code: \t\t" <<nCode<<endl;
cout<<"\nStarted to work at: " << s;
cout<<"\n\nFinished work at: " << f;
}
void employee::allData()
{
cout<<"|" <<setw(15)<<left<<fName<<"|" <<setw(15)<<left<<lName
<<"|" <<setw(15)<<left<<nCode<<"|" ;
cout << s;
cout << "|" ;
cout << f;
cout<<"|" <<endl;
}
int main ()
{
employee e[10];
for (int i=0;i<1;i++)
{
cout<<"----------------->Employee #" <<i+1<<"<-----------------" <<endl;
e[i].information();
system("cls" );
cout<<"----------------->Employee #" <<i+1<<"<-----------------" <<endl;
e[i].data();
cout<<endl;
system("pause" );
system("cls" );
}
for (int i=0;i<1;i++)
{
e[i].allData();
}
cin.ignore();
cin.get();
return 0;
}
Note: I removed timeOutput() since the overloaded operator >> replaces it.
See lines 13 and 40-43.
Last edited on Mar 4, 2016 at 7:12pm UTC
Topic archived. No new replies allowed.