//Create a program which lets the user choose a game genre,
//afterwards a game from the sub-menu for the selected genre
//and show some information about the selected game, previously
//entered in the program.
//Afterwards, the user rates the game on the scale from 1 to 10.
//After 10 users have selected and rated the games, the program
//shows the rating of the games.
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
#define N_GAMES 4 // number of games
struct games_t {
int id;
string genre;
string name;
string info;
int rating;
} games [N_GAMES];
string genres[] = {"RPG","Action games","Racing games","FPS games"};
void inputgames (int gcount);
void printgame (games_t game);
int main()
{
int gcount = sizeof( genres ) / sizeof( genres[ 0 ] ); // getting the size of genres table
string mystr;
// function to input gamedata
inputgames(gcount);
// listing inputted games
cout << "\nYou have entered these games:\n";
for (int i=0; i<N_GAMES; i++) {
printgame (games[i]);
}
cout << "\n\n";
cout<<"What kind of games are you interested in?"<<endl;
for (int i=0;i<gcount;i++)
{
cout<<i+1<<"\t\t"<<genres[i]<<endl;
}
int myChoice;
cout<<"(Please enter a number): ";
cin>>myChoice; //get the choice
cout<<"You have selected the following genre: "+ genres[myChoice-1] <<endl;
cout<<"listing games by genre: \t\t"<<endl;
for (int i = 0; i< N_GAMES; i++)
{
if (games[i].genre.compare(genres[myChoice-1]) == 0) {
cout << games[i].id << ": " << games[i].name << endl;
}
}
cout<<"Please select a interesting game:"<<endl;
int gamechoise;
cin>>gamechoise;
cin.ignore();
cout<<"you have selected the following game: " + games[gamechoise].name <<endl;
cout<<"here is some useless info about that game:"<<endl;
cout<<games[gamechoise].info<<endl;
cout<<"rate the game pl0x (1-10)"<<endl;
getline (cin,mystr);
stringstream(mystr) >> games[gamechoise].rating;
system ("pause");
return 0;
}
void inputgames (int gcount) {
int n;
cout << "Insert the game data."<<endl;
for (n=0; n<N_GAMES; n++)
{
games[n].id = n;
cout << "Select genre from following: "<<endl;
for (int i = 0; i < gcount; i++) { // listing genres
cout << i+1 << "\t\t" << genres[i] << endl;
}
cout<<"(Please enter a number between 1 - " << gcount << "): ";
int myChoice;
cin>>myChoice;
cin.ignore();
games[n].genre = genres[myChoice-1];
cout << "Enter name: ";
getline (cin,games[n].name);
cout << "Enter info: ";
getline (cin, games[n].info);
cout << "\n";
}
}
void printgame (games_t game)
{
cout << game.genre;
cout << " <> " + game.name;
cout << " <> " + game.info + "\n";
}
So as the comment at the start says, the program needs to let the user pick a game genre,a game from the sub-menu of said game genre and then show some info about the game which is previously entered into the program. Afterwards, the user should be able to rate the game and after 10 users have selected and rated games, the program should show the rating of the games.
I would very much appreciate it if anyone helps me make this work. The code is still messy and I'm a beginner but I was given the task of making this.
TS, this type of program really calls for a database and imo C++ is not the best language to use.
That said, why not let the user decide how many games he or she would like to enter by allowing them to enter a quit character?
And instead of storing their entries in memory, why not write them to a file so that you can save them after the program fails or exits?
You might also want to validate the user entry when they are entering a genre number. Because you are using that input to index an array, you need to make sure that it is within the boundaries of the array or else you get a segfault.