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!
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.