Aug 9, 2012 at 11:50pm UTC
I have my code that allows you to save from 3 different functions, depending on which one your in, and i can use the return statement to return back to the function but is there a way i can return to the last function without knowing what that function is?
Aug 10, 2012 at 12:16am UTC
Can you clarify a little more. Give names of the functions, or better yet, share the function codes and specify where you want to "return" to or how exactly you want the code to work.
Aug 10, 2012 at 12:24am UTC
I want it to return whatever function that the user was last in, i dont want to hard code a certain function, i want the program to know what function i was last in and return to it.
I want to return from save.
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
#include <iostream>
#include <string>
#include <ctime>
#include <random>
#include <fstream>
using namespace std;
class Vars
{
public :
void Battle();
void Hospital();
void Stats();
void Save();
void Load();
Vars()
{
PHealth = 100;
EHealth = 100;
XP = 0;
}
private :
int PHealth; //Player Health
int EHealth; //Enemy Health
int XP;
};
int main()
{
int choice;
Vars VO;
VO.Load();
cout << "Welcome to the battle arena" << endl;
cout << "Please enter the number of the selection\n" << endl;
cout << "1. Begin" << endl;
cout << "2. View Stats" << endl;
cin >> choice;
switch (choice)
{
case 1:
VO.Battle();
break ;
case 2:
VO.Stats();
break ;
default :
main();
}
}
void Vars::Battle()
{
int Pchoice; //Player choice
int Echoice; //Enemy Choice
cout << "Choose your attack\n" << endl;
cout << "1. Shotgun" << endl;
cout << "2. Machine Gun" << endl;
cout << "3. Knife" << endl;
cin >> Pchoice;
time_t T;
srand(T);
while (true )
{
time(&T);
int time;
time = rand() % 4;
if (PHealth <= 0)
{
PHealth = 100;
EHealth = 100;
cout << "You were knocked out" << endl;
cout << "You will now be taken to the hospital" << endl;
cin.get();
Hospital();
break ;
}
else if (EHealth <= 0)
{
string Choice;
PHealth = 100;
EHealth = 100;
cout << "Victory!! you killed the enemy!!\n" << endl;
XP += 20;
cout << "What would you like to do now?\n" << endl;
cout << "1. Battle again" << endl;
cout << "2. View Stats" << endl;
cout << "3. Quit Game" << endl;
cout << "4. Save Game" << endl;
cin >> Choice;
if (Choice == "1" )
{
Battle();
}
else if (Choice == "2" )
{
Stats();
}
else if (Choice == "3" )
{
cout << "Goodbye" << endl;
}
else if (Choice == "4" )
{
Save();
}
cin.get();
break ;
}
switch (Pchoice)
{
case 0:
cout << "you used the Shotgun\n" << endl;
EHealth -= 3;
cout << "Enemy Health: " << EHealth << endl;
Echoice;
cin.get();
break ;
case 1:
cout << "You used the Machine Gun\n" << endl;
EHealth -= 4;
cout << "Enemy Health: " << EHealth << endl;
Echoice;
cin.get();
break ;
case 2:
cout << "You used the Knife\n" << endl;
EHealth -= 1;
cout << "Enemy Health: " << EHealth << endl;
Echoice;
cin.get();
break ;
}
switch (Echoice, time)
{
case 0:
cout << "Enemy used Punch\n" << endl;
PHealth -= 4;
cout << "Your Health: " << PHealth << endl;
Pchoice;
cin.get();
break ;
case 1:
cout << "Enemy used Kick\n" << endl;
PHealth -= 3;
cout << "Your Health: " << PHealth << endl;
Pchoice;
cin.get();
break ;
case 2:
cout << "Enemy used Slap\n" << endl;
PHealth -= 1;
cout << "Your Health: " << PHealth << endl;
Pchoice;
cin.get();
break ;
default :
cout << "Enemy attack missed\n" << endl;
}
}
}
void Vars::Hospital()
{
string choice;
cout << "You were revived here at the Hospital\n" << endl;
cout << "What would you like to do from here?/n" << endl;
cout << "1. Fight" << endl;
cout << "2. Quit" << endl;
cout << "3. View Stats" << endl;
cout << "4. Save Game" << endl;
cin >> choice;
if (choice == "1" )
{
Battle();
}
else if (choice == "2" )
{
cout << "Goodbye" << endl;
cin.get();
}
else if (choice == "3" )
{
Stats();
}
else if (choice == "4" )
{
Save();
}
}
void Vars::Stats()
{
string input;
cout << "This screen will display your stats\n" << endl;
cout << "XP: " << XP << endl;
cin >> input;
cin.end;
}
void Vars::Save()
{
ofstream file("BattleArena.txt" );
file << XP << endl;
cout << "File Saved\n" << endl;
}
void Vars::Load()
{
ifstream file("BattleArena.txt" );
file >> XP;
cout << "Load Successful\n" << endl;
}
Last edited on Aug 10, 2012 at 12:24am UTC
Aug 10, 2012 at 12:33am UTC
C++ functions act like that by default...But since you're calling Save() at the end of the function, it's like the Save returns you back to main. Consider using a loop.
Edit: Also, never ever ever ever ever call main(). That is a horrible programming technique and should always always be avoided. You need to learn loops.
Last edited on Aug 10, 2012 at 12:34am UTC