stream problem

In this code when the user presses 1 they are prompted to enter a name for a file, after they enter it; it is saved. The program then tells them to use option number 2 and closes, when the user opens the project again and presses option 2 it should print out the employee information. When I press two after entering a name for the file it simply closes. What am I missing here?
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
#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()
{
	
	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;
	
	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 (userSelection == 1)
	{
		cout << "Please enter file name: ";
		cin >> fileName;
		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 (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.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
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;
}


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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>

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;
};
For option 2, fileName will be an empty string "", so the file open fails.
Thanks for your reply, I am just trying to wrap my head around this. Why would that be an empty string? when I am saving the users input on line 35?
Why would that be an empty string? when I am saving the users input on line 35?
But that is option 1.

I said it would be empty for option 2.

The program then tells them to use option number 2 and closes, when the user opens the project again and presses option 2 ...
On the second run, the variables are all re-initialised. The only thing that is saved from the first run is the actual data file, and the employee data inside that file.
Wow! it was so much easier that I was thinking! thanks for your help again Chervil!
Perhaps you really wanted a loop something like this:
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
#include <iostream>

using namespace std;

int main()
{
    cout << "This program has two options:\n"
        "1 - Do the first thing  \n"
        "2 - Do the second thing \n"
        "0 - Exit" << endl;
    
    int userSelection = 0;
    
    do 
    {
        cout << "please enter 1, 2 or 0 to exit" << endl;
        cin >> userSelection;
        
        if (userSelection == 1)
        {
            cout << "--- ONE ---" << endl;
            // etc.
        }
        if (userSelection == 2)
        {
            cout << "--- TWO ---" << endl;
            // etc.
        }
    } while (userSelection != 0);
    
    return 0;
}

Last edited on
Topic archived. No new replies allowed.