saving the value

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <cmath>
#include <fstream>
#include <math.h>
using namespace 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$"; 
                            }
     else if (goal == "b"){
          goal = "have a villa";
          }                     
     else if (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();
              }
     else if (ujob == "mechanic" || ujob == "Mechanic"){
          cout << "You earned " << mechanic << "$ today" << endl;
          cout << umoney + mechanic;
          maing();
          }         
     else if (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();
           }
  else if (ujob == "Mechanic" || ujob == "mechanic"){
       cout << "You are hired ! Every time you go to work you earn 500$" << endl;
       maing();
       }
  else if (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();
                }
     else if (uinput == "njob"){
          njob();
          }    
     else if (uinput == "work"){
          work();
          }       
     else if (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;
               }
    else if (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();             
             }
    else if (goal == "b"){
         cout << "Get a nice job !" << endl;
         maing();
         }
    else if (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();
               }
    else if (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.

THANKS ! :D
Last edited on
Hi,

>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.
As for saving you could do something like this

string SFile;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
Last edited on
WOW !
Thanks for helping, imma put your names on "special thanks" list :D

But seriously thank you for helping :D
Topic archived. No new replies allowed.