Help needed with my assignment.

So I kind of started the program but I can't manage to make it work properly. Can anyone please help me make it work?
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
//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";
}


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.
What about your N_GAMES up in your #define at the top? Shouldn't there be an equal sign in there?
What about your N_GAMES up in your #define at the top? Shouldn't there be an equal sign in there?


You don't need an = when using #define because it's not an actual variable. It's the old school way of defining constants.

He could just as easily typed

const int N_GAMES = 4;
Really? Thanks. At least I've learned one thing today. My brain feels like mush. :(
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.
Topic archived. No new replies allowed.