Return statement.

Hey guys, I need your help on this one. I'm writing a text RPG game. Basically after creating your character you can either click 1 to play or click 2 to edit your character. If you click 1 its ok, the game plays well. However I can't make it return to the beginning of character creation if you click 2.

I think I should use return statement, but I can't make it work.

Here is the whole character creation code:

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
             case 'P':
             case 'p':
                
                cout << "Please type in your character's name:" << endl;           
                cout << "\n";
                cin >> name;
                Sleep (1000);
                system("CLS");
                break;
      }
      
      //Rase, klase

      {                                                        
                       cout << "Choose your race: " << endl;                 
                       cout << "\n1 - Human" << endl;
                       cout << "2 - Orc" << endl;
                       cout << "3 - Elf" << endl;
                       cout << "\n";
                       
                       cin >> option;
                       system("CLS");
                       
                       switch (option)
                       {
                              case 1:
                                   race = "human";
                                   break;
                              
                              case 2:
                                   race = "orc";
                                   break;
                              case 3:
                                   race = "elf";
                                   break;
                       }
                        
      }
                       
      
      {                cout << "Choose your class: " << endl;    
                       cout << "\n1 - Warrior" << endl;
                       cout << "2 - Thief" << endl;
                       cout << "3 - Mage" << endl;
                       cout << "\n";
                       
                       cin >> classs;
                       system("CLS");
                       
                       switch (option)
                       {
                              case 1:
                                   classs = "warrior";
                                   break;
                              case 2:
                                   classs = "thief";
                                   break;
                              case 3:
                                   classs = "mage";
                                   break;
                       }
    
       }
     
       {
                                       
                       cout << "Your name is: ";             
                       cout << name << endl;             
                       cout << "Your race is: " ;            
                       cout << race << endl;          
                       cout << "Your class is: ";
                       cout << classs << endl;
                       Sleep (3500);
                       system("CLS");

      
                 
                       cout << "\n1. Start the adventure!" << endl;
                       cout << "2. Edit your character" << endl;
                       cout << "\n";
                       cin >> c;
                       
                       if (c == 2)
                       {
                           //return code should be here. How can I make it return to the beginning of character creation if you click 2?
                       }
                           

      }
I would put the game in its own function and your character creation in its own function. Then in your if statement at the bottom, just call the appropriate function. I would use a switch statement instead of an if statement.
I'm sorry could you write an example code?
No, but I can write some example code.

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

Put your "Edit your character" code in its own function:

GetRace()
     {                                                        
      cout << "Choose your race: " << endl;                 
      cout << "\n1 - Human" << endl;
      cout << "2 - Orc" << endl;
      cout << "3 - Elf" << endl;
      cout << "\n";
                       
      cin >> option;
      system("CLS");
                       
      switch( option )
          {
           case 1:
               race = "human";
               break;
                              
           case 2:
                race = "orc";
                break;

            case 3:
                race = "elf";
                break;

           }    /* switch( option )    */
                        
      }
            /*    GetRace()    */


int main( int argc, char** argv )
    {
    cout << "\n1. Start the adventure!" << endl;
    cout << "2. Edit your character" << endl;
    cout << endl;

    cin >> c;

    switch( c )
        {
        case 1:
            StartGame();

            break;

        case 2:
            GetRace();

            break;

        default:
            cout << "Invalid choice!" << endl;

            break;

        }    /*    switch( c )    */

    }
        /*    main()    */
                       
Does that help?
Yes, that helps me. Thanks!
btw you can use like this:

1
2
3
4
5
6
char test;
//...
switch (toupper(test)) {
case 'P':
     //...
}
Hey again, when I try putting char creation screen into it's own function, it keeps saying that 175 char_create undeclared (first use this function). Here's the beginning of character create function:

1
2
3
4
5
6
7
8
9
10
11
12
case 'P':
             case 'p':
             
         int char_create()
                
                cout << "Please type in your character's name:" << endl;           
                cout << "\n";
                cin >> name;
                Sleep (1000);
                system("CLS");
                break;
      }


Here's the second part:
1
2
3
4
5
6
7
8
9
10
11
 switch ( c )
                 {
                    
                        case 2: char_create()
                        break;
                        
                        default:
                                cout << "Invalid choice" << endl;
                        break;
                 }
                       
You need to revisit function syntax. Study my posting above, then look at your latest post. In the first posting, you shouldn't have your char_create() function in the middle of a case statement. Also, you need an opening curly brace.

In the second post, when you call the char_create() function, you need a semicolon after the right parentheses. It's better to put actions on a line by itself, IMHO:

1
2
3
4
5
6

case 2:
    char_create();

    break;
Topic archived. No new replies allowed.