473,378 Members | 1,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. struct Person                                                        // creates a Person struct
  5. {
  6.     char Fname[20],Lname[20];                                        // character arrays for first name and last name
  7.     float age;                                                        // age
  8.     int ssn[9];                                                        // integer arrays for ssn and employee number
  9. };    
  10. class Employee                                                        // employee class declaration
  11. {
  12. private:                                                            //reread instuctions!!!!!!!!!1
  13.     int enumber[9];                                                    // integer arrays for ssn and employee number
  14.     float wage;                                                        // wage
  15.     char job_title[20];                                                // job title
  16.     Person Per;                                                        // declaration of Per as a Person
  17. public:
  18.     Employee();                                                        // employee constructor
  19.     ~Employee(){};                                                    // employee destructor
  20.     void SetFname(int emp);                                            
  21.     void SetLname(int emp);
  22.     void Setage(int emp);
  23.     int Setssn(int emp);
  24.     void Setenumber(int emp);
  25.     void Setwage(int emp);
  26.     void Setjob_title(int emp);
  27.     void output();
  28.     float salary(float wage);
  29. };
  30. // Purpose:  The employee constructor function sets all default values for the employee class
  31. // Input:     Takes no input
  32. // Output:     Outputs constructor to the screen to inform user that the default values have been set
  33. Employee::Employee()
  34. {                                                    // begin Employee constructor function
  35.     int i=0,j=0;
  36.     cout<<"Constructor........"<<endl;
  37.     Per.age=0;
  38.     wage=0;
  39.     for (i=0;i<20;++i)                                // loop through arrays setting all values to 0 or to " "
  40.     {
  41.         Per.Fname[i]='-';
  42.         Per.Lname[i]='-';
  43.         job_title[i]=' ';
  44.     }
  45.     for (i=0;i<=9;++i)
  46.     {                                                // loop through int arrays setting all vaules to 0
  47.         enumber[i]=0;
  48.         Per.ssn[i]=0;
  49.     }
  50.  
  51. }                                                    // end Employee constructor function
  52. float Employee::salary (float wage)
  53. {
  54.     float sal=0, hours=0;
  55.     cout<<"Enter the number of hours worked."<<endl;
  56.     cin>>hours;
  57.     sal=(hours* wage);
  58.     return (sal);
  59. }
  60.  
  61. // Purpose:  Purpose is to set the age of the employee
  62. // Input:     Takes as input the employee to be edited from main
  63. // Output:     No output
  64. void Employee::Setage(int emp)                        // Setage function belongs to Employee class
  65. {                                                    // begin function
  66.     cout<<"Enter the age of the employee."<<endl;
  67.     cin>>Per.age;                                    // takes age input from user
  68. }                                                    // end function
  69. // Purpose:     Purpose is to set the job title of the employee
  70. // Input:     Takes as input the employee to be edited from main
  71. // Output:   No output
  72. void Employee::Setjob_title(int emp)                // Setjob_title function belongs to Employee class
  73. {                                                    // begin function
  74.     cout<<"Enter the job title of the employee."<<endl;
  75.     cin.ignore();
  76.     cin.getline (job_title,20,'\n');                // takes string as input
  77. }                                                    // end function
  78. // Purpose:  Purpose is to set the wage of the employee
  79. // Input:     Takes as input the employee to be edited from main
  80. // Output:     No output
  81. void Employee::Setwage(int emp)                        // Setwage function belongs to Employee class
  82. {                                                    // begin function
  83.     float hours=0;
  84.     cout<<"Enter the wage of the employee."<<endl;
  85.     cin>>wage;                                        // wage input from user
  86.  
  87. //    cout<<"Enter the hours worked."<<endl;
  88. //    cin>>hours;
  89. }                                                    // end function
  90. // Purpose:     Purpose is the set the first name of the employee
  91. // Input:     Takes as input the employee to be edited from main
  92. // Output:     No output
  93. void Employee::SetFname(int emp)                    // SetFname function belongs to Employee class
  94. {                                                    // begin function
  95.     cout<<"Enter the first name of the employee."<<endl;
  96.     cin.ignore();
  97.     cin.getline (Per.Fname,20,'\n');                // takes string as input
  98. }                                                    // end function
  99. // Purpose:     Purpose is to set the last name of the employee
  100. // Input:     Takes as input the employee to be edited from main
  101. // Output:   No output
  102. void Employee::SetLname (int emp)                    // SetLname function belongs to Employee class
  103. {                                                    // begin function
  104.     cout<<"Enter the last name of the employee."<<endl;
  105.     cin.getline (Per.Lname,20,'\n');                // takes string as input    
  106. }                                                    // end function
  107. // Purpose:     Purpose is to set the ssn of the employee
  108. // Input:     Takes as input the employee to be edited from main
  109. // Output:     No output
  110. int Employee::Setssn(int emp)                        // Setssn function belongs to Employee class
  111. {                                                    // begin function
  112.     cout<<"Enter the social security number of the employee."<<endl;
  113.     cin>>Per.ssn[emp];                                // takes ssn as input
  114.     return (0);                                        // returns 0
  115. }                                                    // end function
  116. // Purpose:     Purpose is to set the enumber of the employee
  117. // Input:     Takes as input the employee to be edited from main
  118. // Output:     No output
  119. void Employee::Setenumber(int emp)                    // Setenumber function belongs to Employee class
  120. {                                                    // begin function
  121.     int ans=0;
  122.     cout<<"Enter the employee number."<<endl;
  123.     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
  124.     cin>>ans;
  125.     if (ans==1)                                        // if 1 chosen ssn used as employee number
  126.     {
  127.         enumber[emp]=Per.ssn[emp];                    // enumber=ssn
  128.     }
  129.     else
  130.     {
  131.         cout<<"Enter the employee number."<<endl;
  132.         cin>>enumber[emp];                            // enumber taken as input
  133.     }
  134. }                                                    // end function
  135. // Purpose:     Output information entered for the employee
  136. // Input:     No input
  137. // Output:     Outputs all information for the selected employee
  138. void Employee::output()                                // output function belongs to the employee class
  139. {                                                    // begin function
  140.     int emp,i=0;
  141.     float tp;
  142.     cout<<"Which employee's information would you like to view?"<<endl;
  143.     cin>>emp;                                        // employee selected
  144.     cout<<endl<<"The information entered for employee "<<emp<<" is:"<<endl;
  145.     cout<<"Last name:          "<<Per.Lname<<endl;        //Lname output
  146.     cout<<"First name:         "<<Per.Fname<<endl;        // Fname output
  147.     cout<<"Employee number:    "<<enumber[emp]<<endl;    // enumber output
  148.     cout<<"Employee ssn:       "<<Per.ssn[emp]<<endl;    // ssn output
  149.     cout<<"Employee wage:     $"<<wage<<endl;            // wage output
  150.     tp=salary(wage);
  151.     cout<<"Employee total pay $"<<tp<<endl;
  152.     cout<<"Employee age:        "<<Per.age<<endl;        // age output
  153.     cout<<"Employee job title: ";                        
  154.     for (i=0;i<20;++i)
  155.     {
  156.     cout<<job_title[i];                                    // job_title output
  157.     }
  158.     cout<<endl<<endl;
  159. }                                                        // end function
  160. void main()
  161. {                                                                            // begin program execution
  162.     int emp=-1,choice=0;
  163.     char ans='y';
  164.     Employee employee[10];                                                        // declares 10 employees
  165.     do
  166.     {
  167.         while(emp<0||emp>9)
  168.         {                                                                        // begin while loop
  169.         cout<<"Choose and employee 0-9"<<endl;
  170.         cin>>emp;                                                                // user's employee selection
  171.         }                                                                        // end while loop
  172.         do
  173.         {                                                                        // begin do loop    
  174.         cout<<"Please select what you would like to edit from the menu:"<<endl;
  175.         cout<<"1) Enter the first name."<<endl<<"2) Enter the last name."<<endl<<"3) Enter the social security number."<<endl;
  176.         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;
  177.         cout<<"8) Enter all of the information."<<endl<<"9) Output the information for employee."<<endl<<"10) Exit Program."<<endl;
  178.         cin>>choice;                                                            // user's selection from the menu
  179.             switch(choice)
  180.             {                                                                    // begin switch/case
  181.             case 1:                                                                // if 1 selected
  182.                 employee[emp].SetFname(emp);                                        // calls setfname function of employee selected
  183.                 break;                                                            // break loop
  184.  
  185.             case 2:                                                                // if 2 selected
  186.                 employee[emp].SetLname(emp);                                        // calls setlname function of employee selected
  187.                 break;                                                            // break loop
  188.  
  189.             case 3:                                                                // if 3 selected
  190.                 employee[emp].Setssn(emp);                                            // calls setssn function of employee selected
  191.                 break;                                                            // break loop
  192.  
  193.             case 4:                                                                // if 4 selected
  194.                 employee[emp].Setenumber(emp);                                        // calls setenumber function for employee selected
  195.                 break;                                                            // break loop
  196.  
  197.             case 5:                                                                // if 5 selected
  198.                 employee[emp].Setage(emp);                                            // calls setage function
  199.                 break;                                                            // break loop
  200.  
  201.             case 6:                                                                // if 6 selected
  202.                 employee[emp].Setjob_title(emp);                                    // calls setjob_title function
  203.                 break;                                                            // break loop
  204.  
  205.             case 7:                                                                // if 7 selected
  206.                 employee[emp].Setwage(emp);                                            // calls setwage function
  207.                 break;                                                            // break loop
  208.  
  209.             case 8:                                                                // if 8 selected
  210.                 cout<<"Please enter all of the employees information."<<endl;    
  211.                 employee[emp].SetFname(emp);                                        // calls setfname function of employee selected
  212.                 employee[emp].SetLname(emp);                                        // calls setlname function of employee selected
  213.                 employee[emp].Setssn(emp);                                            // calls setssn function of employee selected
  214.                 employee[emp].Setenumber(emp);                                        // calls setenumber function of employee selected
  215.                 employee[emp].Setage(emp);                                            // calls setage function of employee selected
  216.                 employee[emp].Setjob_title(emp);                                    // calls setjob_title function of employee selected
  217.                 employee[emp].Setwage(emp);                                            // calls setwage fucntion of employee selected
  218.                 break;                                                            // break loop
  219.  
  220.             case 9:                                                                // if 9 selected
  221.                 cout<<"The employees information is: "<<endl;                    
  222.                 employee[emp].output();                                                // calls output function
  223.                 break;                                                            // break loop
  224.  
  225.             case 10:
  226.                 cout<<"Thanks. Exiting program."<<endl;
  227.                 break;
  228.  
  229.             default:
  230.                 cout<<"Enter a number between 1-10"<<endl<<endl;
  231.                 break;
  232.             }                                                                    // end switch/case
  233.         }                                                                        // end do loop
  234.         while (choice<1||choice>10);//&&choice!=10);                                // do loop condition
  235.     }
  236.     while (choice!=10);
  237.     }                                                                    // end do loop    
  238.  
  239.  
  240.  

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.
Jul 5 '07 #1
4 2104
mac11
256 100+
I copied your code and ran it and got "Thanks. Exiting Program"
The only modification I made was to change
Expand|Select|Wrap|Line Numbers
  1. void main()
to
Expand|Select|Wrap|Line Numbers
  1. 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?
Jul 5 '07 #2
mac11
256 100+
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
Jul 5 '07 #3
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
Jul 5 '07 #4
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
Jul 5 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Anup Jishnu | last post by:
Hi, I have installed ASP.Net application on a system. When accessing the Application from within the LAN, it works fine. However, when I access the application from the Internet, some pages...
2
by: .NET Developer | last post by:
On our external customer facing website recently we included a global error catching routine and forward the error message to our support team via email. We notice many emails with the following...
2
by: Brad | last post by:
I have an intranet app that has just started sporadically getting the following error "The viewstate is invalid for this page and might be corrupted." By sproadic I mean 3-4 times during the past...
5
by: Anup Jishnu | last post by:
Hi, I have installed ASP.Net application on a system. When accessing the Application from within the LAN, it works fine. However, when I access the application from the Internet, some pages...
1
by: Plissken.s | last post by:
Hi I have a problem which result in a "corrupted double-linked list error", I would need some help in trouble shot this one: Here is a stack track: Thread (Suspended: Signal 'SIGABRT'...
3
by: Nathan Sokalski | last post by:
I am recieving the following error on the second postback of a page I have written: The state information is invalid for this page and might be corrupted Stack Trace: ...
4
by: catherineod | last post by:
Hi, could anyone help me with this problem? I'm getting the following error message when I try to run this code - "Run-Time Check Failure #2 - Stack around the variable 'vY' was corrupted" Where am...
0
by: mwmorrison93 | last post by:
Hello, I'm at my wit's end regarding the "The state information is invalid for this page and might be corrupted" error and I'm hoping someone can provide some assistance. I working with C#/ASP.NET...
7
by: jc | last post by:
i'm developing a project using vc++. the main exe is a win32 application. it needs two dlls. one is my own implementation of string operations. the other dll is to parse a2l files(it is similar to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.