-
April 1st, 2017, 12:50 PM
#1
[RESOLVED] File loading help
I'm still a beginner to c++ and just learning how to read and write files. I've tried to add a way to save and load Monsters. The saving part saves it until the next time I run the program. And the loading part doesn't load anything (may have to do with the saving). Please Help
EDIT:
Snippets of code with loading and saving
creating the objects
Code:
//SAVED MONSTER OBJECT
ofstream saveMobj("Monsters.txt");
ifstream readMobj("Monsters.txt");
asking if you want to load
Code:
cout<<"do you want to load your Monster?? 1(yes)/0(no) \n";
cin>>LoadMonster;
if(LoadMonster == 0){
loading
Code:
//LOADING YOUR MONSTER
if(LoadMonster == 1){
cout<<"Loading 'em up! \n";
readMobj>>HeadM1>>BodyTM1>>BodyMM1>>BodyBM1>>NameM1>>LevelM1>>TypeM1>>HealthM1>>ArmourM1>>AttackM1;
readMobj.close();
}
saving
Code:
cout<<endl<<" do you want to save your monster? 1(yes)/0(no) \n";
cin>>saveInput;
if(saveInput == 1){
saveMobj<<Mhead[HeadM1]<<endl
<<Mbody1[BodyTM1]<<endl<<Mbody2[BodyMM1]<<endl<<Mbody3[BodyBM1]<<endl<<endl<<
" Name: "<<Mname[NameM1]<<endl<<
" Level: "<<LevelM1<<endl<<
" Type: "<<Mtype[TypeM1]<<endl<<
" Health: "<<HealthM1<<endl<<
" Armour: "<<ArmourM1<<endl<<
" Attack: "<<AttackM1<<endl;
saveMobj.close();
cout<<"saved! \n";
}
if(saveInput == 0){
cout<<" ok then... \n";
}
ENTIRE CODE
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
#include <conio.h>
#include <fstream>
using namespace std;
int randVal(int min,int max){
return (rand() % (max - min + 1) + min);
}
int main(){
srand(time(NULL));
int BodyTM1=0,BodyMM1=0,BodyBM1=0,HeadM1=0,ArmourM1=0,HealthM1=0,LevelM1=0,TypeM1=0,NameM1=0,AttackM1=0,ArmourPlusM1=0,AttackPlusM1=0; //variables for monster one
int BodyTM2=0,BodyMM2=0,BodyBM2=0,HeadM2=0,ArmourM2=0,HealthM2=0,LevelM2=0,TypeM2=0,NameM2=0,AttackM2=0,ArmourPlusM2=0,AttackPlusM2=0; //variables for monster two
int saveInput,LoadMonster;
//SAVED MONSTER OBJECT
ofstream saveMobj("Monsters.txt");
ifstream readMobj("Monsters.txt");
string Mhead[] = {"( 0 ^ 0 )","| 0 * 0 |","( =^_^= )","( > _ < )","(<@> <@>)","( >@ @< )"};
string Mbody1[] = {"1#####1","[[[ ]]]","$ $ ","| ^|^ |"};
string Mbody2[] = {"1#####1","[[[ ]]]","$ $ ","| ^|^ |"};
string Mbody3[] = {"1#####1","[[[ ]]]","| | "," _/|_ "};
string Mname[] = {"Abra","Aipom","Amaura","Arbok","Archen","Aron","Bagon","Beedrill","Beldum","Bayleef","baltoy","Cacnea","Cascoon","Delphox","CHARIZARD",
"Deino","Deerling","Ditto","Dragonair","Drampa","Drowzee","Dustox","Electrabuzz","Emboar","Fearow","Golem","Grimer","Guzzlord","Hypno","Inkay","Invysaur",
"Jolteon","Jynx","Koffinf","Lapras","Latias","Latios","Charmander","Mew","Nidioking","Nidoqueen","Onix","Pichu","Pikachu"};
string Mtype[] = {"Fire","Water","Rock","Air","Magic","Titanium"};
LevelM1 = randVal(1,100);
NameM1 = randVal(0,43);
cout<<"do you want to load your Monster?? 1(yes)/0(no) \n";
cin>>LoadMonster;
if(LoadMonster == 0){
cout<<" input for head, and don't forget to press enter... \n";
cout<<" (0) - ( 0 ^ 0 ) Type: Fire \n";
cout<<" (1) - | 0 * 0 | Type: Titanium \n";
cout<<" (2) - ( =^_^= ) Type: Water \n";
cout<<" (3) - ( > _ < ) Type: Rock \n";
cout<<" (4) - (<@> <@>) Type: Air \n";
cout<<" (5) - ( >@ @< ) Type: Magic \n";
cin>>HeadM1;
if(HeadM1 < 0 || HeadM1 > 5){
cout<<" Donchu try pulling that!! re-enter \n";
cin>>HeadM1;
if(HeadM1 < 0 || HeadM1 > 5){
cout<<" really...";
sleep(3);
return 0;
}
}
cout<<" Now, input... \n"<<" Light, has higher attacking power. Heavy, has higher armour and health \n";
cout<<" (0) - 1#####1 Type: Heavy \n";
cout<<" (1) - [[[ ]]] Type: Heavy \n";
cout<<" (2) - $ $ Type: Light \n";
cout<<" (3) - | ^|^ | Type: Light \n";
cin>>BodyTM1;
if(BodyTM1 < 0 || BodyTM1 > 3){
cout<<" Donchu try pulling that!! re-enter \n";
cin>>BodyTM1;
if(BodyTM1 < 0 || BodyTM1 > 3){
cout<<" really...";
sleep(3);
return 0;
}
}
cout<<" Now for the next layer (Again)..... \n";
cout<<" (0) - 1#####1 Type: Heavy \n";
cout<<" (1) - [[[ ]]] Type: Heavy \n";
cout<<" (2) - $ $ Type: Light \n";
cout<<" (3) - | ^|^ | Type: Light \n";
cin>>BodyMM1;
if(BodyMM1 < 0 || BodyMM1 > 3){
cout<<" Donchu try pulling that!! re-enter \n";
cin>>BodyMM1;
if(BodyMM1 < 0 || BodyMM1 > 3){
cout<<"really...";
sleep(3);
return 0;
}
}
cout<<" once more... \n";
cout<<" (0) - 1#####1 Type: Heavy \n";
cout<<" (1) - [[[ ]]] Type: Heavy \n";
cout<<" (2) - $ $ Type: Light \n";
cout<<" (3) - _/|_ Type: Light \n";
cin>>BodyBM1;
if(BodyBM1 < 0 || BodyBM1 > 3){
cout<<" Donchu try pulling that!! re-enter \n";
cin>>BodyBM1;
if(BodyBM1 < 0 || BodyBM1 > 3){
cout<< "really...";
sleep(3);
return 0;
}
}
//HEADS - HeadM1
if(HeadM1 == 0){
TypeM1 = 0;
}
if(HeadM2 == 1){
TypeM1 = 5;
}
if(HeadM1 == 2){
TypeM1 = 1;
}
if(HeadM1 == 3){
TypeM1 = 2;
}
if(HeadM1 == 4){
TypeM1 = 3;
}
if(HeadM1 == 5){
TypeM1 = 4;
}
//BODY
//HEALTH - BodyTM1
//HEAVY
if(BodyTM1 == 0 || BodyTM1 == 1){
HealthM1 = randVal(1,1150);
AttackPlusM1 = randVal(0,50);
}
//LIGHT
if(BodyTM1 == 2 || BodyTM1 == 3){
HealthM1 = randVal(1,1000);
AttackPlusM1 = randVal(0,100);
}
//ARMOUR - BodyMM1 && BodyBM1
//light
if(BodyMM1 == 2 || BodyMM1 == 3){
ArmourM1 = randVal(0,450);
}
//HEAVY
if(BodyMM1 == 0 || BodyMM1 == 1){
ArmourM1 = randVal(0,500);
}
//ARMOUR BOUNUS
//HEAVY
if(BodyBM1 == 0 || BodyBM1 == 1){
ArmourPlusM1 = randVal(0,100);
ArmourM1 += ArmourPlusM1;
}
//LIGHT
if(BodyBM1 == 2 || BodyBM1 == 3){
ArmourPlusM1 = randVal(0,50);
ArmourM1 += ArmourPlusM1;
}
//ATTACK - AT
//HEAVY
if(BodyBM1 == 0 || BodyBM1 == 1){
AttackM1 = randVal(1,500);
AttackM1 += AttackPlusM1;
}
//LIGHT
if(BodyBM1 == 2 || BodyBM1 == 3){
AttackM1 = randVal(1,550);
AttackM1 += AttackPlusM1;
}
//Special Types
if(TypeM1 == 4){
HealthM1 /= 2;
AttackM1 *= 2;
}
if(TypeM1 == 5){
HealthM1 *= 2;
AttackM1 /= 2;
}
}
//LOADING YOUR MONSTER
if(LoadMonster == 1){
cout<<"Loading 'em up! \n";
readMobj>>HeadM1>>BodyTM1>>BodyMM1>>BodyBM1>>NameM1>>LevelM1>>TypeM1>>HealthM1>>ArmourM1>>AttackM1;
readMobj.close();
}
cout<<"-------------------- \n";
cout<<" YOUR MONSTER \n \n";
cout<<Mhead[HeadM1]<<endl;
cout<<Mbody1[BodyTM1]<<endl<<Mbody2[BodyMM1]<<endl<<Mbody3[BodyBM1]<<endl<<endl;
cout<<" Name: "<<Mname[NameM1]<<endl;
cout<<" Level: "<<LevelM1<<endl;
cout<<" Type: "<<Mtype[TypeM1]<<endl;
cout<<" Health: "<<HealthM1<<endl;
cout<<" Armour: "<<ArmourM1<<endl;
cout<<" Attack: "<<AttackM1<<endl;
//SAVING YOUR MONSTER
cout<<endl<<" do you want to save your monster? 1(yes)/0(no) \n";
cin>>saveInput;
if(saveInput == 1){
saveMobj<<Mhead[HeadM1]<<endl
<<Mbody1[BodyTM1]<<endl<<Mbody2[BodyMM1]<<endl<<Mbody3[BodyBM1]<<endl<<endl<<
" Name: "<<Mname[NameM1]<<endl<<
" Level: "<<LevelM1<<endl<<
" Type: "<<Mtype[TypeM1]<<endl<<
" Health: "<<HealthM1<<endl<<
" Armour: "<<ArmourM1<<endl<<
" Attack: "<<AttackM1<<endl;
saveMobj.close();
cout<<"saved! \n";
}
if(saveInput == 0){
cout<<" ok then... \n";
}
sleep(4);
cout<<"-------------------- \n";
cout<<" HOSTILE MONSTER \n \n";
LevelM2 = randVal(1,100);
BodyTM2 = randVal(0,3);
BodyMM2 = randVal(0,3);
BodyBM2 = randVal(0,3);
HeadM2 = randVal(0,5);
NameM2 = randVal(0,43);
//HEADS - HeadM2
if(HeadM2 == 0){
TypeM2 = 0;
}
if(HeadM2 == 1){
TypeM2 = 5;
}
if(HeadM2 == 2){
TypeM2 = 1;
}
if(HeadM2 == 3){
TypeM2 = 2;
}
if(HeadM2 == 4){
TypeM2 = 3;
}
if(HeadM2 == 5){
TypeM2 = 4;
}
//BODY
//HEALTH - BodyTM2
//HEAVY
if(BodyTM2 == 0 || BodyTM2 == 1){
HealthM2 = randVal(1,1150);
AttackPlusM2 =randVal(0,50);
}
//LIGHT
if(BodyTM2 == 2 || BodyTM2 == 3){
HealthM2 = randVal(1,1000);
AttackPlusM2 = randVal(0,100);
}
//ARMOUR - BodyMM2 && BodyBM2
//light
if(BodyMM2 == 2 || BodyMM2 == 3){
ArmourM2 = randVal(0,450);
}
//HEAVY
if(BodyMM2 == 0 || BodyMM2 == 1){
ArmourM2 = randVal(0,500);
}
//ARMOUR BOUNUS
//HEAVY
if(BodyBM2 == 0 || BodyBM2 == 1){
ArmourPlusM2 = randVal(0,100);
ArmourM2 += ArmourPlusM2;
}
//LIGHT
if(BodyBM2 == 2 || BodyBM2 == 3){
ArmourPlusM2 = randVal(0,50);
ArmourM2 += ArmourPlusM1;
}
//ATTACK - AT
//HEAVY
if(BodyBM2 == 0 || BodyBM2 == 1){
AttackM2 = randVal(1,500);
AttackM2 += AttackPlusM2;
}
//LIGHT
if(BodyBM2 == 2 || BodyBM2 == 3){
AttackM2 = randVal(1,550);
AttackM2 += AttackPlusM2;
}
//Special Types
if(TypeM2 == 4){
HealthM2 /= 2;
AttackM2 *= 2;
}
if(TypeM1 == 5){
HealthM1 *= 2;
AttackM1 /= 2;
}
cout<<Mhead[HeadM2]<<endl;
cout<<Mbody1[BodyTM2]<<endl<<Mbody2[BodyMM2]<<endl<<Mbody3[BodyBM2]<<endl<<endl;
cout<<" Name: "<<Mname[NameM2]<<endl;
cout<<" Level: "<<LevelM2<<endl;
cout<<" Type: "<<Mtype[TypeM2]<<endl;
cout<<" Health: "<<HealthM2<<endl;
cout<<" Armour: "<<ArmourM2<<endl;
cout<<" Attack: "<<AttackM2<<endl;
sleep(4);
cout<<"-------------------- \n";
cout<<" !!!FIGHT!!! \n";
cout<<Mhead[HeadM1]<<" "<<Mhead[HeadM2]<<endl;
cout<<Mbody1[BodyTM1]<<" "<<Mbody1[BodyTM2]<<endl;
cout<<Mbody2[BodyMM1]<<" vs "<<Mbody2[BodyMM2]<<endl;
cout<<Mbody3[BodyBM1]<<" "<<Mbody3[BodyBM2]<<endl;
HealthM1 -= ArmourM2;
HealthM2 -= ArmourM1;
if(TypeM1 == 1 && TypeM2 == 3){
AttackM2 *= 1.2;
}
if(TypeM1 == 1 && TypeM2 == 0){
AttackM1 *= 1.2;
}
if(TypeM1 == 0 && TypeM2 == 1){
AttackM2 *= 1.2;
}
if(TypeM1 == 0 && TypeM2 == 2){
AttackM1 *= 1.2;
}
if(TypeM1 == 2 && TypeM2 == 0){
AttackM2 *= 1.2;
}
if(TypeM1 == 2 && TypeM2 == 1){
AttackM2 *= 1.2;
}
if(TypeM1 == 2 && TypeM2 == 3){
AttackM1 *= 1.2;
}
if(TypeM1 == 3 && TypeM2 == 1){
AttackM1 *= 1.2;
}
if(TypeM1 == 3 && TypeM2 == 2){
AttackM2 *= 1.2;
}
cout<<endl;
sleep(2);
while(HealthM1 > 0 && HealthM2 > 0){
cout<<" You used "<<Mtype[TypeM1]<<" attack for "<<AttackM1<<endl;
HealthM2 -= AttackM1;
cout<<" Their health is "<<HealthM2<<endl;
sleep(1);
cout<<" They used "<<Mtype[TypeM2]<<" attack for "<<AttackM2<<endl;
HealthM1 -= AttackM2;
cout<<" Your health is "<<HealthM1<<endl<<endl;;
sleep(2);
}
cout<<endl;
if(HealthM1 < 0 && HealthM2 > 0){
cout<<" YOUR MONSTER LOST \n";
cout<<" Their health is "<<HealthM2<<endl;
}
else if(HealthM2 < 0 && HealthM1 > 0){
cout<<" YOUR MONSTER WON!!!! \n";
cout<<" Your health is "<<HealthM1<<endl;
}
else{
cout<<"!DRAW! \n";
}
getch();
}
Also: I've tried to debug, but I'm still learning how to debug.
Last edited by 2kaud; April 1st, 2017 at 03:28 PM.
-
April 1st, 2017, 12:57 PM
#2
Re: File loading help
Originally Posted by TheRedSpy
... I've tried to add a way to save and load Monsters. The saving part works great! But sadly the loading part not so much. Instead it creating a new one.
1. Could you provide the code snippets showing how you save and how you load Monsters?
2. How did you check that "The saving part works great"?
3. Did you try to debug to see whether your "load" is really called and works or fails?
Victor Nijegorodov
-
April 1st, 2017, 01:29 PM
#3
Re: File loading help
Originally Posted by VictorN
1. Could you provide the code snippets showing how you save and how you load Monsters?
2. How did you check that "The saving part works great"?
3. Did you try to debug to see whether your "load" is really called and works or fails?
1:
creating the objects
Code:
//SAVED MONSTER OBJECT
ofstream saveMobj("Monsters.txt");
ifstream readMobj("Monsters.txt");
asking if you want to load
Code:
cout<<"do you want to load your Monster?? 1(yes)/0(no) \n";
cin>>LoadMonster;
if(LoadMonster == 0){
loading
Code:
//LOADING YOUR MONSTER
if(LoadMonster == 1){
cout<<"Loading 'em up! \n";
readMobj>>HeadM1>>BodyTM1>>BodyMM1>>BodyBM1>>NameM1>>LevelM1>>TypeM1>>HealthM1>>ArmourM1>>AttackM1;
readMobj.close();
}
saving
Code:
cout<<endl<<" do you want to save your monster? 1(yes)/0(no) \n";
cin>>saveInput;
if(saveInput == 1){
saveMobj<<Mhead[HeadM1]<<endl
<<Mbody1[BodyTM1]<<endl<<Mbody2[BodyMM1]<<endl<<Mbody3[BodyBM1]<<endl<<endl<<
" Name: "<<Mname[NameM1]<<endl<<
" Level: "<<LevelM1<<endl<<
" Type: "<<Mtype[TypeM1]<<endl<<
" Health: "<<HealthM1<<endl<<
" Armour: "<<ArmourM1<<endl<<
" Attack: "<<AttackM1<<endl;
saveMobj.close();
cout<<"saved! \n";
}
if(saveInput == 0){
cout<<" ok then... \n";
}
2: Actually (I'm writing this before I edit my post), I just noticed I saves it till the next I run the program and I'm checking by pulling up the .txt file which it saves to.
3: I tried, but I have no clue to to use it.
-
April 1st, 2017, 03:45 PM
#4
Re: File loading help
Code:
ofstream saveMobj("Monsters.txt");
ifstream readMobj("Monsters.txt");
The problem is here. ofstream opens the file for output and clears the file. Thus when you then come to read from it, the file is empty!
You can use fstream to open the file file for both input and output and use seekg() and seekp() to position the file pointer to where you want read/write to start from. See http://www.cplusplus.com/reference/fstream/fstream/
Or possibly easier, you open the file as ofstream in the code to write to the file then close it when write has finished and open the file as ifstream in the code to read from the file and close it when finished. That way the file is only open when needed.
Also note that its good practice to check that a file has been opened successfully before an attempt is made to read/write to it. See is_open().
PS If you write a string to a file via stream insertion (<<) and it contains white space chars (newline, space, tab) then you cannot read it back properly via sream extraction (>>) as string stream extraction stops when it finds a white space char.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 1st, 2017, 04:16 PM
#5
Re: File loading help
Originally Posted by 2kaud
Code:
ofstream saveMobj("Monsters.txt");
ifstream readMobj("Monsters.txt");
The problem is here. ofstream opens the file for output and clears the file. Thus when you then come to read from it, the file is empty!
You can use fstream to open the file file for both input and output and use seekg() and seekp() to position the file pointer to where you want read/write to start from. See http://www.cplusplus.com/reference/fstream/fstream/
Or possibly easier, you open the file as ofstream in the code to write to the file then close it when write has finished and open the file as ifstream in the code to read from the file and close it when finished. That way the file is only open when needed.
Also note that its good practice to check that a file has been opened successfully before an attempt is made to read/write to it. See is_open().
PS If you write a string to a file via stream insertion (<<) and it contains white space chars (newline, space, tab) then you cannot read it back properly via sream extraction (>>) as string stream extraction stops when it finds a white space char.
You fixed my code again! I changed ofstream to fstream and I realized it wasn't actually saving the values for the Monster rather the look and stats.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|