473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Corrupted stack error

87 New Member
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 2118
mac11
256 Contributor
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 Contributor
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
Hypnotik
87 New Member
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
Hypnotik
87 New Member
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
1790
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 gve the following error for VIEWSTATE. The internal network is protected by a Firewall. My suspicion is that
2
525
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 message. Internally or when we test the site from home we never receive this error. What could be causing this error? How to over come this issue? Our...
2
783
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 two days out of 100's of hits. The error just started yesterday and this app has been running for quite some time without this error and has not...
5
2082
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 gve the following error for VIEWSTATE. The internal network is protected by a Firewall. My suspicion is that
1
5637
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' received. Description: Aborted.) 15 __kernel_vsyscall() 0xb7f25402 14 raise() 0x00646118 13 abort() 0x00647888
3
8696
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: System.Convert.FromBase64String(String s) +0
4
2684
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 I going wrong? (I'm using visual C++) #include <iostream> // Needed for cin, cout, cerr, endl #include <cmath> // Needed for Sin/Cos...
0
9106
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 2.0/ IIS 6/Windows 2003 in a Web Farm environment. I've set up a machineKey in root web configuration file that I'm sharing across all machines,...
7
2548
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 xml files, but it is based on asap2 standard). i created the parser using bison and flex. then ported the code to vc+ + environment including the...
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.