"Moving" strings and ints.

Pages: 12
while (input != 'q' || input!='Q')
And neither 'q' nor 'Q' work.
Thats why i posted with &&
Yea but the other posted || before you.
And thank you, but read edited question
http://www.cplusplus.com/forum/general/22194/#msg116243
Just put your game logic into a function which will be called by newgame() (may a function called playGame() or play_game() or playgame() or blah)

maybe you want to create a Game object or similar. I don't know how big your game gets, but i think you will want to split your code in more files in some time.

I recommend you to read some tutorials on functions (optional classes), if you're not familiar with these.

Maikel

EDIT:

maybe you want to make use of tolower() or toupper()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    map<char,void(*)()> function_map;
    function_map.insert(pair<char,void(*)()>('N', newgame));//new
    function_map.insert(pair<char,void(*)()>('L', loadgame));//Load
    function_map.insert(pair<char,void(*)()>('H', help));//help
    function_map.insert(pair<char,void(*)()>('B', empty));//Back

    cout << "Welcome adventurer!\n\n[N]ew game\n[L]oad game\n[Q]uit\n\nType 'H' to get help.\n\n";
    char input = cin.get();
    while (input != 'q' && input != 'Q') {
        input = toupper(input);
        if (function_map.find(input) != function_map.end())
            function_map[input]();
        else
            cout << "\"" << input << "\" not found!\n";

        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        input = cin.get();        
    }
};
Last edited on
Thank you again, although i would have known that. (Damn me! xD)
Also what is wrong here, beacuse it automaticly gives too "not founds"(for "") before I can write anything. I know it`s not critical, but annoying.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void gender()
{
   cout << "\n\nPlease select your gender.\n\n[M]ale\n[F]emale\n\n";
    map<char,void(*)()> function_map;
    function_map.insert(pair<char,void(*)()>('M', male));
    function_map.insert(pair<char,void(*)()>('F', female));

    char input = cin.get();
    while (input != 'q' && 'Q') {
        input = toupper(input);
        if (function_map.find(input) != function_map.end())
            function_map[input]();
        else
            cout << "\"" << input << "\" not found!\n";

        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        input = cin.get();
    }
                          
}
And actually the "game" is gonna be more like "interactive novel", than game.
I mean there isn`t going to be action like: "You strike enemy and do 8 damage."
Last edited on
Yeah, but you dont have to the map thing every time you have a question to the user. That would lead to an enormous set of functions you get (and you would write then..). Think about parts that you are reusing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Game {
public:
    typedef enum { Female, Male, Hermaphrodite } GenderType;

    GenderType gender() const { return m_gender; }
    string name() const { return m_name; }
    // ... more getter functions
    void setGender(GenderType gender) { m_gender = gender; }
    void setName(string name) { m_name = name; }
    // ... more setter functions

    // playing function
    void play();
private:
    GenderType m_gender;
    string m_name;
    // ... and more states of game
};


Are your familiar with this kind of stuff?
Hmm... I prefer more simple stuff (=I`m not an expert xD).
Just one thing; how can I make it so 'N' also changes "int ch" to 2.
(And so would continue bit like this:)
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
   int main()
{
    int ch=1;
    
        if (ch=1)
       {
       map<char,void(*)()> function_map;
       function_map.insert(pair<char,void(*)()>('N', newgame));
       function_map.insert(pair<char,void(*)()>('L', loadgame));//Load
       function_map.insert(pair<char,void(*)()>('H', help));//help
       function_map.insert(pair<char,void(*)()>('B', empty));//Back
       cout << "Welcome adventurer!\n\n[N]ew game\n[L]oad game\n[Q]uit\n\nType 'H' to get help.\n\n";
       char input = cin.get();
       while (input != 'q' && input != 'Q') {
        input = toupper(input);
        if (function_map.find(input) != function_map.end())
            function_map[input]();
        else
            cout << "\"" << input << "\" not found!\n";

        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        input = cin.get();
        };
        if (ch=2)
{
//Something...
}
        
        
    }
    
};//End program  
Last edited on
So the question is, how to change this function, so that instead of going some void, it would change "int ch" value to certain number. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
map<char,void(*)()> function_map;
       function_map.insert(pair<char,void(*)()>('N', newgame));//would change int ch to 2;
       function_map.insert(pair<char,void(*)()>('L', loadgame));//Load
       function_map.insert(pair<char,void(*)()>('H', help));//help
       function_map.insert(pair<char,void(*)()>('B', empty));//Back
       cout << "Welcome adventurer!\n\n[N]ew game\n[L]oad game\n[Q]uit\n\nType 'H' to get help.\n\n";
       char input = cin.get();
       while (input != 'q' && input != 'Q') {
        input = toupper(input);
        if (function_map.find(input) != function_map.end())
            function_map[input]();
        else
            cout << "\"" << input << "\" not found!\n";

        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        input = cin.get();
        };

And the structure would be like:
1
2
3
4
5
6
7
8
9
10
11
12
13
if ch=1
{
//Above code
}
if ch=2
{
//Some other code
}
if ch=3
{
//some code
}
//And so on... 
Topic archived. No new replies allowed.
Pages: 12