Corrupted stack error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hypnotik
    New Member
    • Jun 2007
    • 87

    #1

    Corrupted stack error

    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.

    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.
  • mac11
    Contributor
    • Apr 2007
    • 256

    #2
    I copied your code and ran it and got "Thanks. Exiting Program"
    The only modification I made was to change
    Code:
    void main()
    to
    Code:
    int main()
    since my compiler makes main return an int (I use g++ on Linux) - just a compiler issue (not a fix)

    All I did was start the program, pick an employee (I used 3) then exit (10[enter]). Can you tell me what sequence of inputs you use to produce the error?

    Comment

    • mac11
      Contributor
      • Apr 2007
      • 256

      #3
      I poked more...

      It looks like your enumber and ssn are screwing you up. You don't need to use arrays for these. I can't tell why you decided to make these arrays, but think about how the memory behind the variables works... I think you got confused about how numbers are stored vs how characters are stored

      if you wanted to store the ssn (123456789 for example) as characters it would look like this in memory
      ['1']['2']['3']['4']['5']['6']['7']['8']['9']
      where '1' is the ascii value for the character '1' NOT the value 1 (the value of '1' is 49 in decimal)

      but as an int the memory is just the binary representation of the number 123456789

      Comment

      • Hypnotik
        New Member
        • Jun 2007
        • 87

        #4
        Hey mac, thanks for posting. The reason that those are arrays is because in the lab assignment I was told to make them an array. I thought it was a little strange too...given I was using numbers and not letters, butI went with it. I also posted this on another forum...and someone else mentioned what you did, almost identically.

        I'm going to try to make those changes and see what happens. I guess I can't get anymore points off for "making" the program work....then submitting it and it not working.


        Thanks for taking a look. I'll post the results when I'm done making the changes to let you know if it works.


        Thanks again,
        J

        Comment

        • Hypnotik
          New Member
          • Jun 2007
          • 87

          #5
          It works! As I said I can't see how I would be penalized for making the program work but we'll see. This program was nothing but a headache from the time I started it, and I'm glad it's done.


          Thanks again to all those who took a look, and to mac for making the final suggestion, I really appreciate it.


          J

          Comment

          Working...