Core Dump Problem

Hello! I am trying to output the highest score of a particular player. It runs well until it hits the last part of the code when I am trying to isolate the biggest number. More specifically, if I delete the last "paragraph" of the code (lines 93-104) and I run the program, it runs fine but when I add the last part, it says "Segmentation fault (core dumped)". Any suggestions?

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <sstream>
using namespace std; 

struct User
{
	string name; 
	string score[11];
	int k[11];  
};

struct UserImitate
{
	string nameImitate; 
	int scoreImitate[11]; 
}; 

int main ()
{
	User player[11]; 
	UserImitate playerImitate[11]; 
	int numberOfScores[11]; 
	string userInput; 
	bool enterAnotherName = true;
	int playerScore = 0; 
	int i = 1; 
	int k = 1; 

	while (enterAnotherName)
	{
		cout << "Enter the name of the player and his or her scores. When there are no more players to add, " 
		<< "type 'done' to go to the next set of options." << endl; 
		cin >> userInput; 
		cout << endl;
		if (userInput == "done")
		{
			enterAnotherName = false; 
		}
		else
		{
			player[i].name = userInput; 
			bool enterScore = true; 
			while (enterScore)
			{
				cout << "Enter " << player[i].name << "'s score(s). Once done, write 'done' and press enter." 
				<< endl;
				int j = 1; 
				bool enterAnotherScore = true;
				while(enterAnotherScore)
				{	
					cin >> userInput; 
					if(userInput == "done")
					{
						enterAnotherScore = false; 
					} 
					else
					{
					player[i].score[j] = userInput;
					j++;
					numberOfScores[k] = j - 1; 
					}
				}
				cout << endl;
				enterScore = false; 
				k++; 
			}
			i++; 
		}
	}

	for (int q = 1; q <= i; q++)
	{
		for(int t = 1; t <= numberOfScores[q]; t++)
		{
			stringstream(player[q].score[t]) >> playerImitate[q].scoreImitate[t]; 
		}
	}

	cout << "Whose high score would you like to see? " << endl;
	cin >> userInput; 
	int specificPlayer = 0;
	for (int q = 1; q <= i; q++)
	{
		if (userInput == player[q].name)
		{
			specificPlayer = q;
		}
	}

	int biggestIndex = 1; 
	int testIndex = 2; 
	while(testIndex <= numberOfScores[specificPlayer])
	{
		if (playerImitate[specificPlayer].scoreImitate[biggestIndex] 
		< playerImitate[specificPlayer].scoreImitate[testIndex])
		{
			biggestIndex = testIndex; 
		}
		testIndex++; 
	}
	cout << biggestIndex; 

return 0; 
}
 
Last edited on

You initialise i to 1, Arrays start at 0, you also setup 11 objects yet your while loop will continue until you type done allowing your code to go past 11 objects (0-10), same with k.

There may be other issues but thats what stands out.

To sort the highest to lowest scores you could have used a bubble sort in descending order, something like this..

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56



#include <iostream>
#include <string>

// number of names
const int MAX_USERS = 2;
// number of scores each name
const int MAX_SCORES = 5;

struct User
{
	std::string name;
	int scores[MAX_SCORES];
	// bubble sort (decending order)
	void Sort() {
		for (int i = 0; i < MAX_SCORES - 1; i++)
			for (int j = 0; j < MAX_SCORES - 1; j++)
				if (scores[j + 1]>scores[j])
				{
					int temp = scores[j];
					scores[j] = scores[j + 1];
					scores[j + 1] = temp;
				}
	}
};


int main()
{
	User Scoreboard[MAX_USERS];

	// read in the names and scores.
	for (int i = 0; i < MAX_USERS; i++) {
		std::cout << std::endl << "Enter Name: ";
		std::cin >> Scoreboard[i].name;
		for (int j = 0; j < MAX_SCORES; j++) {
			std::cout << "Enter Score " << j + 1 << ": ";
			std::cin >> Scoreboard[i].scores[j];
		}
		// sort them
		Scoreboard[i].Sort();
	}

	// list them
	for (int i = 0; i < MAX_USERS; i++) {
		std::cout << std::endl << "Name: " << Scoreboard[i].name << std::endl;
		for (int j = 0; j < MAX_SCORES; j++) {
			std::cout << Scoreboard[i].scores[j] << std::endl;
		}
	}

	return 0;
}

Enter Name: Paul
Enter Score 1: 19
Enter Score 2: 566
Enter Score 3: 891
Enter Score 4: 32
Enter Score 5: 109

Enter Name: John
Enter Score 1: 588
Enter Score 2: 4711
Enter Score 3: 488
Enter Score 4: 8
Enter Score 5: 4959

Name: Paul
891
566
109
32
19

Name: John
4959
4711
588
488
8
Wow, thank you so much. It took me about 5 hours to write that code. And it only took you like 10 minutes. So awesome!

I know that arrays begin at 0 and I begin accessing it at 1. Is that bad coding? The reason why was I thought it would be awkward to have player 0. So if I were to ever to do this: cout << "player " << i << endl; it would not print "player 0" but rather "player 1".

Also, why don't you use namespace? It seems that many people don't use it and I'm not sure why. Anyhow, let me know!

Further, I knew that was the problem with my code, going past beyond the allocated array. However, I made sure that it never went passed the allocated amount. So I'm not sure why the last paragraph of my code is not working. Any suggestion on that?

Thank you so much for writing that code for me. I'll probably take the next 2 hours understanding and internalizing your code. Thank you!!!
Many people don't use using namespace basically because many functions or classes can have names which are fairly generic and so may be unwittingly duplicated by the user as for example a user defined function. This is where namespaces come in as they separate these functions and tell the compiler explicitly which one is which. Using namespace takes away this safety.
Topic archived. No new replies allowed.