Hello everyone. I'm working on a program that crashes whenever I enter the value that is supposed to stop the program. The program takes in various pieces of info, has a struct and a class. The error says the stack around employee (which is the class variable) is corrupted. I cannot figure out the problem...and I think I need fresh eyes to look at it.
Posted is the entire program.
Thanks a ton,
J
Ps: sorry about all the commenting, I thought I was ready to submit the program to my instructor before I discovered the current problem.
Posted is the entire program.
Code:
#include <iostream>
using namespace std;
struct Person // creates a Person struct
{
char Fname[20],Lname[20]; // character arrays for first name and last name
float age; // age
int ssn[9]; // integer arrays for ssn and employee number
};
class Employee // employee class declaration
{
private: //reread instuctions!!!!!!!!!1
int enumber[9]; // integer arrays for ssn and employee number
float wage; // wage
char job_title[20]; // job title
Person Per; // declaration of Per as a Person
public:
Employee(); // employee constructor
~Employee(){}; // employee destructor
void SetFname(int emp);
void SetLname(int emp);
void Setage(int emp);
int Setssn(int emp);
void Setenumber(int emp);
void Setwage(int emp);
void Setjob_title(int emp);
void output();
float salary(float wage);
};
// Purpose: The employee constructor function sets all default values for the employee class
// Input: Takes no input
// Output: Outputs constructor to the screen to inform user that the default values have been set
Employee::Employee()
{ // begin Employee constructor function
int i=0,j=0;
cout<<"Constructor........"<<endl;
Per.age=0;
wage=0;
for (i=0;i<20;++i) // loop through arrays setting all values to 0 or to " "
{
Per.Fname[i]='-';
Per.Lname[i]='-';
job_title[i]=' ';
}
for (i=0;i<=9;++i)
{ // loop through int arrays setting all vaules to 0
enumber[i]=0;
Per.ssn[i]=0;
}
} // end Employee constructor function
float Employee::salary (float wage)
{
float sal=0, hours=0;
cout<<"Enter the number of hours worked."<<endl;
cin>>hours;
sal=(hours* wage);
return (sal);
}
// Purpose: Purpose is to set the age of the employee
// Input: Takes as input the employee to be edited from main
// Output: No output
void Employee::Setage(int emp) // Setage function belongs to Employee class
{ // begin function
cout<<"Enter the age of the employee."<<endl;
cin>>Per.age; // takes age input from user
} // end function
// Purpose: Purpose is to set the job title of the employee
// Input: Takes as input the employee to be edited from main
// Output: No output
void Employee::Setjob_title(int emp) // Setjob_title function belongs to Employee class
{ // begin function
cout<<"Enter the job title of the employee."<<endl;
cin.ignore();
cin.getline (job_title,20,'\n'); // takes string as input
} // end function
// Purpose: Purpose is to set the wage of the employee
// Input: Takes as input the employee to be edited from main
// Output: No output
void Employee::Setwage(int emp) // Setwage function belongs to Employee class
{ // begin function
float hours=0;
cout<<"Enter the wage of the employee."<<endl;
cin>>wage; // wage input from user
// cout<<"Enter the hours worked."<<endl;
// cin>>hours;
} // end function
// Purpose: Purpose is the set the first name of the employee
// Input: Takes as input the employee to be edited from main
// Output: No output
void Employee::SetFname(int emp) // SetFname function belongs to Employee class
{ // begin function
cout<<"Enter the first name of the employee."<<endl;
cin.ignore();
cin.getline (Per.Fname,20,'\n'); // takes string as input
} // end function
// Purpose: Purpose is to set the last name of the employee
// Input: Takes as input the employee to be edited from main
// Output: No output
void Employee::SetLname (int emp) // SetLname function belongs to Employee class
{ // begin function
cout<<"Enter the last name of the employee."<<endl;
cin.getline (Per.Lname,20,'\n'); // takes string as input
} // end function
// Purpose: Purpose is to set the ssn of the employee
// Input: Takes as input the employee to be edited from main
// Output: No output
int Employee::Setssn(int emp) // Setssn function belongs to Employee class
{ // begin function
cout<<"Enter the social security number of the employee."<<endl;
cin>>Per.ssn[emp]; // takes ssn as input
return (0); // returns 0
} // end function
// Purpose: Purpose is to set the enumber of the employee
// Input: Takes as input the employee to be edited from main
// Output: No output
void Employee::Setenumber(int emp) // Setenumber function belongs to Employee class
{ // begin function
int ans=0;
cout<<"Enter the employee number."<<endl;
cout<<"Would you like to use the employees ssn as their employee number? 1 for yes, 2 for no."<<endl; // ssn can be used as employee number
cin>>ans;
if (ans==1) // if 1 chosen ssn used as employee number
{
enumber[emp]=Per.ssn[emp]; // enumber=ssn
}
else
{
cout<<"Enter the employee number."<<endl;
cin>>enumber[emp]; // enumber taken as input
}
} // end function
// Purpose: Output information entered for the employee
// Input: No input
// Output: Outputs all information for the selected employee
void Employee::output() // output function belongs to the employee class
{ // begin function
int emp,i=0;
float tp;
cout<<"Which employee's information would you like to view?"<<endl;
cin>>emp; // employee selected
cout<<endl<<"The information entered for employee "<<emp<<" is:"<<endl;
cout<<"Last name: "<<Per.Lname<<endl; //Lname output
cout<<"First name: "<<Per.Fname<<endl; // Fname output
cout<<"Employee number: "<<enumber[emp]<<endl; // enumber output
cout<<"Employee ssn: "<<Per.ssn[emp]<<endl; // ssn output
cout<<"Employee wage: $"<<wage<<endl; // wage output
tp=salary(wage);
cout<<"Employee total pay $"<<tp<<endl;
cout<<"Employee age: "<<Per.age<<endl; // age output
cout<<"Employee job title: ";
for (i=0;i<20;++i)
{
cout<<job_title[i]; // job_title output
}
cout<<endl<<endl;
} // end function
void main()
{ // begin program execution
int emp=-1,choice=0;
char ans='y';
Employee employee[10]; // declares 10 employees
do
{
while(emp<0||emp>9)
{ // begin while loop
cout<<"Choose and employee 0-9"<<endl;
cin>>emp; // user's employee selection
} // end while loop
do
{ // begin do loop
cout<<"Please select what you would like to edit from the menu:"<<endl;
cout<<"1) Enter the first name."<<endl<<"2) Enter the last name."<<endl<<"3) Enter the social security number."<<endl;
cout<<"4) Enter the employee number."<<endl<<"5) Enter the employee's age."<<endl<<"6) Enter the employee's job title."<<endl<<"7) Enter the employees salary."<<endl;
cout<<"8) Enter all of the information."<<endl<<"9) Output the information for employee."<<endl<<"10) Exit Program."<<endl;
cin>>choice; // user's selection from the menu
switch(choice)
{ // begin switch/case
case 1: // if 1 selected
employee[emp].SetFname(emp); // calls setfname function of employee selected
break; // break loop
case 2: // if 2 selected
employee[emp].SetLname(emp); // calls setlname function of employee selected
break; // break loop
case 3: // if 3 selected
employee[emp].Setssn(emp); // calls setssn function of employee selected
break; // break loop
case 4: // if 4 selected
employee[emp].Setenumber(emp); // calls setenumber function for employee selected
break; // break loop
case 5: // if 5 selected
employee[emp].Setage(emp); // calls setage function
break; // break loop
case 6: // if 6 selected
employee[emp].Setjob_title(emp); // calls setjob_title function
break; // break loop
case 7: // if 7 selected
employee[emp].Setwage(emp); // calls setwage function
break; // break loop
case 8: // if 8 selected
cout<<"Please enter all of the employees information."<<endl;
employee[emp].SetFname(emp); // calls setfname function of employee selected
employee[emp].SetLname(emp); // calls setlname function of employee selected
employee[emp].Setssn(emp); // calls setssn function of employee selected
employee[emp].Setenumber(emp); // calls setenumber function of employee selected
employee[emp].Setage(emp); // calls setage function of employee selected
employee[emp].Setjob_title(emp); // calls setjob_title function of employee selected
employee[emp].Setwage(emp); // calls setwage fucntion of employee selected
break; // break loop
case 9: // if 9 selected
cout<<"The employees information is: "<<endl;
employee[emp].output(); // calls output function
break; // break loop
case 10:
cout<<"Thanks. Exiting program."<<endl;
break;
default:
cout<<"Enter a number between 1-10"<<endl<<endl;
break;
} // end switch/case
} // end do loop
while (choice<1||choice>10);//&&choice!=10); // do loop condition
}
while (choice!=10);
} // end do loop
Thanks a ton,
J
Ps: sorry about all the commenting, I thought I was ready to submit the program to my instructor before I discovered the current problem.
Comment