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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/*
* Chapter 11, Problem 4
*
* Write a program that allows a user to enter high scores of a game,
* keeping tracking of the name of the user and the score. Add the
* ability to show the highest score of each user, all scores for a
* particular user, all scores from all users, and list of users.
*
* Display menu:
* 0. Exit
* 1. Add user info
* 2. Display high scores of all users
* 3. Display all scores for a particular user
* 4. Display list of users
* Write functions for each option
*
*/
#include <iostream>
#include <string>
using namespace std;
struct GameScores
{
string name;
int highest;
int scores[5];
int index_scores;
};
// Function Prototype
GameScores AddUser(GameScores user[], int index, int numS);
void DisplayHighestScoreAllUsers(GameScores user[], int index);
void DisplayAllScoresOfUser(GameScores user[], int index);
void DisplayListOfUsers(GameScores user[], int index);
int main()
{
// Declare and initialize variables
int choice;
GameScores player[10];
int countPlayers = 0;
int numScores = 0;
int index_player = 0;
// Initialize score array
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 5; j++)
{
player[i].scores[j] = 0;
}
}
while (true)
{
// Display menu
cout << "Menu: " << endl <<
'\t' << "0. Exit" << endl <<
'\t' << "1. Add user info" << endl <<
'\t' << "2. Display highest scores of all users" << endl <<
'\t' << "3. Display all scores of a particular user" << endl <<
'\t' << "4. Display list of users" << endl << endl;
cout << "Please make a selection: ";
cin >> choice;
switch (choice)
{
case 0:
return 0;
case 1:
cout << "How many scores to enter? ";
cin >> numScores;
player[countPlayers] = AddUser(player, countPlayers, numScores);
countPlayers++;
cout << endl;
break;
case 2:
DisplayHighestScoreAllUsers(player, countPlayers);
cout << endl;
break;
case 3:
cout << "The database contains players 0 to " << countPlayers - 1 << endl;
cout << ". Which player do you want?: ";
cin >> index_player;
DisplayAllScoresOfUser(player, index_player);
cout << endl;
break;
case 4:
DisplayListOfUsers(player, countPlayers);
cout << endl;
break;
}
}
}
GameScores AddUser(GameScores user[], int index, int numS)
{
cout << "Please enter player's name: ";
getline(cin >> ws, user[index].name);
cout << "Please enter highest score: ";
cin >> user[index].highest;
user[index].index_scores = numS;
int score = 0;
for (int i = 0; i < numS; i++)
{
cout << "Enter score " << i << ": ";
cin >> score;
user[index].scores[i] = score;
}
cout << user[index].name << endl;;
cout << user[index].highest << endl;
for (int i = 0; i < user[index].index_scores; i++)
{
cout << user[index].scores << endl;
}
return user[index];
}
void DisplayHighestScoreAllUsers(GameScores user[], int index)
{
cout << "index" << index << endl;
for (int i = 0; i < user[index].index_scores; i++)
{
cout << user[index].name << "score(s) are " << user[index].scores[i] << endl;
}
}
void DisplayAllScoresOfUser(GameScores user[], int index)
{
cout << "Print index_scores: " << user[index].index_scores << endl;
cout << "Player's name: " << user[index].name << endl;
cout << "Player's highest score: " << user[index].highest << endl;
for (int i = 0; i < user[index].index_scores; i++)
{
cout << "score " << i << " = " << user[index].scores << endl;
}
}
void DisplayListOfUsers(GameScores user[], int index)
{
for (int i = 0; i < index; i++)
{
cout << "Player[" << i << "] is " << user[i].name << endl;
}
}
|