Please help!! ***

Hey all,

New to programming here and doing a project for class.

Is it possible to get an answer from a loop statement and cout it?

I'm creating a grade book program that gets the average test score of the students. The question obtaining the average score is a loop, is there a way to get the average answer from the loop and use it in a cout statement?

I guess what i am asking is what is the variable that i use for the cout statement to get that average test score since it is looped?

Student 1 average is ****
Student 2 average is ****

Not sure if i am wording this correctly. Any assistance would be greatly appreciated
Last edited on
1
2
3
4
5
int average = 0;
while (/*...*/) {
    average = //Calculate average
}
std::cout << average;
I have the loop part correctly.

What i need to do is format the cout in a way where it says

Student Average 1 is ***
Student Average 2 is ***

Class average is ***

How do i get cout to display the 2 and 3rd student average?
1
2
3
4
5
6
7
8
9
10
11
12
double student1_average = 0;
double student2_average = 0;
double student3_average = 0;
while (/*...*/) {
    student1_average = //Calculate average
    student2_average = //Calculate average
    student3_average = //Calculate average
}
std::cout << "Student Average 1 is " << student1_average << '\n';
std::cout << "Student Average 2 is " << student2_average << '\n';
std::cout << "Student Average 3 is " << student3_average << '\n';
std::cout << "Class average is " << (student1_average  + student2_average + student3_average) / 3<< '\n';
I guess I'm not asking the questions correctly. This is the code i have so far to ask for test scores

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
cout << "What is the teachers name: ";
    getline(cin, teacher_name);
    cout << "What is the class designation: ";
    getline(cin, class_desig);
    cout << "How many students are in the the class? ";
    cin >> numStudent;
    cout << "How many test scores does each student have? ";
    cin >> numTest;

    for (int student = 1; student <= numStudent; student++)

    {

        total = 0;
        for (int grade = 1; grade <= numTest; grade++)

        {
            double score;
            cout << "Enter grade " << grade << " for ";
            cout << "student " << student << ": ";
            cin >> score;
            total += score;
        }


        average = total / numTest;
        cout << "The average score for the student " << student;
        cout << " is " << average << "\n";

        if (average >= A_Grade)
            cout << "Grade: A \n";
        else if (average >= B_Grade)
            cout << "Grade: B \n";
        else if (average >= C_Grade)
            cout << "Grade: C \n";
        else if (average >= D_Grade)
            cout << "Grade: D \n";
        else
            cout << "Grade: F \n";



What I am trying to do is after this code is to create a template

That says

Teacher Name
Class Name

Student 1 Average is ***
Student 2 Average is ***
Class Average is ***


I cant figure out how to type a cout and to display student 1 average?

Am i going about to code wrong?
Store averages for all students in container for later retrieval:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::vector<int> averages;
for(/*all students*/) {
    /*get grades*/
    average = total / numTest;
    averages.push_back(average);
    cout << "The average score for the student " << student;
    //...
}
std::cout << teacher_name << '\n' <<
             class_desig << '\n';
double class_total = 0;
for(int i = 0; i < averages.size(); ++i) {
    std::cout << "Student " << i+1 << " average is " << averages[i] << '\n';
    class_totat += averages[i];
}
std::cout << "Class average is " << class_total / averages/size() << '\n';
Topic archived. No new replies allowed.