Finding the Lowest and Highest Score of User Input

Hi. I'm trying to find the Highest and Lowest value of testscores that I input them program. Along with the Students name next to the score when you figure out the highest and lowest 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
40
#include <iostream>
using namespace std;

int main()
{
	double scores[6];
	int counter = -1;
	
	do
	{
		counter++;
		cout << "Please enter a score, then a name (enter -1 to stop both name and score entries): ";
		cin >> scores[counter];
		cout << "Please enter students name: ";
		char student[20];
		cin >> student;
		
	}
	
	while (scores[counter] >= 0);
	
	double total = 0, average = 0, min = 101, max = 0;
	for (int x = 0; x < counter; x++)
	{    
		total += scores[x];                 
		if (scores[x] > max)        
			max=scores[x];
		if (scores[x] < min)        
			min=scores[x];
	}    
	
	average = total/counter;    
	char student[20];
	cout << "The average is " << average << endl;    
	cout << student << " has the highest score " << max << endl;    
	cout << student <<"has the lowest score " << min << endl;

	system("pause");
	return 0;
}

Any help would be greatly appriciated.
Last edited on
line 33 declares an uninitialized char[] and line 35 tries to use that uninitialized char[]...

line 15 goes out of scope when the while loop finishes...

it'd be better to use an array of strings to store each student's name
char student[20]; only stores 1 name which gets overwritten each time that while loop goes through
Last edited on
Use an array of std::pairs of doubles and std::strings(in that order, I know what I'm talking about, trust me!), and then just do
1
2
cout<<min_element(scores, scores+6)->second<<" "<<min_element(scores, scores+6)->first<<endl;
cout<<max_element(scores, scores+6)->second<<" "<<max_element(scores, scores+6)->first;
Last edited on
A Little Program Of Mine That Tells That!(:
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
#include <iostream>
using namespace std;
int main()
int score;
int score2;
int score3;
int score4;
cout <<"Enter Four Scores';
cin >>score;
cin >>score2;
cin >>score3;
cin >>score4;
if (score<score2&&score3&&score4){
cout <<score;
}
elseif (score2<score&&score3&&score4){
cout <<score2;
}
elseif (score3<score&&score2&&score4){
cout <<score3;
}
elseif (score4<score&&score3&&score2){
cout <<score4;
}
return 0; 
Topic archived. No new replies allowed.