Averages from a file

Really Stumped!!! Not even sure where to start this is my first attempt at something like this and little info from the videos for a program like this. Here are are the parameters I need to follow not sure how to implement the file?:
2. Use a nested loop structure to read the data from the text file and calculate the student’s average grade.
a. The outer look will be a while loop; the inner loop will be a for loop (4 quizzes)
b. To calculate each student’s average score, use a total variable initialized to 0 before the for loop, then calculate the student’s average after the loop.
c. To calculate the class average, initialize a classTotal variable to 0 before the while loop, add each student’s total into the classTotal following the for loop, then calculate the classAverage after the while loop.
d. Only display 2 decimals for the averages.
3. BEWARE of integer division.
4. Use cout to output the values of the variables to the console. Your cout statement MUST use the variables to display the values
5. Output must be labelled and easy to read as shown in the sample output below.

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



using namespace std;

int main()
{

    int x, y, studentID, grade1, grade2, grade3, grade4, average;
    int total = 0;
    char again = 'Y';

    ofstream file;
    file.open("grade.txt");

    while ( again == 'Y' || again == 'y')
    {
    for (x = 0; x < 1; x++)
    {//enter grades
    cout << "Enter student ID#:" << endl;
    cin >> studentID;
    cout << "Enter grade: " << endl;
    cin >> grade1;
    if (grade1 <0 || grade1 > 100)
    {
        cout << "0 - 100 only! Enter again: " << endl;
        cin >> grade1;
    }
    else if (grade1 <= 0 || grade1 >= 100)
    {
        cout << endl;
    }
    cout << "Enter grade: " << endl;
    cin >> grade2;
        if (grade2 <0 || grade2 > 100)
    {
        cout << "0 - 100 only! Enter again: " << endl;
        cin >> grade2;
    }
    else if (grade2 <= 0 || grade2 >= 100)
    {
        cout << endl;
    }
    cout << "Enter grade: " << endl;
    cin >> grade3;
        if (grade3 <0 || grade3 > 100)
    {
        cout << "0 - 100 only! Enter again: " << endl;
        cin >> grade3;
    }
    else if (grade3 <= 0 || grade3 >= 100)
    {
        cout << endl;
    }
    cout << "Enter grade: " << endl;
    cin >> grade4;
        if (grade4 <0 || grade4 > 100)
    {
        cout << "0 - 100 only! Enter again: " << endl;
        cin >> grade4;
    }
    else if (grade4 <= 0 || grade4 >= 100)
    {
        cout << endl;
    }


    //file imput
    file << studentID << " " << grade1 << " " << grade2 << " " << grade3 << " " << grade4 << endl;
    }//stop or continue?
    cout << "Enter Y/N to enter another student: " << endl;
    cin >> again;


    }//while loop ends

    while (average <= 0 && average >= 0)
    {
        for (y = 0; y < average ; y++)
        {
         
        }



    }
    cout << "OK, we will stop here." << endl;
    return 0;
}
Last edited on
Read from file, not from cin. Looks like you have started from code of a different program.


What is the difference between these bits of your code:
1
2
cin  >> studentID;
file << studentID;



What does the file actually contain? Just four valid scores per student, or studentID too?

The number of students in the file is unknown. Hence the outer while loop.



You could, for starters, write a different program that reads four scores from cin (without validation), and calculates sum and average for them.
No, I am supposed to send to a text file and then read from text file to get results, I cannot find anything closely enough related to figure it our in my own. As far as reading and using data from the text file?
Last edited on
Reading from a stream (file) is no different from reading from a stream (cin).
Ok so I ifstream ? but other than that I am completely lost I can find nothing to tell me how?
Every problem can usually be broken into smaller, manageable steps.
You could, for starters, write a different program that reads four scores from cin (without validation), and calculates sum and average for them.

Yes, I wouldn't have any problem doing that either. But this is required and I have to read and write from text file. Online class and no interaction so I cannot ask there?
If you can do it, then do it. You will need most of that code in this assignment.
Sorry, but that is totally off of the topic even bro. I asked how to convert the text file to a usable variable such as total or average ?
1
2
3
4
5
6
7
8
9
10
int score;

// this
std::cin >> score;

// or this
std::ifstream infile( "grade.txt" );
infile >> score;

// use score 

Replace "cin" in the code with "infile".
I am so lost on this. The scores are correctly written in the text file. I am being asked to use the data from the text file to calculate averages, class average, and total. I know ifstream is the open file command. The way you have this wrote doesn't look like it will make the saved text a variable, unless maybe its reannouncing variable score. Sorry total noob. I will try to play around with it. thank you
I made some progress but I cannot get it to function even. I'm sorry this is very bad I am still trying. cpp.sh/3bsg
So, there are two files? What is each one supposed to contain, and what should be done with that data?
the grades are stored in the text in the first loop. the second loop reads the grades from the text and then averages them. I think I have it in theory but the second loop fails to run? I am noob
this is current state of program cpp.sh/4xnp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    }//while loop ends
  file.close();  // You should to close the output file before you start reading from that file

  // 2c "initialize a classTotal variable to 0 before the while loop"
    while ( score < 0) // What condition is this?
    {
    // The file contains studentID before scores. What to do about it?
    // 2b "use a 'total' variable initialized to 0 before the for loop"
        for (x = 0; x < 0; x++) // Again, how many times do you loop here?
        {
         ifstream file ("grade.txt"); // This cannot be inside the loop. Not even inside the while-loop
         file >> score;
         cout << "Average is: " << score / 4 << endl; // 2b "calculate the student’s average after the loop"
         score++; // Why?
        }
    // 2c "add each student’s total into the classTotal following the for loop"
    }
  // 2c "calculate the classAverage after the while loop" 
Aha! Great help ther must close. I am doing an online class with no interaction so many times I am left with questions about these very simple things. Hope I am not bother and thank you again.
Topic archived. No new replies allowed.