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