help saving to txt file

i have been asked to make a gradebook where i ask the user to enter a students first and last name major as well as three assignment scores and their average. I have to do this for three students and then find the lowest/highest grade per assignment and then the mean score for each assignment.

My problem is that when i try to save the information to a file its only saving the data from the first set in the loop, not the other two. Any help would be greatly appreciated.
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

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

std::ifstream inputHandler;
std::ofstream outputHandler;
using namespace std;

int main()
{
	int rank;
	int assignmentScore1;
	int assignmentScore2;
	int assignmentScore3;
	int averageScore;
	int count = 0;


	string firstName;
	string lastName;
	string major;
	
	for (count = 0; count <= 2; count++)
	{
		// Get student's first name
		std::cout << "Enter the student's first name. ";
		std::cin >> firstName;

		// Get student's last name
		std::cout << "Enter the student's last name. ";
		std::cin >> lastName;

		// Get student's major
		std::cout << "Enter the student's major. ";
		std::cin >> major;

		// Get 1st assignment score
		 std::cout << "Enter the student's score for assignment 1. ";
		std::cin >> assignmentScore1;

		// Get assignment score 2
		std::cout << "Enter the student's score for assignment 2. ";
		std::cin >> assignmentScore2;

		// Get assignment score 3
		std::cout << "Enter the student's score for assignment 3. ";
		std::cin >> assignmentScore3;

		//Get average assignment score
		averageScore = (assignmentScore1 + assignmentScore2 + assignmentScore3) / 3;
		std::cout << "Average score is:" << averageScore << endl;

		outputHandler.open("StudentData.txt");
		outputHandler << firstName << lastName << major << assignmentScore1 << assignmentScore2 << assignmentScore3 << std::endl;
	}

after I did this i was going to read from the lines in the txt file to determine the lowest/highest and average score. Would that be an appropriate thing to do?

p.s. if i put the output handler code on the outside of the bracket it gives me the last one of the loop
Last edited on
Please use code tags:

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

std::ifstream inputHandler;
std::ofstream outputHandler;
using namespace std;

int main()
{
int rank;
int assignmentScore1;
int assignmentScore2;
int assignmentScore3;
int averageScore;
int count = 0;


string firstName;
string lastName;
string major;

for (count = 0; count <= 2; count++)
{
// Get student's first name
std::cout << "Enter the student's first name. ";
std::cin >> firstName;

// Get student's last name
std::cout << "Enter the student's last name. ";
std::cin >> lastName;

// Get student's major
std::cout << "Enter the student's major. ";
std::cin >> major;

// Get 1st assignment score
std::cout << "Enter the student's score for assignment 1. ";
std::cin >> assignmentScore1;

// Get assignment score 2
std::cout << "Enter the student's score for assignment 2. ";
std::cin >> assignmentScore2;

// Get assignment score 3
std::cout << "Enter the student's score for assignment 3. ";
std::cin >> assignmentScore3;

//Get average assignment score
averageScore = (assignmentScore1 + assignmentScore2 + assignmentScore3) / 3;
std::cout << "Average score is:" << averageScore << endl;

outputHandler.open("StudentData.txt");
outputHandler << firstName << lastName << major << assignmentScore1 << assignmentScore2 << assignmentScore3 << std::endl;
}


Try putting outputHandler.open("StudentData.txt"); outside the forloop. You're trying to open a file several times--you should only open it once.
I tried putting it outside of the for loop and it only saves the information from the last set of data entered.
Please show your modified 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
#include<iostream>
#include<fstream>
#include<string>

std::ifstream inputHandler;
std::ofstream outputHandler;
using namespace std;

int main()
{
	
	int assignmentScore1;
	int assignmentScore2;
	int assignmentScore3;
	int averageScore;
	int count = 0;


	string firstName;
	string lastName;
	string major;
	string rank;

	
	for (count = 0; count <= 2; count++)
	{
		// Get student's first name
		std::cout << "Enter the student's first name. ";
		std::cin >> firstName;

		// Get student's last name
		std::cout << "Enter the student's last name. ";
		std::cin >> lastName;

		// Get student's major
		std::cout << "Enter the student's major. ";
		std::cin >> major;

		// Get student's rank
		std::cout << "Enter the student's rank. ";
		std::cin >> rank;

		// Get 1st assignment score
		 std::cout << "Enter the student's score for assignment 1. ";
		std::cin >> assignmentScore1;

		// Get assignment score 2
		std::cout << "Enter the student's score for assignment 2. ";
		std::cin >> assignmentScore2;

		// Get assignment score 3
		std::cout << "Enter the student's score for assignment 3. ";
		std::cin >> assignmentScore3;

		//Get average assignment score
		averageScore = (assignmentScore1 + assignmentScore2 + assignmentScore3) / 3;
		std::cout << "Average score is:" << averageScore << endl;

	}
	outputHandler.open("StudentData.txt");
	outputHandler << firstName << lastName << major << rank << assignmentScore1 << assignmentScore2 << assignmentScore3 << std::endl;

I have also tried simply putting: outputHandler << firstName << lastName << major << rank << assignmentScore1 << assignmentScore2 << assignmentScore3 << std::endl;
inside the bracket and leaving :outputHandler.open("StudentData.txt"); outside and that did not work as well.
You need to put your file opening before the loop, but leave the output statement inside the loop.

Also why are you using those global variables. You have one function, main(), so just make them local to that function.

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


using namespace std;

int main()
{
	std::ifstream inputHandler;
        std::ofstream outputHandler("StudentData.txt"); // Open the file here.
	int assignmentScore1;
	int assignmentScore2;
	int assignmentScore3;
	int averageScore;
	int count = 0;


	string firstName;
	string lastName;
	string major;
	string rank;

	
	for (count = 0; count <= 2; count++)
	{
		// Get student's first name
		std::cout << "Enter the student's first name. ";
		std::cin >> firstName;

		// Get student's last name
		std::cout << "Enter the student's last name. ";
		std::cin >> lastName;

		// Get student's major
		std::cout << "Enter the student's major. ";
		std::cin >> major;

		// Get student's rank
		std::cout << "Enter the student's rank. ";
		std::cin >> rank;

		// Get 1st assignment score
		 std::cout << "Enter the student's score for assignment 1. ";
		std::cin >> assignmentScore1;

		// Get assignment score 2
		std::cout << "Enter the student's score for assignment 2. ";
		std::cin >> assignmentScore2;

		// Get assignment score 3
		std::cout << "Enter the student's score for assignment 3. ";
		std::cin >> assignmentScore3;

		//Get average assignment score
		averageScore = (assignmentScore1 + assignmentScore2 + assignmentScore3) / 3;
		std::cout << "Average score is:" << averageScore << endl;
                // Print each record to your file.
                outputHandler << firstName << lastName << major << rank << assignmentScore1 << assignmentScore2 << assignmentScore3 << std::endl;
	}

	
Ah thank you! It works now! However when I try to read the data from the txt file to average all the scores from assgnment 1, it will only read the data from the last set.. Not sure if its a similar solution as my first problem.

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

using namespace std;

int main()
{
	std::ifstream inputHandler;
	std::ofstream outputHandler("Studentdata.txt");

	int assignmentScore1;
	int assignmentScore2;
	int assignmentScore3;
	int averageScore;
	int count = 0;


	string firstName;
	string lastName;
	string major;
	string rank;

	
	for (count = 0; count <= 2; count++)
	{
		// Get student's first name
		std::cout << "Enter the student's first name. ";
		std::cin >> firstName;

		// Get student's last name
		std::cout << "Enter the student's last name. ";
		std::cin >> lastName;

		// Get student's major
		std::cout << "Enter the student's major. ";
		std::cin >> major;

		// Get student's rank
		std::cout << "Enter the student's rank. ";
		std::cin >> rank;

		// Get 1st assignment score
		 std::cout << "Enter the student's score for assignment 1. ";
		std::cin >> assignmentScore1;

		// Get assignment score 2
		std::cout << "Enter the student's score for assignment 2. ";
		std::cin >> assignmentScore2;

		// Get assignment score 3
		std::cout << "Enter the student's score for assignment 3. ";
		std::cin >> assignmentScore3;

		//Get average assignment score
		averageScore = (assignmentScore1 + assignmentScore2 + assignmentScore3) / 3;
		std::cout << "Average score is:" << averageScore << endl;

		outputHandler << firstName << lastName << major << rank << assignmentScore1 << assignmentScore2 << assignmentScore3 << std::endl;
	}
	// Open the txt file
	inputHandler.open("Studentdata.txt");
	inputHandler >> firstName >> lastName >> major >> rank >> assignmentScore1 >> assignmentScore2 >> assignmentScore3;

	// Attemp to average assignments
	std::cout << (assignmentScore1 + assignmentScore1 + assignmentScore1) / 3;
	


Is there a way to choose which students information i take?
What exactly are you trying to do?

If you want to average the assignments after you write the file then you will need to loop through the file.

I am trying to find the minimum/maximum score from each assignment as well as the average score for each assignment.
ex. Student 1 assignment 1 score = 95 - Student 2 assignment 1 score = 78 - Student 3 assignment 1 score = 84 find the lowest grade from assignment 1, and the same for max and average and do it for each of the three assignments
I dont know how to input say the assignment 1 score from each of the three students. I have been playing around with trying to draw the info out based on the students name but its not working. This is my first code I have written so im not sure if this is something complicated or very simple. Maybe I have to do something inside the loop that sets say the first entered assignment score 1 to a variable say assignment1a, then the next person is asignment1b but again im not quite sure if that is possible or the best way. Thanks for the help!
Last edited on
What is the actual assignment?

What is the real purpose of the files? This can probably be done much easier without using files.

I have to make a grade book program where the user enters 3 assignments for 3 students as well as their name class and major then find the average for each student then the average score of the class for each assignment then the lowest and highest score for each assignment and then save it to a txt file. I've never coded before and assumed I should have the user enter all the info for the students and their scores find the average . Save it to a file then draw the information out to find the highest lowest and average . Is there a way to find all of the info first then save it to a txt file???
Is there a way to find all of the info first then save it to a txt file???

Yes, several different ways.

Have you studied vectors or arrays yet?

Have you studied structures or classes yet?



We have covered them but my knowledge on them is pretty minimum, I only did it the way I did because thats all I know how to do. Do you think it would be worth it to start over? I know that code i wrote is pretty simple but it took me quite a bit of time to do...
We have covered them but my knowledge on them is pretty minimum, I only did it the way I did because thats all I know how to do.

The purpose of homework is to reinforce topics you've covered in class. Since you've stated you've studied vectors and structures, then I suggest you rethink your code and and use both of these features to solve this problem.



Ahh okay thank you. Any recommendation on how to figure out how to learn these?
Just try to make different programs to practice.

~ Hirokachi
Okay guys, I re wrote the code using arrays and I have gotten pretty far, my only problem now is when im trying to find the min/max/average of the three assignments my program is only giving me the min/max/average for the last assignment, I have been playing around with it for some time and i can't seem to figure out whats wrong.



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

using namespace std;

int main()
{
	int intDataArray[3] = { 0 };
	int sumTotalArray = 0;
	int maxScore = 0;
	int minScore = 0;
	int count = 0;

	std::string firstNames[3];
	std::string lastNames[3];
	std::string rank[3];
	std::string major[3];

		for (count = 0; count <= 2; count++)
		{
			std::cout << "Enter student's first name:" << count + 1 << ": ";
			std::cin >> firstNames[count];

			std::cout << "Enter student's last name:" << count + 1 << ": ";
			std::cin >> lastNames[count];

			std::cout << "Enter student's rank:" << count + 1 << ": ";
			std::cin >> rank[count];

			std::cout << "Enter student's major:" << count + 1 << ": ";
			std::cin >> major[count];

			std::cout << "Enter student's assignment score 1:";
			std::cin >> intDataArray[count];


			std::cout << "Enter student's assignment score 2:";
			std::cin >> intDataArray[count];

			std::cout << "Enter student's assignment score 3:";
			std::cin >> intDataArray[count];

			sumTotalArray = sumTotalArray + intDataArray[count];

			if (count == 0)
			{
				maxScore = intDataArray[count];
				minScore = intDataArray[count];
			}
			else
			{
				if (intDataArray[count] > maxScore)
				{
					maxScore = intDataArray[count];
				}
				

				if (intDataArray[count] < minScore)
				{
					minScore = intDataArray[count];
				}
			}

		}

		std::cout << "Array total is: " << sumTotalArray << std::endl;
		std::cout << "The average of the number is: " << sumTotalArray / 3 << std::endl;
		std::cout << "Max score is: " << maxScore << std::endl;
		std::cout << "Min score is: " << minScore << std::endl;

		return 0;

		}

	
I know you don't know what's wrong, but at this point we don't know what's wrong either.

What kind of error are you seeing?

Are you seeing an unexpected output or the code produces an error?

Hope to help,

~ Hirokachi
Last edited on
using namespace std;
but you have std::string, std::cout and std::cin.

sumTotalArray = sumTotalArray + intDataArray[count];
can be just
sumTotalArray += intDataArray[count];

sumTotalArray / 3
Integer division will truncate the fractional part.
sumTotalArray / 3.0

Prefer "\n" over std::endl.
Topic archived. No new replies allowed.