To many choices in choices

Pages: 12
Ok, so like my name suggests i am a complete noob to c++. So here is my delema. Im making a text based RPG game. Where you have many choices to make. My problem is that some of my choices in the game do the same things. For example one of the choices in my game is too go speak to your father or explore the area around you, but.. if you choose to explore the area around you one of the places around you is the place your father is. SO if you choose to explore and you choose to go to the granary (where your father is) its the same as if you didnt explore and spoke to your father in the first place. So i had to put the ENTIRE code of what happens after you speak to your father in both places of the program. Witch would be the entire game basicly. So what im asking is, is there a way to maybe put in a chunk of code in the explore part where you can talk to your father that calls to the previously written code where you talked to your father in the first place? Instead of having the LARGE chunk of code for what happens when you talk to your father in both places? I know the way i worded this question could make it hard to understand what i mean. But please help or ask me to clarify what it is i exactly need help with. THANKS!

closed account (3qX21hU5)
I'm sorry but I couldn't really follow what you were saying or what you are trying to do. Could you be a bit more specific on what you want to do and also post some code (Remember to use codetags)?
Ok so here is the code for the first choice. Witch is to speak to your father or explore the area around you. I didnt put the entire code of the conversation with your father. You can see that there is two choice talk to your father or explore the area around your hut. But if you choose to explore there is three options you can choose from. 1Go to the granary where your father is 2Go to the garden/wheat feild 3Go to the barn. BUT if you choose 1 its the same as if you didnt explore at all. So i had to put the chunk of code of what happens if you talk to your father in both places. Witch leads to the rest of the game. So i would have to put the rest of the code for the entire game in both places.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    int choice1;
    cout <<"------------------------------------------------------------------------" << endl;
    cout <<"Now that you have spoken to your mother, you may" << endl;
    cout <<"go see your father or explore the area around your" << endl;
    cout <<"hut." << endl;
    cout <<"Choose 1 to talk to your father" << endl;
    cout <<"Choose 2 to explore the area around your hut." << endl;
    cout <<"Choice: ";
    cin >>choice1;
    if(choice1 == 1)
     {
    cout <<"------------------------------------------------------------------------" << endl;          
    cout <<"(Your father)Hey " << Cname << endl; 
    cout <<"It's about time you woke up." << endl; 
    system ("pause");
    cout <<"------------------------------------------------------------------------" << endl;
    cout <<"I need you to help me." << endl;
    cout <<"Will you?" << endl;
    int choice3;
    cout <<"If you would like to help your father choose: 1" << endl; 
    cout <<"If you would not like to help your father choose 2:" << endl;
    cout <<"Choice: ";
So is there anything i can do so that i dont have to have the whole games worth of code in both places?
How about having a function to avoid the repeating code?

Sounds as though you are going to have lots of menus. Showing a menu should be a function, control each menu with a switch & have each case call a function.

Hope all goes well :)
So what your saying is have a int choice; and if(choice == 1) as a function and have a call to the function instead of doing each one indavidualy??
No, I meant have a switch instead of if statements. Google C++ switch to see how it works. Each menu should be shown via a function call & each case in the switch should call a function.

So if you have a menu with one of the options is to "talk with father", then the particular case in the switch could call a function called "TalkWithFather" say. If there is another menu that also has "talk with father" as an option then it's particular case in it's switch would call the same "TalkWithFather" function. In this way you avoid duplicating code.

Have a go at this & show the code - we can help from there.

If you have any compiler errors, then post hem in full.
ohhh ok but if i do another switch later on down the code can i call to the same case in the switch before?
No, you call the same function. Read the reference in the Google search - it should take you to the reference page on this site, or you can navigate the links at the top left of this page.

Once you understand how switch & function calls work, show us your new code.
will do
So what your saying is do this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    switch (choice){
    cout <<"Choose 1 to talk to your mother. Choose 2 to kill" << endl;
    cout <<"your mother. \nChoice: ";
    cin >>choice;
    
    case 1:  
    cout <<"------------------------------------------------------------------------" << endl;       
    cout <<"(Your mother) Hi " << Cname << endl; 
    cout <<"You need to speak to your father." << endl;
    cout <<"He is out by the granary." << endl;       
    break;    
      
    case 2:
      
    cout <<"------------------------------------------------------------------------" << endl;          
    cout <<"Good job now your mother is dead and you dont know what to do." << endl;
    system("pause");
    break;
    }


instead of this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    int choice;
    cout <<"Choose 1 to talk to your mother. Choose 2 to kill" << endl;
    cout <<"your mother. \nChoice: ";
    cin >>choice;
    if(choice == 1)
      {
    cout <<"------------------------------------------------------------------------" << endl;       
    cout <<"(Your mother) Hi " << Cname << endl; 
    cout <<"You need to speak to your father." << endl;
    cout <<"He is out by the granary." << endl;       
        
      }
    if(choice == 2)
      {
    cout <<"------------------------------------------------------------------------" << endl;          
    cout <<"Good job now your mother is dead and you dont know what to do." << endl;
    system("pause");
Do you know how to do functions?

Put the menu in a function as I said, so you can reuse it.

The cases are function calls for the same reason.

And always have a default clause to catch bad input, and put the switch into a bool controlled while loop, so it keeps looping until there is good input. Include options (cases) to go back to the previous menu and quit the program altogether. You can use chars instead of ints if you want - put them in single quotes.

Im sorry im very very new to this. I think i know what a function is. Its when you declare the function before hand and then call the function later on in the code right?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}

int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z;
  return 0;
}


like this right?
1-6 would be the function correct?
and 10-12 would be the call to the function?
That is correct Max, line 11 is the actual call to the function
ok, so how would i do that in a menu/choice form? would i put the whole menu in the function and then call the function later on? or would i put the menu options in the function and then use the menu to call each individual function?
ohhhhhhhhhhhhhhhhhh i just reread what he said earlier :) i get it!!! hahaha i feel so stupid thank you to everyone who helped REALLY REALLY apriciate it!
Ill do a little version of it and post it to see if its correct
why doesnt this work? Am i close?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>

using namespace std;

int talkwithfather()
{
   return (cout <<"Since the begining of time, there has been a proficy.");
}

int main()
{
    int x;
    {
    x = (talkwithfather);
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Pages: 12