Writing to a File // Repeats itself?

Apr 17, 2019 at 3:34am
Hi! I am trying to write this information to a file (I am not worrying about the actual formatting right at this moment) and after I coded the outFile << [whatever code goes here], it repeated itself and seems to still output to the screen. For this lesson in particular, I wasn't able to attend the lecture, so I know I am not writing to the file correctly. Any tips or ideas on how to fix this would be amazing!



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

using namespace std;

int main()
{
	int userInput, userGrade, count = 1;

	ofstream outFile;

	outFile.open("userGrades.txt");

	cout << "How many scores would you like to read? : ";
	cin >> userInput;

	cout << "Enter your grade for assignment " << count << ": ";
	cin >> userGrade;
	outFile << "Enter your grade for assignment " << count << ": ";
	outFile << userGrade;

	cin >> userGrade;


	for (count = 0; count <= userInput; count++)
	{
		if (userGrade < 0 || userGrade > 100)
		{
			cout << "Invalid score. Please enter again: ";
			cin >> userGrade;
		}
		else
		{
			cout << "Enter your grade for assignment " << count << ": ";
			cin >> userGrade;
		}
	}

	for (count = 0; count <= userInput; count++)
	{
		if (userGrade < 0 || userGrade > 100)
		{
			outFile << "Invalid score. Please enter again: ";
			outFile << userGrade;
		}
		else
		{
			outFile << "Enter your grade for assignment " << count << ": ";
			outFile << userGrade;
		}
	}

	
	outFile.close();




	return 0;


//Here is the Output:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
How many scores would you like to read? : 12
Enter your grade for assignment 1: 12
12
Enter your grade for assignment 0: 12
Enter your grade for assignment 1: 12
Enter your grade for assignment 2: 12
Enter your grade for assignment 3: 12
Enter your grade for assignment 4: 12
Enter your grade for assignment 5: 12
Enter your grade for assignment 6: 12
Enter your grade for assignment 7: 12
Enter your grade for assignment 8: 12
Enter your grade for assignment 9: 12
Enter your grade for assignment 10: 12
Enter your grade for assignment 11: 12
Enter your grade for assignment 12: 12


// This is what it should be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
How many scores would you like to read? : 12
Enter your grade for assignment 1: 12
Enter your grade for assignment 2: 12
Enter your grade for assignment 3: 12
Enter your grade for assignment 4: 12
Enter your grade for assignment 5: 12
Enter your grade for assignment 6: 12
Enter your grade for assignment 7: 12
Enter your grade for assignment 8: 12
Enter your grade for assignment 9: 12
Enter your grade for assignment 10: 12
Enter your grade for assignment 11: 12
Enter your grade for assignment 12: 12



Apr 17, 2019 at 5:08am
Set count = 2.. fixed it..
Apr 17, 2019 at 7:20am
count is 0 at start
WHILE ( count < userInput && cin >> grade )
{
  IF grade is valid
  {
    // we have got a grade
    ++count; // we have got count valid grades show far
    outFile << grade << '\n'; // store the result
  }
}
Apr 17, 2019 at 5:26pm
Thanks! This seemed to work:

1
2
3
4
5
6
7
8
9
10
11
12
	while (count <= userInput)
	{
		cout << "Enter your grade for assignment " << count << ": ";
		cin >> userGrade;
		outFile << "assignment " << count << " ";
		outFile << userGrade << endl;

		if (userGrade >= 0 && userGrade <= 100)
		{
			count++;
		}
	}
Last edited on Apr 17, 2019 at 5:26pm
Apr 17, 2019 at 5:42pm
Consider input data:
7 101 42 boo 4

The output file will get:
assignment 1 7
assignment 2 101
assignment 2 42
assignment 3 
...

Reading "boo" into integer fails and the stream will be in error state.
No further read will succeed, unless you clear the error.

If 101 is not valid, why do you write it into the output?


You could test the success of each read and write only good values out:
1
2
3
4
5
6
7
8
9
10
cout << "Enter your grade for assignment " << count << ": ";
while ( count <= userInput && cin >> userGrade ) // loop ends at "boo"
{
  if (userGrade >= 0 && userGrade <= 100)
  {
    outFile << "assignment " << count << ' ' << userGrade << '\n';
    ++count;
  }
  cout << "Enter your grade for assignment " << count << ": ";
}
Topic archived. No new replies allowed.