I have my program and instead of dinking around witht he save thing im going to try to make a save class but it cant find the variables to save so i was wondering how to call a class function from another class, i tried but dont think im doing it right since i got errors
#include <iostream>
#include <string>
#include <ctime>
#include <random>
#include <fstream>
usingnamespace std;
class CLASS
{
public:
void Player();
void TRex();
void Upgrades();
void Help();
void Stats();
void Load();
CLASS()
{
PHealth = 100;
TRHealth = 100;
XP = 0;
NumDef = 0;
PKill = 0;
}
private:
int PHealth; //Player Health
int TRHealth; //T-Rex Health
int XP; //Experience points
int NumDef; //Number of opponents defeated
int PKill; //Number of times player was killed
};
class SAVEG
{
public:
void Save();
SAVEG()
{
CLASS();
file.open("stats.txt");
file << XP << endl;
file << NumDef << endl;
file << PKill << endl;
file.close();
}
private:
ofstream file;
};
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp||In constructor 'SAVEG::SAVEG()':|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|42|error: invalid use of 'class CLASS'|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|46|error: 'XP' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|47|error: 'NumDef' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|48|error: 'PKill' was not declared in this scope|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp||In function 'int main()':|
C:\Users\Chay Hawk\Desktop\Dinosaur Arena\main.cpp|86|error: 'class CLASS' has no member named 'Save'|
||=== Build finished: 5 errors, 0 warnings ===|
The reason why you got that error is because you declared and defined the Save() method inside the class declaration.
You need to either declare it and define it outside the class declaration or just declare and define it inside the class declaration.
Basically you need to make up your mind. Either define ALL methods inside the class declaration or define all of them outside of it. The former is a shortcut but is messy and defeats the purpose of having an interface.
*I only define methods inside the class declaration for example purposes.
P.S.- What kind of game are you trying to develop? You might want to spend some more time coming up with a better design. You could create a Save class but it would need access to all the necessary data.
This normally wouldnt be a problem but my program is big and its a bit disorentating. sorry i keep asking about it but i guess im just nbot seeing it, i looked up stuff about classes but it doesnt help all that much.
The problem im having isd that i give 30 points to the player but its not adding it to the variable in CLASS, i tested everything and thats the only problem im having, also how would putting my calls to main in a while loop help? im calling it in a bunch of different locations, putting it in a loop wont allow me to go back.
It's just bad practice to explicitly call main() inside main. Who told you to do that? Your main() should only be called once, when the program executes.
I have no idea i just did it myself because i didnt know of any other way of going back to the program. Well i do know of one, which would be to create another function and put all my stuff in main, in that function, then tell main to go to that function when it starts. but anyways my problem writing to the variables in CLASS is what i really need help with, i figured out how to get them to write to the file, its just when you defeat the Trex and et points it doesnt change the variables in CLASS
The reason you're having issues is just like everyone else has pointed out to you, you keep calling main. Now let me explain why this is bad. First, calling main is a big no no in C++, period. Second off, calling main will either erase your current class, it's as if your program never ran, or it will create another class object, which will no longer store the correct information. Why do you feel you need to call main? You know classes, you should know how to do loops, and especially since I seem them there, there is no reason why you're not using loops instead.
People have more than likely stopped responding to you since you were told several times what your issue is and you refused to correct it.
Please be a bit patient - there's other trying to get help too.
Here are some things to try:
- Close the file after loading, perhaps writing to a file open for reading is not possible.
- use ofstream.is_open() to verify whtether the file was opened or not.
- Set a breakpoint at the save function and see what the debugger has to say.
On a sidenote, it's best to name your classes relevant to its representation, for example: Class car{}; Car racecar; If you would use more than one class in your program using "class" will get very confusing.
That's not really fixing it. You're no longer calling main, but you're running your code over and over agan, deleting anything you've done. I believe you need to rethink your code and stop trying to use recursion. That's not the answer here.
Edit: Currently, your program is doing this:
Call MAINPROGRAM();
Create CLASS Object CO;
Create string variable choice;
Display:
"
DINOSAUR ARENA
Type in the number next to the choice
1) Enter Game
2) Help
3) View Stats
4) Save game progress
"
Enter user input to choice;
If user enter's 1, Call CO function Player;
Create string variable choice;
Display:
"
What weapon will you use?
1) Shotgun = 4 Damage
2) Pistol = 6 Damage
3) Knife = 5 Damage
"
You call an infinite loop;
Enter user input to choice;
TRHealth is 100, so if statement is ignored;
PHealth is 100 so if statement is ignored;
If user entered 1, choice == "1" is true;
Display:
"
You used the shotgun!
Your health: 100
"
TRHealth = 96;
Call TRex();
Skip several calls;
Enter user input to choice;
TRHealth is 5, so if statement is ignored;
PHealth is -3, PHealth <= 0 is true;
Display:
"You were killed!
XP gained: 0
"
PKill = 1;
PHealth = 1;
Call MAINPROGRAM();
Create CLASS Object CO;
Create string variable choice;
Display:
"
DINOSAUR ARENA
Type in the number next to the choice
1) Enter Game
2) Help
3) View Stats
4) Save game progress
"
Enter user input to choice;
If user enter's 4, Call CO function SAVEG();
Construct new CLASS();
Open stats.txt to read from;
Save information as local XP, NumDef, and PKill;
MAINPROGRAM Deletes new CO Object;
MAINPROGARM Terminates;
Infinite loop is still going on;
Enter user input to choice;
TRHealth is 5, so if statement is ignored;
PHealth is 100 so if statement is ignored;
If user entered 1, choice == "1" is true;
Display:
"
You used the shotgun!
Your health: 100
"
And so on.
As you can see, your program makes very little sense, and I'm not spending the next several hours just typing recursion over and over again. You need to remove the infinite loops from your programs, you also need to stop calling the functions over and over again. Like I said, you need to redesign your program and try it all over again.
I won't write your entire program for you tonight, but I suggest writing down on a piece of paper exactly what you want your complete program to do. I'm not sure if you noticed it, but there is a lot of lines in there what don't need to be. You're also somewhat abusing the idea of a class. Anyways, don't even look at this code, walk away from it, and design from scratch. If you're still having issues after I get out of work tomorrow night and no one else has helped you, I'll show you how to do it.
Ok i tried to re write it and it ended up looking very similar, i honestly dont know what to do, ive never been taught any different, actually ive never been taught how to structure programs at all. if that is even a teachable thing.