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:
#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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?
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;
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, 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.
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.
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.
// 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.
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 ===|
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;
}
// 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);