#include <iostream>
#include <cmath>
#include <fstream>
#include <math.h>
usingnamespace std;
void maing();
void hbuy();
void njob();
void work();
void charc();
void ustats();
int umoney;
int chef = 900;
int mechanic = 500;
int maid = 650;
string goal;
string ujob;
string firstname;
string lastname;
string gender;
void ustats(){
if (goal == "a"){
goal = "have 1 000 000$";
}
elseif (goal == "b"){
goal = "have a villa";
}
elseif (goal == "c"){
goal = "be immortal";
}
cout << "Name: " << lastname << ", " << firstname << endl;
cout << "Gender: " << gender << endl;
cout << "Life goal: " << goal << endl;
cout << "Work: " << ujob << endl;
cout << "Balance: " << umoney << endl;
maing();
}
void work(){
if (ujob == "Chef" || ujob == "chef"){
cout << "You earned " << chef << "$ today" << endl;
cout << umoney + chef;
maing();
}
elseif (ujob == "mechanic" || ujob == "Mechanic"){
cout << "You earned " << mechanic << "$ today" << endl;
cout << umoney + mechanic;
maing();
}
elseif (ujob == "maid" || ujob == "Maid"){
cout << "You earned " << maid << "$ today" << endl;
cout << umoney + maid;
maing();
}
}
void njob(){
cout << "What job would you like ? Chef/Mechanic/Maid" << endl;
cin >> ujob;
if (ujob == "Chef" || ujob == "chef"){
cout << "You are hired ! Every time you go to work you earn 900$" << endl;
maing();
}
elseif (ujob == "Mechanic" || ujob == "mechanic"){
cout << "You are hired ! Every time you go to work you earn 500$" << endl;
maing();
}
elseif (ujob == "Maid" || ujob == "maid"){
cout << "You are hired ! Every time you go to work you earn 650$" << endl;
maing();
}
}
void hbuy(){
...
}
void maing(){
string uinput;
cout << "What would you like to do ?" << endl;
cin >> uinput;
if (uinput == "hbuy"){
hbuy();
}
elseif (uinput == "njob"){
njob();
}
elseif (uinput == "work"){
work();
}
elseif (uinput == "status"){
ustats();
}
}
void charc(){
cout << "Please choose the first name for your character" << endl;
cin >> firstname;
cout << "Please choose the last name for your character" << endl;
cin >> lastname;
cout << "Your new characters name is " << firstname << " " << lastname << endl;
cout << "What gender is your character ? M/F" << endl;
cin >> gender;
if (gender == "M" || gender == "m"){
cout << "Your character is male." << endl;
}
elseif (gender == "F" || gender == "f"){
cout << "Your character is female." << endl;
}
else {cout << "No such gender. Error code: b0x0002 " << endl;}
cout << "Choose the life goal of " << firstname << " " << lastname << ": " << "have 1 000 000$, have a villa, immortality.a/b/c" << endl;
cin >> goal;
if (goal == "a"){
cout << "good luck !" << endl;
maing();
}
elseif (goal == "b"){
cout << "Get a nice job !" << endl;
maing();
}
elseif (goal == "c"){
cout << "good luck with that !" << endl;
maing();
}
else { cout << "Goal not available. Error code: c0x0003" << endl;}
}
int main(){
string choice;
cout << "WELCOME to the life game - Alpha 1.1_1" << endl;
cout << "HELP " << endl;
cout << "You will be prompted to choose your characters life goal at the beginning along with other personalities." << endl;
cout << "At the begining of the game you receive 50 000$." << endl;
cout << "Buy a house that you can afford, to do so, type in hbuy after you start the game" << endl;
cout << "You must get a job, there are currently only 3 jobs available in this alpha version of the game, to get a job type in njob" << endl;
cout << "To check your status, type status(money, age, work...)" << endl;
cout << "OTHER COMMANDS" << endl << "fbuy - allows you to buy furniture for your house" << endl << "work - go to work" << endl << "qwork - quits your current work" << endl;
cout << "NOTE: for now saving the game is not possible, it will be possible in the near future." << endl;
cout << "NOTE: you are given one small house at the beginning worth 20000$." << endl;
cout << "WOULD YOU LIKE TO START YOUR GAME ? Y/N" << endl;
cin >> choice;
if (choice == "Y" || choice == "y"){
charc();
}
elseif (choice == "N" || choice == "n" ){
cout << "Thanks for reading the rules..." << endl;
}
else {cout << "There are 2 exact types of answers you can write, this answer does not match any of them. Error code: a0x0001" << endl;}
system("pause");
return 0;
}
So in this (let's call it) game, when users goes to work, he earns money, but when the user inputs "status", it will show 0 on "balance" line. How can I fix this so that the user earns money?
Also, if you know, is it possible to add game saving ?
EDIT: look at the "void work(){}", I think that I should add something there.
>when the user inputs "status", it will show 0 on "balance" line. How can I fix this so that the user earns money?
I've just had a quick look and it looks to me like the variable umoney is never actually modified.
Inside the work function you have things like
cout << umoney + chef;
But this doesn't modify the value of umoney, it remains at 0. This line simply prints the value of the expression umoney + chef. You just need to add chef to the current value of umoney and then print the value of umoney, and the same for the other jobs.
void Save()
{
cout << "Enter in a name for your save file" << endl;
cin >> SFile;
ofstream MyFile(SFile.c_str());
MyFile << goal << endl;
MyFile << lastname << endl;
MyFile << firstname << endl;
MyFile << gender << endl;
MyFile << ujob << endl;
MyFile << umoney << endl;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void Load()
{
cout << "Enter in the name of the save file you want to load" << endl;
cin >> SFile;
ifstream MyFile(SFile.c_str());
getline(MyFile, goal);
MyFile >> lastname;
MyFile >> firstname;
MyFile >> gender;
MyFile >> ujob;
MyFile >> umoney;
ustats();
}
Then call those functions if the user enters "save" or "load" (you would probably want to ask if they want to load a file when they first open the program as well)
Those functions aren't perfect (you should check if the save file exists etc), but that's the general idea