help with finding average

Ok so, I'm fairly new to C++ language and I am learning the best I can. I had an in class assignment to write a code that prompts the user for students (unknown number... assuming a loop) first name, last name, student number,5 test scores for each student, average their grade, then find the average of the class and display this data. I think I'm putting too much thought into it but, does this need to be an array or can a simple while loop work? The instructor was writing the code for us, but the way he was writing it would end up that the program would have to executed each time a new student needed to be entered and I do not believe the class average would ever be shown. Here is what I have so far. If you notice at the end of my function, I'm not sure why I'm getting an error at my return 0; statement. I'm not sure how to store the values each time it loops to average each students grade and then store each students grade to get the class average.

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

void scores();

string fName, lName;
double studAverage, classAverage, score1, score2, score3, score4, score5;
double studTotal = score1 + score2 + score3 + score4 + score5, counter = 0;
double studAverage = studTotal / 5, classAverage = studAverage / counter;
char studNum[10];
int students = 0;

using namespace std;
int main()
{
	
	cout << "How many students will you be entering data for?\n";
	cin >> students;

	while (students >= counter + 1)
	{
	counter++;
	cout << "Please enter the first name for student #" << counter << ".\n";
	cin >> fName;
	cout << "Please enter the last name for student #" << counter << ".\n";
	cin >> lName;
	cout << "Please enter the student ID number for student #" << counter << ".\n";
	cin >> studNum[10];
	scores();
			
	}
   return 0;
}

void scores()
{
cout << "Please enter the first score for student #" << counter << ".\n";
cin >> score1;
cout << "Please enter the second score for student #" << counter << ".\n";
cin >> score2;
cout << "Please enter the third score for student #" << counter << ".\n";
cin >> score3;
cout << "Please enter the fourth score for student #" << counter << ".\n";
cin >> score4;
cout << "Please enter the fifth score for student #" << counter << ".\n";
cin >> score5;
return 0;
}
1
2
3
4
5
6
7
8
9
10
cout << "Please enter the first score for student #" << counter << ".\n";
cin >> score1;
cout << "Please enter the second score for student #" << counter << ".\n";
cin >> score2;
cout << "Please enter the third score for student #" << counter << ".\n";
cin >> score3;
cout << "Please enter the fourth score for student #" << counter << ".\n";
cin >> score4;
cout << "Please enter the fifth score for student #" << counter << ".\n";
cin >> score5;
stuff like this you want to use an array and a for loop.

this can be rewrote as:

1
2
3
4
5
6
7
8
void scores()
{
    for( int i = 0; i < scores; ++i ) //in this case scores == 5
    {
        cout << "Please enter score" << i + 1 <<" for student #" << counter << ".\n";
        cin >> score[i];
    }
}
also generally speaking you want to avoid using global variables and instead pass them to the function.

I see some other problems too:

1
2
double studTotal = score1 + score2 + score3 + score4 + score5, 
double studAverage = studTotal / 5, classAverage = studAverage / counter;


You are trying to do stuff with undefined variables. Think about this math problem:
A = B + C What is A? We do not know since we don't have values for B and C yet.

I would also suggest indenting inside of each set of {braces}.

As far as average scores. You can add them up and divide by how many scores there were.

I would recommend using class/struct to organize.

1
2
3
4
5
6
7
8
9
10
11
struct Student
{
    static constexpr int NumberOfScores = 5;
     //or if You want I think this compiles in c++11 ----
    // const int NumberOfScores = 5;
    // I would also make this a private data member
    double Scores[NumberOfScores];
    double AverageScore;
    int ID;
    std::string Firstname , Lastname;
};


Then we can create a student like:

 
Student fred;


Or an array if you wish to save all the information.

To call the member variables you will do fred.FirstName = "Fred"; ect...


I appreciate the advice and the speedy response. I apologize for my lateness in replying. I'll give your advice a try and see what I come up with. I'll post what I come up with. Hopefully I get it to work.
Topic archived. No new replies allowed.