I think I am having trouble moving a string value from one array to another.

Currently teaching myself c++ by working though Jumping into C++. The problem that has me stuck currently is entering and keeping track of high scores. Part of the problem is to display all scores of a specific name. I am trying to generate a list of user names without duplicates, so the user won't have to guess names. The list of user names is not showing anything. I am fairly certain that the problem is in the allScoresUser function.

Any help that any of you can offer me is greatly appreciated.

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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  //Score Tracker

#include <iostream>
#include <string>

using namespace std;

struct hiScores{
	string user;
	int score;
};

void enterNewScore(hiScores records[50], int index);
void allScoresUser(hiScores records[50], int index);
void topTen(hiScores records[50], int index);
void allScoresEvery(hiScores records[50], int index);
void menu(hiScores records[50], int index);


int main()
{
	hiScores records[50];
	int index = 0;     //lowest unused position in the array.
	
	menu(records, index);
}

void menu(hiScores records[50], int index)
{
	int choice = 42;

	while (choice >= 6 || choice <= 0)
	{
		cout << "\nPlease enter the number that corresponds to your choice:";
		cout << "\n     1). Enter new score";
		cout << "\n     2). Display all scores by specific user";
		cout << "\n     3). Top ten scores";
		cout << "\n     4). All scores saved";
		cout << "\n     5). Exit\n";

		cin >> choice;

		switch (choice)
		{
		case 1: enterNewScore(records, index);
			break;
		case 2: allScoresUser(records, index);
			break;
		case 3: topTen(records, index);
			break;
		case 4: allScoresEvery(records, index);
			break;
		case 5: cin.ignore();
			cin.get();
		}
	}
}

void enterNewScore(hiScores records[50], int index)
{

	cout << "\n\nPlease enter the name of the person with the score :  "; // still need to add code to replace low scores when array is full
	cin >> records[index].user;
	cout << "\nEnter your score :  ";
	cin >> records[index].score;
	index++;

	menu(records, index);
}

void allScoresUser(hiScores records[50], int index)
{
	string nameSearch[50];                                   // list of each name associated with a record
	int NSindex = 0;                                         // index of lowest empty spot in nameSearch
	for (int i = 0; i < index; i++)
	{
		for (int j = 0; j < NSindex; j++)                    
		{
			bool notOnList = false;
			
			if (records[i].user != nameSearch[j])
			{
				notOnList = true;	
			}

			if (notOnList == true)
			{
				nameSearch[NSindex] = records[i].user;
				NSindex++;
			}
		}

	}

	cout << "\nNames of record holders\n";
	for (int i = 0; i < NSindex; i++)
	{
		cout << nameSearch[i] << endl;
	}
	
	string inquiry;
	cout << "Who's name do you want to look up?";
	cin >> inquiry;

	cout << "Displaying all scores for "<< inquiry << ":" << endl;

	for (int i = 0; i < index; i++)
	{
		if (records[i].user == inquiry)
		{
			cout << records[i].user << "     " << records[i].score << endl;
		}
	}
	
	menu(records, index);
}
void topTen(hiScores records[50], int index)
{
	cout << "\n\nThis is where you see top ten scores";
	menu(records, index);
}
void allScoresEvery(hiScores records[50], int index)
{
	cout << "User Name     Score\n";
	cout << "---------     -----\n";
	int i = index - 1;
	for (i; i >= 0; i--)
	{
		cout << records[i].user << "     " << records[i].score << endl;
	}
	cout << "\n\nThis is where you see all records";
	menu(records, index);
}
Try doing this between lines 75-93:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	for (int i = 0; i < index; i++)
	{
		bool notOnList = true;
		for (int j = 0; j < NSindex; j++)                    
		{
			if (records[i].user == nameSearch[j])
			{
				notOnList = false;	
			}
		}
		if (notOnList == true)
		{
			nameSearch[NSindex] = records[i].user;
			NSindex++;
		}
	}

I like to think of it as the innocent-until-proven-guilty strategy. You could probably also do a guilty-until-proven-innocent strategy if you prefer. Either way you are proving whether or not a needle can be found in the haystack.

As for why you code right now is not working it is because the for loop on line 77 never has its body executed. This is because NSindex starts at zero. So the initial value of j is always not less than zero, which causes the body to never be executed.
Last edited on
Thank you for help.
Topic archived. No new replies allowed.