Code:
//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>
using namespace 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";
}
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.
