Program Closes Unexpectedly
May 8, 2013 at 3:35pm UTC
When this programs runs, it asks for the supervisor's name then closes right after the input. Any help would be appreciated.
Employee.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
class Employee
{
private :
string employeeName;
int employeeNumber;
string hireDate;
public :
Employee()
{
employeeName = "" ;
employeeNumber = 0;
hireDate = "" ;
}
Employee (string eName, int eNumber, string hDate)
{
employeeName = eName;
employeeNumber = eNumber;
hireDate = hDate;
}
void setEmployeeName (string eName)
{
employeeName = eName;
}
void setEmployeeNumber (int eNumber)
{
employeeNumber = eNumber;
}
void setHireDate (string hDate)
{
hireDate = hDate;
}
string getEmployeeName()
{
return employeeName;
}
int getEmployeeNumber()
{
return employeeNumber;
}
string getHireDate()
{
return hireDate;
}
};
#endif
ShiftSupervisor.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#ifndef SHIFTSUPERVISOR_H
#define SHIFTSUPERVISOR_H
#include "Employee.h"
#include <string>
class ShiftSupervisor : public Employee
{
private :
int salary;
int bonus;
public :
ShiftSupervisor() : Employee()
{
salary = 0;
bonus = 0;
}
ShiftSupervisor (string eName, int eNumber, string hDate,
int empSalary, int empBonus) : Employee(eName, eNumber, hDate)
{
salary = empSalary;
bonus = empBonus;
}
void setSalary(int empSalary)
{
salary = empSalary;
}
void setBonus(int empBonus)
{
bonus = empBonus;
}
int getSalary()
{
return salary;
}
int getBonus()
{
return bonus;
}
};
#endif
ShiftSupervisorDemo.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
#include <string>
#include <iomanip>
#include "ShiftSupervisor.h"
using namespace std;
char again;
int main()
{
do
{
ShiftSupervisor shiftSupervisor;
string supervisorName;
int supervisorNumber;
string supervisorHireDate;
int supervisorSalary;
int supervisorBonus;
cout << "Shift Supervisor Class" << endl;
cout << "-----------------------" << endl;
cout << "What is the supervisor's name? " << endl;
cin >> supervisorName;
cout << "What is the supervisor's number? " ;
cin >> supervisorNumber;
cout << "When was the supervisor hired? " ;
cin >> supervisorHireDate;
cout << "What is the supervisor's salary? " ;
cin >> supervisorSalary;
cout << "What is the supervisor's bonus? " ;
cin >> supervisorBonus;
shiftSupervisor.setEmployeeName(supervisorName);
shiftSupervisor.setEmployeeNumber(supervisorNumber);
shiftSupervisor.setHireDate(supervisorHireDate);
shiftSupervisor.setSalary(supervisorSalary);
shiftSupervisor.setBonus(supervisorBonus);
cout << "Employee Name: " << shiftSupervisor.getEmployeeName() << endl;
cout << "Employee Number: " << shiftSupervisor.getEmployeeNumber() << endl;
cout << "Hire Date: " << shiftSupervisor.getHireDate() << endl;
cout << "Salary: $" << shiftSupervisor.getSalary() << endl;
cout << "Bonus: $" << setprecision(2) << fixed << shiftSupervisor.getBonus() << endl;
cout << "Total Earnings: $" << shiftSupervisor.getSalary() + shiftSupervisor.getBonus() << endl;
cout << "Do you want to run this program again? Y/N: " ;
cin >> again;
} while (again == 'y' || again == 'Y' );
return 0;
}
May 8, 2013 at 4:08pm UTC
try std::getchar() just before return 0;
remember to include <cstdio> for std::getchar()!
May 8, 2013 at 4:30pm UTC
Thanks but that didn't work either.
May 8, 2013 at 4:45pm UTC
What inputs are you giving the program?
May 8, 2013 at 4:47pm UTC
I wonder what happens if he does while (true), I'm suspicious of an input overflow if there's no crash...
Edit:
I tried it (clean copy):
Shift Supervisor Class
-----------------------
What is the supervisor's name?
John
What is the supervisor's number? 213213
When was the supervisor hired? 02.11.2011
What is the supervisor's salary? 2000
What is the supervisor's bonus? 100
Employee Name: John
Employee Number: 213213
Hire Date: 02.11.2011
Salary: $2000
Bonus: $100
Total Earnings: $2100
Do you want to run this program again? Y/N: y
Shift Supervisor Class
-----------------------
What is the supervisor's name?
I use GCC (C::B as IDE) on Linux Mint 14 (MATE), -std=c++0x
Last edited on May 8, 2013 at 4:51pm UTC
May 8, 2013 at 6:18pm UTC
Try adding cin.get() after all inputs and before return 0; it works with me.
Topic archived. No new replies allowed.