CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Mar 2017
    Posts
    12

    [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.

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
  •  





Click Here to Expand Forum to Full Width

Featured