Help with Text game

Hi everyone, i have been learning C++ for a few months now and i decided to make a small text game, but i need some help figuring something out. here is my 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
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
158
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//Help Function
void help()
{
     string choice;

            cout << "\n";
            cout << "HELP DOCUMENTATION" << endl;
            cout << "\n";
            cout << "Below is a list of catagories, please type in the letter next to the catagorie to see more info on that subject" << endl;
            cout << "\n";

            cout << "A - Keywords" << endl;
            cout << "B - Pets List" << endl;
            cin >> choice;
            cout << "\n";

            if(choice == "A" || choice == "a"){
                cout << "KEYWORDS LIST" << endl;
                cout << "\n";
                cout << "Quit - Exits from the game" << endl;
                cout << "Feed - Feeds your TextPet" << endl;
                cout << "Rename - Allows you to re-name your text pet" << endl;
                cout << "Stats - Shows your stats like pet name, money, pet health etc" << endl;
                cout << "\n";
            }

            if(choice == "B" || choice == "b"){
                cout << "TextCat" << endl;
                cout << "TextDog" << endl;
            }

}

//Choose TextPet
void choosepet()
{

     string TextPetChoose;
     string TextPet;

    cout << "Ok now please choose your TextPet" << endl;
    cout << "\n";
    cout << "1 - TextDog" << endl;
    cout << "2 - TextCat" << endl;
    cout << "\n";
    getline(cin, TextPetChoose);
    cout << "\n";

    if(TextPet == "1"){
        cout << "Ok TextDog it is then" << endl;

             if(TextPet == "2"){
                cout << "TextCat it is then" << endl;
             }
             }
    }


//Here you can rename your pet
void RenameTextPet()
{
    string rename;

    cout << "What would you like to re-name your pet?" << endl;
    cout << "\n";
    getline(cin, rename);
    cout <<"\n";

}

//View Statistics
void Stats()
{

    int health = 100;
    int money = 0;

    cout << "\n";
    cout << "Pet Health: " << health << endl;
    cout << "\n";
    cout << "Money: "<< "$" << money << endl;
    cout << "\n";

}

//Feed TextPet
void FeedTextPet()
{

    cout << "What would you like to feed your TextPet?" << endl;
    cout << "\n";
    cout << "A - TextPet " << endl;
    cout << "     " << "Cost: $10" << endl;
    cout << "     " << "     " << "Health Restored: 8 points" << endl;
    cout << "\n";
    cout << "B - " << endl;

}

int main()
{
    string PlayerName;
    string death = "Your TextPet has died";
    string choice;
    string TextPetName;

    cout << "Welcome to the world of TextPet" << endl;
    cout << "TextPet is a interactive text pet that you can play with!" << endl;
    cout << "If you need help, just type in help at anytime" << endl;
    cout << "Its suggested you visit the help corner if this is your first time playing" << endl;
    cout << "\n";
    cout << "Press Enter to begin" << endl;
    cin.get();
    cout << "\n";

    //Enter your name

    cout << "Ok lets start by entering your name" << endl;
    cout << "\n";
    getline(cin, PlayerName);
    cout << "\n";

    //Name your TextPet

    cout << "what do you want to name your new TextPet?" << endl;
    getline(cin,TextPetName);


    while(choice !="quit"){
        cin >> choice;

            if(choice == "Help" || choice == "help"){
                help();
                }

                    if(choice == "Rename" || choice == "rename"){
                    RenameTextPet();
                    }

                        if(choice == "Stats" || choice == "stats"){
                        Stats();
                        }

                            if(choice == "Feed" || choice == "feed"){
                            FeedTextPet();
                            }
    }

    cin.get();
    return 0;
}


now what i want to do is make the health and money go up but they are in a function and i dont know how to make them go up when your in a different function. also how do you make the health or money go up and down in more increments than 1? i know about health++ or money++ but what if i wanted to give the player $10 or -5 health?
Last edited on
i know about health++ or money++ but what if i wanted to give the player $10 or -5 health?


You can use money += 10 and health -= 5
It gives me this error:

C:\Users\Chay Hawk\Desktop\C++ Projects\C++\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\C++ Projects\C++\main.cpp|10|error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'|
||=== Build finished: 1 errors, 0 warnings ===|

Heres the code i just used to test it out:

// void function example
#include <iostream>
using namespace std;


int main ()
{
int money = 0;

cout << money +=10 << endl;
cout << money << endl;

return 0;
}


also do you know how i would access those integers from another function? because i have health and money in one function and i'll be subtracting and adding money from the main function:

//View Statistics
void Stats()
{

int health = 100;
int money = 0;

cout << "\n";
cout << "Pet Health: " << health << endl;
cout << "\n";
cout << "Money: "<< "$" << money << endl;
cout << "\n";

}
Last edited on
1
2
3
4
5
6
7
8
9
10
int main ()
{
int money = 0;
money += 10;

//cout << money +=10 << endl;
cout << money << endl;

return 0;
}


As for your second case, you need parameter passing in your Stats

E.g
void Stats(int &health, int &money) {
health -= 5;
money += 10;
......
}

Call it from main function like

int main() {
int health = 0;
int money = 0;
Stats(health,money);
//down here if you cout you will see the changed values in health and money
...
}
I see, i will try this and try to make it all work as best i can before i ask for more help, thanks!
-->>
Last edited on
Please use [code][/code] to format the code!
Ok, sorry :P
Ok so itried doing it and it didnt work, this is what i had:

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
//View Statistics
void Stats(int &health, int &money)
{

    int health = 100;
    int money = 5;

    cout << "\n";
    cout << "Pet Health: " << health << endl;
    cout << "\n";
    cout << "Money: "<< "$" << money << endl;
    cout << "\n";

}

//Feed TextPet
void FeedTextPet()
{
    string FoodChoice;

    cout << "What would you like to feed your TextPet?" << endl;
    cout << "\n";
    cout << "A - TextPet " << endl;
    cout << "     " << "Cost: $2" << endl;
    cout << "     " << "     " << "Health Restored: 8 points" << endl;
    cout << "\n";
    cout << "B - " << endl;

    if(FoodChoice == "A" || FoodChoice == "a"){
        money =-2;
    }

}




I wanted to be able to set prices for each piece of food, i dont want a set price for all of it. i'm totally lost and confused right now on what to do.
Can someone help me solve my problem i really need to get this done.

let us assume:
1
2
3
4
5
6
7
8
9
10
11
12

void SetFoodPrice()
{
        // I am assuming that foodPrice is a global to the program
        // double foodPrice = 0.0;
        
        cout << "Please Enter a Food Price:" << endl;
        cin >> foodPrice;

        cout << "You entered: " << foodPrice << endl;
}



One thing through out your code I don't see any std::cin to read in any data.
Last edited on
The cin data is ther, i put it in getline(cin, string here); because i plan to output the typed in stuff to a text document which i already know how to do. For the money what i want it to do is this:

I want to set the price of the food lets say $2 for regular food i want it to subtract from the total amount of money the player has every time they buy it. the cost of the food and items will be pre set and the player will not be able to set the price for them. The food will all be different prices so the code needs to subtract accordingly, this is where im stumped, my entire code is at the top of the page if you need to look at all of it to help you.

also i cannot figure out how to access the money and health data as they are all in different functions.
Last edited on
yes I noticed that too.

Have you learned about scope and/or having a function return a value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
double SetFoodPrice()
{
        double foodPrice = 0.0;
        
        cout << "Please Enter a Food Price:" << endl;
        cin >> foodPrice;

        return foodPrice; 
}

int main()
{
       double foodPrice = SetFoodPrice();
       
        cout << "You entered: " << foodPrice << endl;

       return 0;
}


You also had things with references in them function definition.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// your function :
void Stats(int &health, int &money);
{
      health += 10; // added 10 health
      money -= 2; // deducted 2 money
}

int main()
{
        int health = 10;
        int money = 10;

        Stat(health, money);

       // when you get here and print out the numbers you will note that they have changed.
        cout << "You have " << health << " health." << endl;
        cout << "You have $" << money << " left."  << endl;

        return 0;
}


This is two ways I can pass variables back and forth.

Last edited on
I cant remember but im pretty sure i didnt. I get an error

C:\Users\Chay Hawk\Desktop\TextPet\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\TextPet\main.cpp|73|error: too few arguments to function 'void Stats(int&, int&)'|
C:\Users\Chay Hawk\Desktop\TextPet\main.cpp|153|error: at this point in file|
||=== Build finished: 2 errors, 0 warnings ===|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
//View Statistics
void Stats(int &health, int &money)
{
    health += 10;
    money +=10;


    cout << "\n";
    cout << "Pet Health: " << health << endl;
    cout << "\n";
    cout << "Money: "<< "$" << money << endl;
    cout << "\n";

}


what i want it to do is deduct the amount i pre set in code when the user selects which type of food they want. so in this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void FeedTextPet()
{
    string FoodChoice;

    cout << "What would you like to feed your TextPet?" << endl;
    cout << "\n";
    cout << "A - TextPet " << endl;
    cout << "     " << "Cost: $2" << endl;
    cout << "     " << "     " << "Health Restored: 8 points" << endl;
    cout << "\n";
    cout << "B - " << endl;

    if(FoodChoice == "A" || FoodChoice == "a"){

    }

}


if the player chooses food A it will deduct $2 from their overall money. i was thinking it would work if in the if statement it would deduct it something like this:

if(FoodChoice == "A" || FoodChoice == "a"){
money -=2;
}
Last edited on
it is the function call in main:

1
2
3
4
5
6
7
8
9
10

// you have something like this which is wrong
int health = 10;
int money = 10;

Stats(); // this is where the error is.

// it should look like:
Stats(health, money);


for this I would expect:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void FeedTextPet(int &health, int &money)
{
    string FoodChoice;

    cout << "What would you like to feed your TextPet?" << endl;
    cout << "\n";
    cout << "A - TextPet " << endl;
    cout << "     " << "Cost: $2" << endl;
    cout << "     " << "     " << "Health Restored: 8 points" << endl;
    cout << "\n";
    cout << "B - " << endl;

    cout << "Please Make a Selection: ";
    cin >> FoodChoice;

    if(FoodChoice == "A" || FoodChoice == "a")
    {
          health+=8;
          money-=2;
    }

}
Last edited on
Then when i enter that in how do i use this function somewhere else? like i stated above?
What do you mean?

my main line with all this stuff would be:

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
void Stats(int &health, int &money)
{
    health += 10;
    money +=10;


    cout << "\n";
    cout << "Pet Health: " << health << endl;
    cout << "\n";
    cout << "Money: "<< "$" << money << endl;
    cout << "\n";

}

void FeedTextPet(int &health, int &money)
{
    string FoodChoice;

    cout << "What would you like to feed your TextPet?" << endl;
    cout << "\n";
    cout << "A - TextPet " << endl;
    cout << "     " << "Cost: $2" << endl;
    cout << "     " << "     " << "Health Restored: 8 points" << endl;
    cout << "\n";
    cout << "B - " << endl;

    cout << "Please Make a Selection: ";
    cin >> FoodChoice;

    if(FoodChoice == "A" || FoodChoice == "a")
    {
          health+=8;
          money-=2;
    }

}

void attackwithPet(int &health, int &money)
{
      // simple demo
      health-=5;
      if(health < 0)
      {
             cout << "Your Pet has died" << endl;
       }
       else
       {
             cout << "You won the fight and you won $5!!" << endl;
             money += 5;
       }
}

int restPet(int health);
{
      cout << "Your Pet rested a little" << endl;
      return heath+2;
}

int main()
{

       // set up initial values..
       int health =10;
       int money =10;
       bool done = false;
       int choice = 0;

       while(done != true)
       {
              Stats(health, money);

              cout << endl;
              cout << "1 -- Feed Your Pet" << endl;
              cout << "2 -- Attack with your pet" << endl;
              cout << "3 -- rest your pet" << endl;
              cout << "4 -- quit the game" << endl;

              cin >> choice;
              if(cin.good())
              {
                    switch(choice)
                    {
                          case 1: FeedTextPet(health, money);
                                      break;
                          case 2: attackWithPet(health, money);
                                      break;
                          case 3:  health = restPet(health);
                                      break;
                          case 4:  
                                      done = true;
                                      break;
                         defualt:
                                  cout << "You entered an Invalid Choice!!' << endl;
                   } // switch
               } // we are good..
               else
               {
                      cout << "You didn't enter a number!!" << endl;
               }
       } // end of while

       cout << "Thank you for playing!" << endl;
       return 0;
}// end of main. 
Topic archived. No new replies allowed.