I am about to rip my hair out with the results im getting from this simple file I/O program

Hello everyone. I am incredibly, incredibly frustrated and confused. ALL i am trying to do is read in a list of names from a master.lst, which contains 3 lines only, 3 names.

Then, read in data from a .dat file, and put the data into variables.

When i run this program ALL I GET is a random-ass float value in the middle of the .dat file, from nowhere. all the previous data that was in there was erased. Please, for the love of god, help me.




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
  

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>


using namespace std;

const string EXTENSION1 = ".dat";
const string EXTENSION2 = ".report";

int main ()
{
	string baseName;
	string fullName, jobDescrip;
	string inputFileName, outputFileName;
	ifstream masterList, inFile;
	ofstream outFile;
	float hoursWorked;
	float payRate;

		// Variables.
	masterList.open("master.lst");

	masterList >> baseName;

    inputFileName = baseName + EXTENSION1;                                                // This is operations for first employee listed in the master.lst, ONLY.
    outputFileName = baseName + EXTENSION1;

	inFile.open ((inputFileName).c_str());                     // Opening up the I/O files and naming them according to the data contained within MASTER.LST
	outFile.open ((outputFileName).c_str());

    getline (inFile, fullName);                                        // reading data from the .dat file for .report use
cout << "Contents of fullName: " << fullName << endl;

    inFile >> hoursWorked;
    cout << "Contents of hoursWorked: " << hoursWorked << endl;

    inFile >> payRate;
    cout << "Contents of payRate " << payRate << endl;

    getline (inFile, jobDescrip);
    cout << "Contents of jobDescrip " << jobDescrip << endl;

    outFile << fullName << endl << hoursWorked << endl;


    /*


	ofstream employeeReport1 ( (employeeName1+=".report").c_str() );

	employeeReport1 << "Dear" << fullName <<
		"Last week you worked a total of " <<  hoursWorked << endl;
	employeeReport1 << "In addition, your pay rate is " << payRate <<																		// contents of .report information
		"Your pay rate is " << payRate << "per hour.";
	employeeReport1	<<	"The amount $ " << payRate << " has been electronically deposited in your account. " << endl;
	employeeReport1 << "We appreciate your service as " << jobDescrip << endl;


	ifstream employeeData2( (employeeName2+=".dat").c_str() );

	string fullName2, jobDescrip2,hourAmount2, hourlyPay2;			// same for rest of employees

	ifstream employeeData3( (employeeName3+=".dat").c_str() );

	string fullName3, jobDescrip3,hourAmount3, hourlyPay3;


	// setw(4) >> hourAmount1 >> setw(5) >> hourlyPay1 >> setw(13) >> jobDescrip1;
	// cout << fullName1 << jobDescrip1;

	//getline(employeeData1, employeeInformation, '\n');

	//ofstream employeeReport1 ( (employeeName1+=".report").c_str() );
	//employeeReport1 << "Dear" << employeeName1 << endl;
	//employeeReport1 << "Last week, you have worked a total of

	*/



	return 0;

}














	// ifstream employeeData1( (employeeName1+=".dat").c_str() );
	// employeeData1.open( employeeName1.c_str() );
	// employeeData1 >> data1 >> data2 >> data3;

//	cout << data1 << endl



	//  ofstream employeeData1( (employeeName1+=".dat").c_str() );


	 // employeeData1.open( employeeName1.c_str() );
Last edited on
OKAY, so i figured out if i comment out the open function for the OUTPUT file, it works, (so far). does anyone want to tell me WHY this happens?


Thanks.


EDIT: ALSO, i am having trouble reading another string in from the data file. It reads in the first one perfectly fine, but the last one, jobDescrip, simply does not do anything. Any help?
Last edited on
can you post the text files ?

I am about to rip my hair out with the results im getting from this simple file I/O program


we all feel that pain :)) LOL
Okay, i feel like a doofus lol. After my initial freaking-out frustration (somewhat reminiscent of Bryan Cranston's freakouts), I've figured out a lot of things. Let me finish up my code, and i will post my updated source here in a second.


oh, and thanks for responding!
Here is my updated code:
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>


using namespace std;

const string EXTENSION1 = ".dat";
const string EXTENSION2 = ".report";

int main ()
{
	string baseName, baseName2, baseName3;
	string fullName,fullName_payroll1, fullName_payroll2, fullName_payroll3;
	string  jobDescrip, jobDescrip_payroll1, jobDescrip_payroll2, jobDescrip_payroll3;
	string inputFileName, outputFileName;
	ifstream masterList, inFile;
	ofstream outFile;

	float hoursWorked, hoursWorked_payroll1, hoursWorked_payroll2, hoursWorked_payroll3;
	float payRate, payRate_payroll1, payRate_payroll2, payRate_payroll3;
	float totalPayAmount;

		// Variables.
	masterList.open("master.lst");

	masterList >> baseName >> baseName2 >> baseName3;

    inputFileName = baseName + EXTENSION1;                                                // This is operations for first employee listed in the master.lst, ONLY.
    outputFileName = baseName + EXTENSION2;

	                  // Opening up the I/O files and naming them according to the data contained within MASTER.LST

//	YOU HAVE JUST FIGURED OUT THAT COMMENTING OUT THE OPEN FUNCTION ON THE OUTPUT FILE STREAM MAKES IT WORK. WAS JUST THINKING ABOUT USING CLOSE FOR EACH EMPLOYEE NAME, AND THEN OPEN UP THE NEEDED
	//OUTFILE FILE STREAM FOR THE .REPORT. THEN, OPEN UP THE INPUTFILE STREAM, READ IN THE NEEDED VARIABLES, AND OUTPUT THAT TO THE .REPORT.
inFile.open ((inputFileName).c_str());

    getline (inFile, fullName);                                        // reading data from the .dat file for .report use
cout << "Contents of fullName: " << fullName << endl;

    inFile >> hoursWorked;
    cout << "Contents of hoursWorked: " << hoursWorked << endl;

    inFile >> payRate;
    cout << "Contents of payRate " << payRate << endl;

    getline (inFile, jobDescrip);
    cout << "Contents of jobDescrip " << jobDescrip << endl;

inFile.close ();

// PUT THE ABOVE DATA OF EMPLOYEE ONE INTO NEEDED VARIABLES FOR FINAL PAYROLL.OUT

fullName_payroll1 = fullName;
hoursWorked_payroll1 = hoursWorked;
payRate_payroll1 = payRate;
jobDescrip_payroll1 = jobDescrip;

// GENERATION OF THE REPORT FILE FOR EMPLOYEE NUMBER 1
outFile.open ((outputFileName).c_str());

outFile << "Dear" << fullName << endl <<
        "Last week you worked a total of " << hoursWorked << "hours." << endl;
      outFile << "Your pay rate is " << payRate << " per hour." << endl;
      outFile <<  "The amount $ " << payRate * hoursWorked << " has been electronically deposited into your account." <<endl;
       outFile << "We appreciate your service as " << jobDescrip << endl;


outFile.close ();



        // READING OF DATA FOR SECOND EMPLOYEE

        inputFileName = baseName2 + EXTENSION1;
    outputFileName = baseName2 + EXTENSION2;

inFile.open ((inputFileName).c_str());

    getline (inFile, fullName);                                        // reading data from the .dat file for .report use
cout << "Contents of fullName: " << fullName << endl;

    inFile >> hoursWorked;
    cout << "Contents of hoursWorked: " << hoursWorked << endl;

    inFile >> payRate;
    cout << "Contents of payRate " << payRate << endl;

    getline (inFile, jobDescrip);
    cout << "Contents of jobDescrip " << jobDescrip << endl;


inFile.close ();

// PUT THE ABOVE DATA OF EMPLOYEE TWO INTO NEEDED VARIABLES FOR FINAL PAYROLL.OUT

fullName_payroll2 = fullName;
hoursWorked_payroll2 = hoursWorked;
payRate_payroll2 = payRate;
jobDescrip_payroll2 = jobDescrip;


// GENERATION OF REPORT FILE FOR EMPLOYEE NUMBER 2




outFile.open ((outputFileName).c_str());

outFile << "Dear" << fullName << endl <<
        "Last week you worked a total of " << hoursWorked << "hours." << endl;
      outFile << "Your pay rate is " << payRate << " per hour." << endl;
      outFile <<  "The amount $ " << payRate * hoursWorked << " has been electronically deposited into your account." <<endl;
       outFile << "We appreciate your service as " << jobDescrip << endl;


outFile.close ();



// NOW FOR DATA READING OF EMPLOYEE NUMBER 3


        inputFileName = baseName3 + EXTENSION1;
    outputFileName = baseName3 + EXTENSION2;

inFile.open ((inputFileName).c_str());

    getline (inFile, fullName);                                        // reading data from the .dat file for .report use
cout << "Contents of fullName: " << fullName << endl;

    inFile >> hoursWorked;
    cout << "Contents of hoursWorked: " << hoursWorked << endl;

    inFile >> payRate;
    cout << "Contents of payRate " << payRate << endl;

    getline (inFile, jobDescrip);
    cout << "Contents of jobDescrip " << jobDescrip << endl;


inFile.close ();

// PUT THE ABOVE DATA OF EMPLOYEE THREE INTO NEEDED VARIABLES FOR FINAL PAYROLL.OUT

fullName_payroll3 = fullName;
hoursWorked_payroll3 = hoursWorked;
payRate_payroll3 = payRate;
jobDescrip_payroll3 = jobDescrip;


// GENERATION OF .REPORT FOR EMPLOYEE NUMBER 3
outFile.open ((outputFileName).c_str());

outFile << "Dear" << fullName << endl <<
        "Last week you worked a total of " << hoursWorked << "hours." << endl;
      outFile << "Your pay rate is " << payRate << " per hour." << endl;
      outFile <<  "The amount $ " << payRate * hoursWorked << " has been electronically deposited into your account." <<endl;
       outFile << "We appreciate your service as " << jobDescrip << endl;


outFile.close ();

// GENERATION OF THE PAYROLL.OUT OUTPUT FILE
ofstream payRoll_Out;

payRoll_Out.open ("payroll.out");


payRoll_Out << fullName_payroll1 << ", " << hoursWorked_payroll1 << ", " << payRate_payroll1 * hoursWorked_payroll1 << endl;

payRoll_Out << fullName_payroll2 << ", " << hoursWorked_payroll2 << ", " << payRate_payroll2 * hoursWorked_payroll2 << endl;

payRoll_Out << fullName_payroll3 << ", " << hoursWorked_payroll3 << ", " << payRate_payroll3 * hoursWorked_payroll3 << endl;

totalPayAmount = (payRate_payroll1 * hoursWorked_payroll1) + (payRate_payroll2 * hoursWorked_payroll2) + (payRate_payroll3 * hoursWorked_payroll3);

payRoll_Out << "TOTAL PAYROLL: " << "$" << totalPayAmount << endl;

	return 0;

}





now my ONLY PROBLEM here is that it simply does not read in jobDescrip from the input file!
@ line 47 & other relevant parts :
1
2
3
4
5
6
7
8
inFile >> payRate; // using >> before getline() is terrible
cout << "Contents of payRate " << payRate << endl;

// to stop conflicts w/ >> and getline(), add this line :
inFile.ignore();   

getline (inFile, jobDescrip);
cout << "Contents of jobDescrip " << jobDescrip << endl;


add that <object_name>.ignore() for every occurance in your code in which you're going to use getline() after >> operator
Last edited on
Topic archived. No new replies allowed.