Storing names and displaying their scores

Hello, I''m stuck on a problem for my programming class. The question reads "Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a for loop." After completing the problem replace the for loop with a while loop." It also gives me an example of data to input:

Test Data: Nick Foles 70 Carson Wentz 80 Corey Clement 90 Darren Sproles 75

Any help and or tips would be greatly appreciated. Thank you. I got as far as I could but I'm still pretty stumped on what data types to use and how to put a loop within a loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int numOfStudents, numOfScore;
	string nameOfStudents;


		cout << "Enter the number of students:" << endl;
		cin >> numOfStudents;
		cout << "Enter each students name, (seperate first and last names with underscores) after that press enter and enter their score:" << endl;
		cin >> nameOfStudents;
		cout << "What's their score?" << endl;
		cin >> numOfScore;

		for (int i = 0; i < numOfStudents; i++) {
	}
}
The normal way would be to use a vector of structs, but I'm not sure what you are allowed to use.
1
2
3
4
5
6
7
struct Student
{
  string name;
  int score;
};

vector<Student> students;
Last edited on
As far as I know we're allowed to use anything we know, but I haven't learned structs or vectors. I guess I'll have to learn about them on YouTube if it makes this any easier for me.
@whalrus

You don't really need to use a vector, since you aren't keeping the list of student names and their scores. You only need to be checking the scores and how it relates to each input.

This program will show high score, the name of the student and the average of the scores. I'll leave it to you to figure out the top score, average difference.

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

using namespace std;

int main()
{
	int numOfStudents, numOfScore, high_score=0, total_score = 0;
	string nameOfStudents, name;
	float Average_score=0.0;


	cout << "Enter the number of students:" << endl;
	cin >> numOfStudents;
	for (int i = 0; i < numOfStudents; i++) 
	{
		cout << "Enter each students name, (separate first and last names with underscores) after that press enter and enter their score:" << endl;
		cin >> nameOfStudents;
		cout << "What's their score?" << endl;
		cin >> numOfScore;
		total_score+=numOfScore;
		for(int x=0;x<i;x++)
		{
			if(numOfScore>high_score)
			{
				high_score = numOfScore;
				name = nameOfStudents;
			}
		}
	}
	Average_score=total_score/numOfStudents;
	cout << "Average score is " << Average_score << endl;
	cout << "Student with highest score is " << name << " with a score of " << high_score << "." << endl;
}
Last edited on
I don't see a need for nested loops since we compare the current score with the current highscore only.
Here's a example based on whitenite1's code
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
  int numStudents, currentScore, high_score = 0, total_score = 0;
  string name, currentName;
  float averageScore = 0.0;


  cout << "Enter the number of students:" << endl;
  cin >> numStudents;
  for (int i = 0; i < numStudents; i++)
  {
    cout << "Enter each students name, (separate first and last names with underscores) after that press enter and enter their score:" << endl;
    cin >> currentName;
    cout << "What's the score?" << endl;
    cin >> currentScore;
    total_score += currentScore;
    if (currentScore > high_score)
    {
      high_score = currentScore;
      name = currentName;
    }

  }
  averageScore = total_score / numStudents;
  cout << "Average score is " << averageScore << endl;
  cout << "Student with highest score is " << name << " with a score of " << high_score << "." << endl;
}



Enter the number of students:
4
Enter each students name, (separate first and last names with underscores) after
 that press enter and enter their score:
Nick_Foley
What's the score?
70
Enter each students name, (separate first and last names with underscores) after
 that press enter and enter their score:
Carson_Wentz
What's the score?
80
Enter each students name, (separate first and last names with underscores) after
 that press enter and enter their score:
Corey_Clement
What's the score?
90
Enter each students name, (separate first and last names with underscores) after
 that press enter and enter their score:
Darren_Sproles
What's the score?
75
Average score is 78
Student with highest score is Corey_Clement with a score of 90.


I guess I'll have to learn about them on YouTube if it makes this any easier for me.

Be careful, there is a lot of crap on YouTube.
Maybe just wait until they are covered in your class.
@Thomas1965

I don't see a need for nested loops

You're right. There isn't. I'm just so used to checking values in arrays, that I used the loop. But in reality, there wasn't anything actually there to check, since previous values aren't being kept.
Topic archived. No new replies allowed.