Help!!

I'm working on a program from where I can open all my games. But I'm quite new to c++.
Is there a way to make it possible to add games from the program itself instead of changing the code manually?

maybe something like
1
2
3
4
5
6
7
8
Choose a game

1. game 1
2. game 2
3. game 3
4. game 4

5. Register new game


when you select 5
1
2
3
4
5
6
7
cin << name;
cout << "name: " << name << endl;

//path of the game launcher
cin << path;
cout << "path: " << path << endl;


and then it throws you back to the main menu but now it's
1
2
3
4
5
6
7
1. game 1
2. game 2
3. game 3
4. game 4
5. game 5 //new game

6. Register new game

The most apparent way to do this would be to:

1.) Make a game class
2.) Make a container of games using one of the STL containers
3.) Populate the container by reading from a file (a file which you would also write to to "save" games).
Generally, what xismn said is sound advice, but I'd like to elaborate on it a little bit.

1. You don't have to do anything fancy for this bit. Even a struct with just two strings would do for what you provided. If you don't want to do this, then you'll need two of... (see below)

2. In case this bit confused you at all, you may want to consider using an std::vector specifically to store your list of games. If you didn't want to follow step 1, then you'll likely want to use two of these.

Some additional things to consider:
*How exactly are you going to be launching those games?
*Is your file going to be stored in the same location on each system, or stored in a location relative to the current working directory (depending on how you launch your program, usually the same directory as your program's executable, but not always)?

You mentioned that you were quite new, so if you'd need any clarification as to what any of this stuff is, please don't hesitate to ask. :)

-Albatross
I don't know how to do that :(
Okay, I got a PM with the code so far, and I can't help but notice that you're missing a little piece of prerequisite knowledge. Specifically, about arrays.

http://www.cplusplus.com/doc/tutorial/arrays/

Once you feel comfortable with the concepts (specifically the section on std::arrays), ease yourself into vectors. If the reference page on them seems a bit intimidating, here are a few snippets you may find useful.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <vector> //Necessary to use vectors.
#include <string>
#include <iostream>
int main() {
    std::vector<std::string> vecstr; //Make a new and empty vector of strings.
    vecstr.push_back("WAFFLE!"); //Add the string WAFFLE! to the vector.
    vecstr.push_back("PANCAKE!"); //Add the string PANCAKE! to the vector after the string WAFFLE!.
    vecstr.push_back("FRENCH TOAST!"); //Add the string FRENCH TOAST! after the string PANCAKE!.
    std::cout << vecstr.size() << std::endl; //Will print 3 because there are 3 elements in the vector.
    for(int i = 0; i < vecstr.size(); ++i)
        std::cout << vecstr[i] << std::endl; //Output each element of the vector on a new line.
    return 0; //Good coding habit.
}


Reference page: http://www.cplusplus.com/reference/vector/vector/

If you have more questions, feel free to ask!

-Albatross

But how would i go about adding more things into "vecstr" for exaple:
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
int main()
{
#include <vector>
#include <string>
#include <iostream>

int main()
{	
        vector<string> vecGame;
	vecGame.push_back("1.Add a game");

	for (unsigned int i = 0; i < vecGame.size(); ++i)
		cout << vecGame[i] << endl;
	
	cin >> Game;

	switch (Game)
	{
	case 1:
			cout << "Name: " << endl;
			cin >> Name;
                        cout << "Path: " << endl;
                        cin >> Path;
       //add Name to vecstr and what do i do with the Path?

 return 0;
}






Last edited on
But how would i go about adding more things into "vecstr" for exaple:
use push_back()


Note that there are some logical (and syntactic) errors in your code :

vecGame.push_back("1.Add a game");
This doesn't make sense, the purpose of the vector is to be a storage for the games (name & path)
This should be a simple cout<< statement instead :
cout << "1. Add a game" << endl;

~

1
2
for (unsigned int i = 0; i < vecGame.size(); ++i)
	cout << vecGame[i] << endl;
The purpose of this piece of code is to iterate over the vector and print the contents of it. In your code above, this will just print "1. Add a game" since it is what you stored in the vecGame

~
A minimal example, assuming the text file looks like :
Crysis Warhead
Crysis 3
Need for Speed Most Wanted
Gta IV
Farcry
etc...


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
#include <iostream>
#include <fstream> // to be able to open a file and store the list of games in it
#include <string>
#include <vector> // for std::vector container
using namespace std;

int main ()
{
  const char* filepath( "games.dat" ); // the path to the text file

  fstream gamestream( filepath, ios::in | ios::out | ios::app ); // open the file "games.dat"
  vector<string> games; // declare a vector of string w/c corresponds to the game title
  // games.reserve( SOME_VALUE ); // this is optional, but it will prevent unneccessary push_back's
  
  /* read the file */
  string temp; // a temporary variable for reading the name of games
  while( getline( gamestream, temp) { // getline() to read a line instead of single word
    games.push_back( temp ); // add the contents of string temp into the vector
  }
  // now the container games contains the list of games in the text file

  // Print the menu
  
  // print the list of games :
  for( unsigned i = 0; i < games.size(); i++ ) {
    cout << i << " " << games[ i ];
  }

  switch( ... ) {
  case 0 :
    break;
  case 1 :
    break;
  case 3:
    break;
  // ...
  case ADD_A_GAME :
    string temp_;
    // prompt
    cin >> temp_;
    gamestream << temp_ << endl; // add it to the file
  }

}


Note that i havent tested this yet, it might have errors.

Now it is up to you how you will add the path of the game to the file

Google up: input/output with files, and go to this site :http://www.cprogramming.com/tutorial/stl/vector.html
Last edited on
closed account (j3Rz8vqX)
Something to play with:

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
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;

void load_game(int i,vector<string> game_path){
    //for simplicity using system(); use CreateProcess instead for performance
    system(game_path[i].c_str());
}
void register_game(int &number_of_games,vector<string> &game_name,vector<string> &game_path){
    string temp;
    cout<<"Name: ";
    getline(cin,temp);
    game_name.push_back(temp);
    cout<<"Path: ";
    getline(cin,temp);
    game_path.push_back(temp);
    number_of_games++;
}

int main()
{
    vector<string> game_name;
    vector<string> game_path;
    string choice;
    int i=0,number_of_games=0;
    do{
        cout<<"\nChoose a game: "<<endl;
        for(i=0;i<number_of_games;i++){
            cout<<i<<". "<<game_name[i]<<endl;
        }
        cout<<endl<<i<<". Register new game"<<endl;
        cout<<endl<<i+1<<". Exit"<<endl;
        cout<<"\nChoice: ";
        getline(cin,choice);
        int int_choice = atoi(choice.c_str());
        if(int_choice>=0 && int_choice<number_of_games){
            load_game(int_choice,game_path);
        }
        else{
            if(int_choice==number_of_games){
                register_game(number_of_games,game_name,game_path);
            }
        }
        if(int_choice==number_of_games+1){
            break;
        }

    }while(true);
    return 0;
}


System() is not preferred, but simple enough for this case.
It works pretty okay, but it doesn't save the games :)
closed account (j3Rz8vqX)
Yeah...lol

Things to work on:

Write the games name and path to files, and read from the files on startup.
If file is not valid, skip read.

Create file on completion or enable save option in menu.

Logic to persistent data:
Life outside of the app, can only be obtain with hard disk access (file) or memory leak (ram).
Last edited on
closed account (j3Rz8vqX)
Not sure if you're still interested, but I've finally gone back and added the extra features:

Load from File. -Auto Load file on startup-
Save to File. -Auto Save file on completion-

Also, I've enabled 1 command line argument to change the default name of the file.

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
/*
    Simple Game Loader
        -Free-

    Warning:    When executing this program from the command line,
                argument 1 should not have a name of an important file
                that exists and is not associated to this program.
                Just think of the possibilities...
                    Load - Possibly read massive amounts of data.
                    Save - Possibly overwrite important file with loader data.
*/
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <fstream>
using namespace std;

void register_game(int &number_of_games,vector<string> &game_name,vector<string> &game_path)
{
    cout<<"\n===== Register Menu ====="<<endl;
    string temp;
    cout<<"Name: ";
    getline(cin,temp);
    game_name.push_back(temp);
    cout<<"Path: ";
    getline(cin,temp);
    game_path.push_back(temp);
    number_of_games++;
}
void load_from_file(vector<string> &game_name, vector<string> &game_path,int &number_of_games,string game_data)
{
    cout<<"\n===== Auto Load " << game_data << " ====="<<endl;
    fstream file(game_data.c_str(), ios::in);
    if(file.bad())
        cout<<game_data<<" does not exist - file will be created at exit"<<endl;
    string s;
    do
    {
        getline(file,s);
        if(s == "")
            break;
        game_name.push_back(s);
        getline(file,s);
        if(s == "")
            break;
        game_path.push_back(s);
        number_of_games++;
    }while(true);
    file.close();
}
void save_to_file(vector<string> game_name, vector<string> game_path,int number_of_games,string game_data)
{
    cout<<"\n===== Auto Save " << game_data << " ====="<<endl;
    fstream file(game_data.c_str(),ios::out|ios::trunc);
    for(int i=0;i<number_of_games;i++)
    {
        file << game_name[i] << '\n';
        file << game_path[i] << '\n';
    }
    file.close();
}
void game_option(int int_choice,vector<string> &game_name,vector<string> &game_path,int &number_of_games)
{
    do
    {
        int i=0;
        string choice;
        cout<<"\n===== "<<game_name[int_choice]<<" "<<game_path[int_choice]<<" ====="<<endl;
        cout<<endl<<i++<<". Play "<<game_name[int_choice]<<endl;
        cout<<endl<<i++<<". Edit "<<game_name[int_choice]<<endl;
        cout<<endl<<i++<<". Delete "<<game_name[int_choice]<<endl;
        cout<<endl<<i++<<". Exit "<<game_name[int_choice]<<" option"<<endl;
        getline(cin,choice);

        if(choice == "0")
        {
            //for simplicity using system(); use CreateProcess instead for performance
            system(game_path[i].c_str());
        }
        if(choice == "1")
        {
            register_game(number_of_games,game_name,game_path);
            --number_of_games;
            game_name[int_choice] = game_name[number_of_games];
            game_name.pop_back();
            game_path[int_choice] = game_name[number_of_games];
            game_path.pop_back();
        }
        if(choice == "2")
        {
            game_name.erase(game_name.begin()+int_choice);
            game_path.erase(game_path.begin()+int_choice);
            --number_of_games;
            break;
        }
        if(choice == "3")
        {
            break;
        }

    }while(true);
}
int main(int argc, char* argv[])
{
    string game_data;
    if(argc>1)
    {
        game_data = argv[1];
    }
    else
    {
        game_data = "my_game_list.dat";
    }

    vector<string> game_name;
    vector<string> game_path;

    int number_of_games=0;

    load_from_file(game_name,game_path,number_of_games,game_data);

    do
    {
        int i;
        string choice;
        cout<<"\n===== Main Menu ====="<<endl;
        cout<<"Choose a game: "<<endl;
        for(i=0;i<number_of_games;i++)
        {
            cout<<i<<". "<<game_name[i]<<endl;
        }
        cout<<endl<<i<<". Register new game"<<endl;
        cout<<endl<<i+1<<". Exit"<<endl;
        cout<<"\nChoice: ";
        getline(cin,choice);
        int int_choice = atoi(choice.c_str());
        if(int_choice>=0 && int_choice<number_of_games)
        {
            game_option(int_choice,game_name,game_path,number_of_games);
        }
        else
        {
            if(int_choice==number_of_games)
            {
                register_game(number_of_games,game_name,game_path);
            }
            if(int_choice==number_of_games+1)
            {
                break;
            }
        }
    }while(true);

    save_to_file(game_name,game_path,number_of_games,game_data);
    return 0;
}


Anyone is allowed to use/modify the code. I don't claim any credit from this program and I am not reliable for any harm or disasters it may cause you.

The code is as is.

1
2
3
4
5
6
    Warning:    When executing this program from the command line,
                argument 1 should not have a name of an important file
                that exists and is not associated to this program.
                Just think of the possibilities...
                    Load - Possibly read massive amounts of data.
                    Save - Possibly overwrite important file with loader data.

Last edited on
Topic archived. No new replies allowed.