Trouble finding problem. Please help.

Alright, so I have coded this program so far and it compiles just fine. It crashes after reading user input for the 10th score, so after the 1st FOR loop.

I can't seem to find the issue and I'm not yet done with the program, I have yet to figure out how to out put the scores descending and the names alphabetically.

So, here is the 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
40
#include "stdafx.h"
#include <iostream>
#include <istream>
#include <string>
#include <algorithm>
#include <cstdlib>

using namespace std;

int main()
{
	string player_names[10];  //Array for player names                              
	string player_scores[10]; //Array for player scores
	string nameIN;            //variable to hold name
	string scoreIN;           //variable to hold score

for( int i=1; i < 11; i++)  //For loop
{
	cout << "Please enter name and score for Player " << i << "\n" << endl; //Ask user to input name and score
	cout << "Name: \n";
	cin >> nameIN;
	cout << "Score: \n";
	cin >> scoreIN;
	
	player_names[i] = nameIN+" "+scoreIN;   //To store name, then score
	player_scores[i] = scoreIN+" "+nameIN;  //To store score, then name
	
}

sort(player_scores, player_scores+10); //Sorting

for( int i=10; i>=0; i--)
{
	cout << player_scores[i] << "\n";
}

system("pause");

return 0;
}


Yes, I realize that I initialized scoresIN as a string instead of integer, this is because I can't put an integer array into a string array. This is so I can sort them later.

Any help, suggestions, or opinions are greatly appreciated!!!
Last edited on
Indexing into an array starts at 0 and finishes to 9 (10 elements)
when going backwards indexing starts at last element 9 to 0
Alright, that confuses me quite often. I'll fix that and get back to you.

Thanks a lot.

**EDIT**

Ok, I changed both FOR loops accordingly and it still crashes?

1
2
3
for( int i=1; i < 11; i++) //this is right, correct? For 10 iterations?

for( int i=9; i>=0; i--) // changed this accordingly 
Last edited on
First loop is incorrect:
 
for( int i=1/*indexing starts*/; i < 11/*indexing ends*/; i++)

look my first post.
Second loop is ok.
Alright, so it should be this?

 
for( int i=1; i <= 10; i++)


Sorry, I'm new to all this. Ive tried it like this but it still crashes.
Ive been working on this for 2 days and can't find the issue.
1
2
3
4
for(
int i=0; //start at 0
i < 10;//finishes at 9
 i++)
Yea, so when I initialize i=1, it should be coded as such:

1
2
3
for(
int i=1; //starts at 1
i < 11; //finishes at 10 


Correct? That shouldn't be the reason it's crashing then?

I'm so stuck!
BUMP
Yes, that is why is crashing!
I'm sorry if I'm frustrating you, just trying to understand.

If I want the loop to iterate 10 times and I initialize i to 1, shouldn't I have the parameter as less than 11 ( i < 11 ).

If not, why and what am I doing wrong?
Yes, but you are skipping first element.

read this:
http://www.cplusplus.com/doc/tutorial/arrays/
OOOOOHHHHH. Alright.

You see, on a different forum, someone asked me why I didn't just intialize i to 1 instead of using:

 
cout << " Please enter player name << i+1; 


So I changed it. I take it they were wrong in suggesting that then?
If array is declared as
 
T array[n];
you can access array[i] where 0>=i<n.
You're trying to access array[j] where 1>=j<=n.
Alright. I have it now and it is working as is. Thank you so much for your time and patience.

Now, before I mark as solved, can anyone give me a hint on how to add a sort() at the end to print the player_names[] alphabetically?

Thanks a lot.
Last edited on
You need to provide function for sort:
1
2
3
4
5
6
7
bool alpha(const string& left, const string& right)
{
	return left[0] < right[0];//test first letter of a names
}
...
sort(names, names + 10, alpha);
...
Alright. I'll mess with it. Thanks a lot.

**EDIT**

Alright, I've read and reread your code, but I'm not sure I quite get it.

How do I implement this into my code?
Last edited on
BUMP
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
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool alpha(const string& left, const string& right)
{
	return left[0] < right[0];//test first letter of a names
}

int main()
{
	const unsigned short numPlayers = 4;
	string player_names[numPlayers];  
	player_names[0] = "George";
	player_names[1] = "Abbaf";
	player_names[2] = "Joe";
	player_names[3] = "Hannibal";

	cout << "---------------------------------" << endl;
	for(unsigned short i = 0; i < numPlayers; ++i)
	{
		cout << player_names[i] << endl;
	}

	sort(player_names, player_names + numPlayers, alpha);

	cout << "---------------------------------" << endl;
	for(unsigned short i = 0; i < numPlayers; ++i)
	{
		cout << player_names[i] << endl;
	}


	cout << "Press enter to exit...";
	cin.get(); 
	return 0;
} 
Last edited on
@savavampir

You've helped me out a great deal. It is much appreciated.

Take care.
Topic archived. No new replies allowed.