program output file

I have done a program for my beginner C++ class. When I rebuild the solution, I don't get any errors, and when I start without debugging, the program runs perfectly. The problem is, when I look at the output file, there are a few things missing, and I don't know how to fix it.
I have a very strong urge to go full sarcasm mode here but I'll retain myself.

We can't read your mind or hack your computer, open your program and see what's wrong. Post your code, using code tags - http://www.cplusplus.com/articles/jEywvCM9/ And tell us what you think the problem might be. It would also help to post a bit of your output file as it is now, and how it should actually look like.
Could you provide your code (or at least the important part of it) and what it should do. It's a little hard to help you without any information.
ha, yesss i was waiting for responses like this. This is my first time using this site and didn't know how much code to post. here is what I get when I run the program:
************************************************************************************************************************
Dwayne Johnson
CST 113 - Exercise 6
************************************************************************************************************************
Enter the name of input file to process
Select from these three files:
1. Ex6-1.txt
2. Ex6-2.txt
3. Ex6-3.txt

************************************************************************************************************************
Enter the input file choice: 1
The file Ex6-1.txt was successfully opened
************************************************************************************************************************
************************************************************************************************************************
Task 1 - Grocery Total and Points Awarded

The total groceries spent for the month are: 501.24
The total points awarded are: 140
************************************************************************************************************************
************************************************************************************************************************
Task 2 - C++ Academy Standing

Student's name: Karen Connolly
Total credits completed: 299
Standing of the student: Professional
Advisor's name: Mrs. Cooley
************************************************************************************************************************
************************************************************************************************************************
Task 3 - Tire Pressure

The psi of your tire is: 42
Your tire pressure is in the: Too High range
Decrease your pressure by: 7
************************************************************************************************************************
************************************************************************************************************************
Task 4 - High School Counselor Grouping

Name of the student: Fitzgerald, Kelsie
Group number assigned: 2
************************************************************************************************************************
Press any key to continue . . .
this is what the output file gives me:
************************************************************************************************************************
Task 1 - Grocery Total and Points Awarded

The total groceries spent for the month are: 501.24
************************************************************************************************************************
Task 2 - C++ Academy Standing

Student's name: Karen Connolly
Total credits completed: 299
Standing of the student: Professional
Advisor's name: Mrs. Cooley
************************************************************************************************************************
************************************************************************************************************************
Task 3 - Tire Pressure

Decrease your pressure by: 7
************************************************************************************************************************
I'm missing a piece of task 1, task 2 is completely fine, missing a piece of task 3, and task 4 just isn't there.
This is my first time using this site and didn't know how much code to post.


You still haven't posted any code. How do you expect us to help you solve your problem if you are not even showing us the code which contains the problem? Please post your code.

Use code tags for your code - http://www.cplusplus.com/articles/jEywvCM9/
my code is too long to do in one post, so bare with me please:

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

using namespace std;

const int SCREEN_WIDTH = 120;

const string PROGRAMMER_NAME = "Dwayne Johnson";
const string COURSE = "CST 113 - Exercise 6";

int main(void)
{


	string firstName, lastName;
	string ranking, advisor;
	string filename;

	ofstream outfile;
	ifstream infile;

	//Task 1
	double monthlyGroceryTotal = 0.0;
	double weeklyTotal = 0.0;

	//Task 2
	int creditsCompleted;

	//Task 3
	int tirePressure;
	int perfectPsiDiff;
	int adjustmentAmount;

	string rangeMessage = "";
	string adjustmentMessage = "";

	//Task 4
	int groupNum = 0;

	string studentLast = "";
	string studentFirst = "";
	string fullName = "";

	char firstCharLastName;

	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;

	const string PROGRAMMER_NAME = "Dwayne Johnson";
	const string COURSE = "CST 113 - Exercise 6";

	int centerWidth = (SCREEN_WIDTH + PROGRAMMER_NAME.length()) / 2;
	cout << setw(centerWidth) << PROGRAMMER_NAME << endl;
	centerWidth = (SCREEN_WIDTH + COURSE.length()) / 2;
	cout << setw(centerWidth) << COURSE << endl;

	//Output border to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;

	//Let user know they will have to enter a file name to process
	cout << "Enter the name of input file to process" << endl;

	//Prompt user to select from three files
	cout << "Select from these three files:" << endl;

	//Output to the screen the three possible file options to select from
	cout << "1. Ex6-1.txt" << endl;
	cout << "2. Ex6-2.txt" << endl;
	cout << "3. Ex6-3.txt" << endl << endl;

	//Output border to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;




	//Output to the screen input file message
	cout << "Enter the input file choice: ";

	outfile.open("Ex6Out.txt");

	//If user enters 1, open file 1
	int fopt;
	cin >> fopt;
	if (fopt == 1)
	{
		filename = "Ex6-1.txt";
		infile.open("Ex6-1.txt");
	}

	//If user enters 2, open file 2
	else if (fopt == 2)
	{
		filename = "Ex6-2.txt";
		infile.open("Ex6-2.txt");
	}

	//If user enters anything else, open file 3
	else if (fopt == 3)
	{
		filename = "Ex6-3.txt";
		infile.open("Ex6-3.txt");
	}

	//Output message to the screen that selected file was opened successfully
	cout << "The file " << filename << " was successfully opened" << endl;

	//Output border to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;



	//---------------------------------------------------------------
	//Task 1 - Grocery Total & Points Awarded

	outfile << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;
	outfile << "Task 1 - Grocery Total and Points Awarded \n\n";

	//Output border to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;

	//Output Task 1 to the screen
	cout << "Task 1 - Grocery Total and Points Awarded \n\n";

	for (int i = 0; i < 4; i++)
	{
		infile >> weeklyTotal;
		monthlyGroceryTotal += weeklyTotal;
	}

	outfile << "The total groceries spent for the month are: " << monthlyGroceryTotal << endl;

	//Output total groceries heading to the screen
	cout << "The total groceries spent for the month are: " << monthlyGroceryTotal << endl;

	//Assign pointsAwared a value
	int pointsAwarded = 0;

	//Determine pointsAwarded based on monthlyGroceryTotal
	if (monthlyGroceryTotal>1000.00)
	{
		//Assign pointsAwarded to 325
		pointsAwarded = 325;
	}

	else if (monthlyGroceryTotal>850.00)
	{
		//Assign pointsAwarded to 250
		pointsAwarded = 250;
	}

	else if (monthlyGroceryTotal>625.00)
	{
		//Assign pointsAwarded to 185
		pointsAwarded = 185;
	}

	else if (monthlyGroceryTotal>450.00)
	{
		//Assign pointsAwarded to 140
		pointsAwarded = 140;
	}

	else if (monthlyGroceryTotal > 250.00)
	{
		//Assign pointsAwarded to 100
		pointsAwarded = 100;
	}

	else if (monthlyGroceryTotal > 0.00)
	{
		//Assign pointsAwarded to 50
		pointsAwarded = 50;
	}

	else
	{
		//Assign pointsAwarded to 0
		pointsAwarded = 0;
	}

	//Output total points heading to the screen
	cout << "The total points awarded are: " << pointsAwarded << endl;

	//Output divider line to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;
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
	//---------------------------------------------------------------
	//Task 2 - C++ Academy Standing

	outfile << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;
	outfile << "Task 2 - C++ Academy Standing  \n\n";

	//Output border to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;

	//Output task 2 to the screen
	cout << "Task 2 - C++ Academy Standing  \n\n";



	infile >> firstName >> lastName;


	infile >> creditsCompleted;

	switch (creditsCompleted / 100)
	{
	case 0:
	{
		ranking = "Beginner";
		advisor = "Mr. Kohut";
	}
	case 1:
	{
		ranking = "Intermediate";
		advisor = "Mrs. Sedelmeyer";
	}
	case 2:
	{
		ranking = "Associate";
		advisor = "Mrs. Gannett";
	}
	case 3:
	{
		ranking = "Professional";
		advisor = "Mrs. Cooley";
	}
	}

	outfile << "Student's name: " << firstName << " " << lastName << endl;
	outfile << "Total credits completed: " << creditsCompleted << endl;
	outfile << "Standing of the student: " << ranking << endl;
	outfile << "Advisor's name: " << advisor << endl;
	outfile << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;


	//Output students name heading to the screen
	cout << "Student's name: " << firstName << " " << lastName << endl;

	//Output Total credits completed to the screen
	cout << "Total credits completed: " << creditsCompleted << endl;

	//Output Standing of student to the screen
	cout << "Standing of the student: " << ranking << endl;

	//Output Advisors name to the screen
	cout << "Advisor's name: " << advisor << endl;

	//Output border to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;


	//---------------------------------------------------------------
	//Task 3 - Tire Pressure

	//Output divider line to output file
	outfile << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;

	//Output task 3 to output file
	outfile << "Task 3 - Tire Pressure  \n\n";

	//Output border to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;

	//Output Task 3 to the screen
	cout << "Task 3 - Tire Pressure  \n\n";

	//Assign the perfect tire pressure a value
	const int PERFECT_PSI = 35;

	adjustmentAmount = 5;

	infile >> tirePressure;

	//Determine range message based on tire pressure
	if (tirePressure > PERFECT_PSI)
	{
		//Assign rangeMessage "too high"
		rangeMessage = "Too High";
	}

	else if (tirePressure > PERFECT_PSI - adjustmentAmount)
	{
		//Assign rangeMessage "normal"
		rangeMessage = "Normal";
	}

	else if (tirePressure > PERFECT_PSI - adjustmentAmount * 2)
	{
		//Assign rangeMessage "Low"
		rangeMessage = "Low";
	}

	else if (tirePressure > PERFECT_PSI - adjustmentAmount * 3 - 1)
	{
		//Assign rangeMessage "Dangerous"
		rangeMessage = "Dangerous";
	}

	else
	{
		//Assign rangeMessage "Flat"
		rangeMessage = "Flat";
	}

	//Output tire psi heading to the screen
	cout << "The psi of your tire is: " << tirePressure << endl;

	//Output tire pressure heading to the screen
	cout << "Your tire pressure is in the: " << rangeMessage << " range" << endl;

	//Calculate perfectPsi
	perfectPsiDiff = tirePressure - PERFECT_PSI;

	if (perfectPsiDiff == 0)
	{
		outfile << "The tire pressure is perfect" << endl;
		cout << "The tire pressure is perfect" << endl;
	}
	else if (perfectPsiDiff > 0)
	{
		outfile << "Decrease your pressure by: " << perfectPsiDiff << endl;
		cout << "Decrease your pressure by: " << perfectPsiDiff << endl;
	}
	else if (perfectPsiDiff < 0)
	{
		outfile << "Increase your pressure by: " << (-1 * perfectPsiDiff) << endl;
		cout << "Increase your pressure by: " << (-1 * perfectPsiDiff) << endl;
	}

	outfile << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;

	//Output border to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;
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
	//---------------------------------------------------------------
	//Task 4 - High School Counselor Grouping

	infile >> studentLast >> studentFirst;
	fullName = studentLast + " " + studentFirst;

	firstCharLastName = toupper(fullName.at(0));

	//Output border to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;

	//Output Task 4 to the screen
	cout << "Task 4 - High School Counselor Grouping  \n\n";

	switch (firstCharLastName)
	{
	case 'A':
	case 'E':
	case 'I':
	case 'M':
	case 'Q':
	case 'U':
	case 'Y':
		groupNum = 1;
		break;
	case 'B':
	case 'F':
	case 'J':
	case 'N':
	case 'R':
	case 'V':
	case 'Z':
		groupNum = 2;
		break;
	case 'C':
	case 'G':
	case 'K':
	case 'O':
	case 'S':
	case 'W':
		groupNum = 3;
		break;
	case 'D':
	case 'H':
	case 'L':
	case 'P':
	case 'T':
	case 'X':
		groupNum = 4;
		break;

	}

	//Output Name of Student heading to the screen
	cout << "Name of the student: " << fullName << endl;

	//OUtput Group Number heading to the screen
	cout << "Group number assigned: " << groupNum << endl;

	//Output border to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;

	//Close input file
	infile.close();

	//Close output file
	outfile.close();

	return 0;
}
Ok, I'm guessing you want your output file to contain the same information as cout? If so, you should probably write everything you write to cout to your output file stream as well.

As an example, we'll take part 4:

Your logic is perfectly fine, for as far as I can see. The problem arises when we come near the end:

1
2
3
4
5
//Output Name of Student heading to the screen
cout << "Name of the student: " << fullName << endl;

//Output Group Number heading to the screen
cout << "Group number assigned: " << groupNum << endl;


Here you write your information to cout, but you never write to the output file stream. You should probably change this into:

1
2
3
4
5
6
7
8
9
//Output Name of Student heading to the screen
cout << "Name of the student: " << fullName << endl;
//Output Name of Student heading to the file
outfile << "Name of the student: " << fullName << endl;

//Output Group Number heading to the screen
cout << "Group number assigned: " << groupNum << endl;
//Output Group Number heading to the file
outfile << "Group number assigned: " << groupNum << endl;


This way you write it to both cout and your file stream, making sure everything ends up in both. You'll have to do this for every time you use cout, otherwise you won't write the information to your file.

I think this is what you intended.
Last edited on
that worked perfectly! thank you. Now the only problem I have, is getting the name and course number to go to the output file. when I try to use the outfile for that, too, it doesn't go over.
I'm looking at my last program that also has an output file, and I don't see anything that I'm missing in the code... yet the name show up in the output file.
Same thing as Shadowwolf said. You never output the name and course number to the file, so just do that before you output task 1.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
// output name and course
outfile << The name << endl;
outfile << The course << endl;

//Task 1 - Grocery Total & Points Awarded

	outfile << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;
	outfile << "Task 1 - Grocery Total and Points Awarded \n\n";

	//Output border to the screen
	cout << setfill('*') << setw(SCREEN_WIDTH) << "*" << setfill(' ') << endl;

	//Output Task 1 to the screen
	cout << "Task 1 - Grocery Total and Points Awarded \n\n";
Have you studied functions yet? This "program" is screaming for their use.
Topic archived. No new replies allowed.