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. -
-
#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.
4 2118
I copied your code and ran it and got "Thanks. Exiting Program"
The only modification I made was to change
to
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?
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
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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...
|
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
|
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
| |
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
|
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...
|
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,...
|
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...
|
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. ...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |