Exception Handling

In this program I am trying to catch the out of bounds input from the user. I have options for 1 and 2, but would like to have an exception put out a message if they go below one or above two. I have added the exception as I see it should work and I am getting errors. I don't have line numbers it states error LNK2005 and error LNK1169. Please let me know if there is a more effective way to do this.

driver.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "employee.h"

using namespace std;

// printCheck function
// Purpose: Create a paystub for an employee
// Parameters: Employee information
// Returns: void
void printCheck(const Employee&);

//Setting variables
int userSelection;
string fileName;

//main method
//Purpose: To call to my employee functions and display information about employees
//Parameters: None
//Returns: None
int main()
{
	//Getting information from the user
	cout << "This program has two options:" << endl;
	cout << "1 - Create a data file, or " << endl;
	cout << "2 - Read data from a file and print paychecks." << endl;
	cout << "Please enter <1> to create a file or <2> to print checks: ";
	cin >> userSelection;
	if (userSelection < 1 && userSelection > 2)
	{
		try
		{
			throw myex;
		}
		catch (outOfBoundsNumber& e)
		{
			cout << e.what() << '\n';
		}
		return 0;
	}
	cout << "Please enter file name: ";
	cin >> fileName;

	Employee b1(1234, "Bruce Banner", "436 E 900 S", "801-465-2291", 10.00, 45);
	Employee b2(1569, "Tony Stark", "7000 E 9680 S", "801-941-0745", 12.50, 30);
	Employee b3(1967, "Charles Xavier", "500 W 780 N", "801-754-3687", 15.00, 40);

	//If statement that handles the writeEmployee information
	if (userSelection == 1)
	{
		ofstream odataFile(fileName);
		if (odataFile.good())
		{
			//Write the data
			b1.writeEmployee(odataFile);
			b2.writeEmployee(odataFile);
			b3.writeEmployee(odataFile);
			odataFile.close();
			cout << "Data file created ... you can now run option 2." << endl;
			system("PAUSE");
		}
	}
	//If statment that handles the readEmployee information
	if (userSelection == 2)
	{
		ifstream idataFile(fileName);
		if (idataFile.good())
		{
			// Read the file
			if (b1.readEmployee(idataFile))
			{
				// If read was successful, print b1
				printCheck(b1);
			}
			if (b2.readEmployee(idataFile))
			{
				//If read was successful, print b2
				printCheck(b2);
			}
			if (b3.readEmployee(idataFile))
			{
				//If read was successful, print b3
				printCheck(b3);
			}
			else // the read operation failed ... display a message
				cout << "\nRead failed";
		}
	}
}

//printCheck method
//Purpose: To organize data into a readable window
//Parameters: Const Employee information from .h and .cpp files
//Returns: Employee information
void printCheck(const Employee& b)
{
	cout << "\n--------------------Marvel Comics--------------------" << endl;
	cout << "\n\nPay to the order of " << b.getEmployeeName() << ".............$" << setprecision(2) << fixed << b.getCalcPay() << endl;
	cout << "\n\nS.H.I.E.L.D Bank" << endl;
	cout << "-----------------------------------------------------" << endl;
	cout << "Hours worked: " << b.getWeeklyHours() << endl;
	cout << "Hourly Wage: " << b.getHourlyWage() << endl;
	cout << "\n" << endl;
	system("PAUSE");
	system("CLS");
}


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <stdexcept>
#include <exception>

using namespace std;

//Provides start point for employee information
class Employee
{
private:
	//Setting initial values
	int employeeNumber;
	string employeeName;
	string employeeAddress;
	string phoneNumber;
	double hourlyWage;
	double weeklyHours;
	double calcPay;

public:
	//Default constructor
	//Purpose: Initialize data in employee object to null
	//Parameters: None
	//Returns: None
	Employee( );

	//Parameterized constructor
	//Purpose: Initialize data in employee object to specified data
	//Parameters: Employee number, name, address, phone number, hourly wage, week hours
	//Returns: None
	Employee(int, string, string, string, double, double);

	//readEmployee function
	//Purpose: Reads employee information from a file
	//Parameters: A reference from the ifstream object
	//Returns: True if read is successful
	bool readEmployee(ifstream&);

	//writeEmployee function
	//Purpose: Writes employee information to a file
	//Parameters: A reference from the ofstream object
	//Returns: None
	void writeEmployee(ofstream&);

	// getEmployeeNumber function
	// Purpose: returns number of employee
	// Parameters: none
	// Returns: The employees number
	int getEmployeeNumber( ) const;

	// getName function
	// Purpose: Returns name of employee
	// Parameters: none
	// Returns: The employees name
	string getEmployeeName( ) const;

	// getAddress function
	// Purpose: Returns address of employee
	// Parameters: none
	// Returns: The employees address
	string getEmployeeAddress( ) const;

	// getPhoneNumber function
	// Purpose: Returns phone number of employee
	// Parameters: none
	// Returns: The employees phone number
	string getPhoneNumber( ) const;

	// getHourlyWage function
	// Purpose: Returns hourly wage of employee
	// Parameters: none
	// Returns: The employees wage
	double getHourlyWage( ) const;

	// getWeeklyHours function
	// Purpose: Returns weekly hours of employee
	// Parameters: none
	// Returns: The employees weekly hours
	double getWeeklyHours( ) const;

	// getCalcPay function
	// Purpose: Returns total pay for the employee
	// Parameters: none
	// Returns: The employees paycheck
	double getCalcPay( ) const;
};

class outOfBoundsNumber
{
public:
	outOfBoundsNumber() : message("Error: Only options '1' and '2' are available for this program")
	{ }
	string what() const { return message; }

private:
	string message;
} myex;
No idea what linker errors you're getting. Please post the FULL text of the linker errors.

Since you did not post employee.cpp, was unable to compile and link your program.
Here is the employee.cpp, the errors read. Error 3 error LNK2005: "class outOfBoundsNumber myex" (?myex@@3VoutOfBoundsNumber@@A) already defined in driver.obj and Error 4 error LNK1169: one or more multiply defined symbols found


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "employee.h"

//Declaring constants
const int WEEK_MAX = 40;
const double FED_TAX = 0.20;
const double STATE_TAX = 0.075;
const double OVER_TIME = 1.5;
const char delim = '\t';
const char space = ' ';

//Declaring variables 
double netPay;
double fedTotal;
double stateTotal;
float payCheck;
double overHours;
double overPay;
double regularPay;

//Setting initial values for the default constructor
Employee::Employee()
{
	employeeNumber = 0;
	employeeName = "";
	employeeAddress = "";
	phoneNumber = "";
	hourlyWage = 0.00;
	weeklyHours = 0.00;
}

//Setting condensed names for parameterized constructor
Employee::Employee(int eNumber, string eName, string eAddress, string pNumber, double hWage, double wHours)
{
	employeeNumber = eNumber;
	employeeName = eName;
	employeeAddress = eAddress;
	phoneNumber = pNumber;
	hourlyWage = hWage;
	weeklyHours = wHours;
}

//Reading information into formatted statements
bool Employee::readEmployee(ifstream& input)
{
	if (input >> employeeNumber && getline(input, employeeName, delim) && getline(input, employeeAddress, delim) && getline(input, phoneNumber, delim) && input >> hourlyWage && input >> weeklyHours)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//Writing information and delimiting output
void Employee::writeEmployee(ofstream& output)
{
	//No error checking when writing a file
	output << employeeNumber	<< space
		   << employeeName		<< delim
		   << employeeAddress	<< delim 
		   << phoneNumber		<< delim 
		   << hourlyWage		<< space
		   << weeklyHours		<< '\n';
}

//EmployeeNumber setter
int Employee::getEmployeeNumber( ) const
{
	return employeeNumber;
}

//EmployeeName setter
string Employee::getEmployeeName( ) const
{
	return employeeName;
}

//EmployeeAddress setter
string Employee::getEmployeeAddress( ) const
{
	return employeeAddress;
}

//PhoneNumber setter
string Employee::getPhoneNumber( ) const
{
	return phoneNumber;
}

//HourlyWage setter
double Employee::getHourlyWage( ) const
{
	return hourlyWage;
}

//WeeklyHours setter
double Employee::getWeeklyHours( ) const
{
	return weeklyHours;
}

//CalcPay method
//Purpose: To calculate pay for an employee
//Parameters: None
//Returns: payCheck as a float
double Employee::getCalcPay( ) const
{
	if (weeklyHours <= WEEK_MAX)
	{
		netPay = hourlyWage * weeklyHours;
		fedTotal = netPay * FED_TAX;
		stateTotal = netPay * STATE_TAX;
		payCheck = ((netPay - fedTotal) - stateTotal);
	}
	else if (weeklyHours > WEEK_MAX)
	{
		overHours = weeklyHours - WEEK_MAX;
		regularPay = WEEK_MAX * hourlyWage;
		overPay = overHours * (hourlyWage * OVER_TIME);
		netPay = regularPay + overPay;
		fedTotal = netPay * FED_TAX;
		stateTotal = netPay * STATE_TAX;
		payCheck = ((netPay - fedTotal) - stateTotal);
	}
	return payCheck;
}
1
2
3
4
class outOfBoundsNumber
{
/*...*/
} myex;

myex is a variable defined in header file.
If you include it more than once, you will have multiple definition error.

Solution: never have variable definition in header file.
Topic archived. No new replies allowed.