Dynamic Allocation High scores

Hello all! I'm hoping you guys can help me figure out what's wrong with my code. The output should look like


How many scores will you enter?: 5
Enter the name for score #1: Suzy
Enter the score for score #1: 9900
Enter the name for score #2: Kim
Enter the score for score #2: 1000000
Enter the name for score #3: Bob
Enter the score for score #3: 1012
Enter the name for score #4: Armando
Enter the score for score #4: 822
Enter the name for score #5: Tim
Enter the score for score #6: 514
Top Scorers:
Suzy: 9900
Kim: 1000000
Bob: 1012
Armando: 822
Tim: 514
The highest of the high-scores is Suzy, with 1000000


But my output looks like:


How many scores will you enter?: 
5
Enter the name for score # 1
Anne
Enter the score for # 1
Enter the name for score # 2
Enter the score for # 2
Enter the name for score # 3
1200
Enter the score for # 3
Enter the name for score # 4
Enter the score for # 4
Enter the name for score # 5
13
Enter the score for # 5
Top Scorers: 
3:3


I'm not sure why it's not letting me enter it for all of them. I tried some stuff from the page on dynamic allocations, but it doesn't seem to change anything. Any help is appreciated! Thanks!

My 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
33
34
35
36
37
38
39
#include <iostream>
#include<new>

using namespace std;

int main ()

{ 

	char *name;
	int score;
	int count;

	cout << "How many scores will you enter?: " << endl;
	cin >> score;
	
	name = new char[score];
	
	for (count=0; count < score; count++)
	{ 

	cout << "Enter the name for score # " << (count +1) << endl;
	cin >> name[count];

	cout << "Enter the score for # " << (count +1) << endl;
	cin >> name[score];
	
	}

	cout << "Top Scorers: " << endl;
	for (count=0; count < score; count++);
	cout << name[count] << ":" << name[score];

	delete [] name;
	

return 0;

}
If you enter 5 for score you're only allocating enough space to store 5 chars.

cin >> name[count]; is extracting one character from the input stream because name[count] is a char and not a c-string/char array.

cin >> name[score]; is modifying memory you don't own.

In this case you would need: an array of pointers to pointers to char. You would need to allocate enough memory for the array of pointers (to pointers) Then you would need to go through and allocate memory for each pointer in the previously allocated array to point to.

And that only handles the names. You'll also need somewhere to store the actual scores.

Topic archived. No new replies allowed.