CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2017
    Posts
    12

    [RESOLVED] Why does ShipHealthequal one thing and then it equals some random number every time?

    I'm Making a text based Naval SIM (and is in VERY early stages), and I'm trying to set a ship's health, which works fine in the beginning of the program, but equals a random number. I have tried to debug (Dev-c++ debugger is kinda bad). I also have tried disabling class functions and adding a number to each time it outputs the health to narrow it down to where it's happening.
    Name:  BattleFleet bug.jpg
Views: 98
Size:  23.4 KB
    MY CODE:
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <unistd.h>
    using namespace std;
    
    	int randVal(int min,int max){
      		return (rand() % (max - min + 1) + min);
      	}
    
    class ShipCreation{
    	public:
    		
    		void CarrierPlayer(){
    			ShipClassSelect=1;
    			cout<<"  Level 1: (1) 12x AA Guns (2) 4x 78MM AT Guns (3) \n";
    			cin>>Level1Select;
    			cout<<"  Flight Deck: (1) 100x Fighters (2) 100x torpedo Bombers \n";
    			cin>>Level2Select;
    		}
    		void BattleShipPlayer(){
    			ShipClassSelect=2;
    			cout<<"  Level 1: (1) 2x 204 MM Cannons (2) 10x AA Guns \n";
    			cin>>Level1Select;
    			cout<<"  Level 2: (1) 4x 106 MM Cannons (2) 6x AA Guns \n";
    			cin>>Level2Select;
    		}
    		void ShipAssemble(){
    			if(ShipClassSelect==1){
    				
    				ShipHealth=3500;
    				cout<<ShipHealth<<" 1"<<endl;
    				ShipLookLevel1=LevelOne[0];
    				ShipLookLevel2=LevelTwo[0];
    				
    				cout<<"   YOUR SHIP \n \n";
    				cout<<ShipLookLevel2<<endl<<ShipLookLevel1<<endl;
    				
    			}
    			if(ShipClassSelect==2){
    				
    				ShipHealth=5000;
    				cout<<ShipHealth<<" 2"<<endl;
    				ShipLookLevel1=LevelOne[0];
    				
    				if(Level1Select==1){
    					
    					ShipLookLevel2=LevelTwo[1];
    				}
    				if(Level1Select==2){
    					
    					ShipLookLevel2=LevelTwo[2];
    				}
    				
    				cout<<"   YOUR SHIP \n \n";
    				cout<<ShipLookLevel2<<endl<<ShipLookLevel1<<endl;
    				
    				//NPC SHIP
    				if(NpcShipClass==1){
    					
    					NpcShipHealth=5000;
    				}
    				
    				if(NpcShipClass==2){
    					
    					NpcShipHealth==3500;
    				}
    			}
    		}
    		
    		int Level1Select,Level2Select;
    		int ShipClassSelect;
    		int ShipHealth;
    		string ShipLookLevel1,ShipLookLevel2;
    		
    		string LevelOne[1]={"|:::::::::::::::::::/"};
    		string LevelTwo[3]={"_____|----|__________","   ===[---]    [---]===","  \\\\  \\\\    // //"};
    		
    		int PlayerAAGuns=0,PlayerATGuns=0,PlayerFighters=0,PlayerTorpedo=0;//Carrier Guns
    		int MM204CannonsPlayer=0,MM106CannonsPlayer=0,x6AAGunsPlayer=0,x10AAGunsPlayer=0;//BattleShip Guns
    		
    		//NPC SHIP
    		int NpcShipClass=randVal(1,2);
    		int NpcLevel1=randVal(1,2);
    		int NpcLevel2=randVal(1,2);
    		int NpcShipHealth=0;
    		
    		int NpcAAGuns=0,NpcATGuns=0,NpcFighters=0,NpcTorpedo=0;//Carrier Guns
    		int MM204CannonsNpc=0,MM106CannonsNpc=0,x6AAGunsNpc=0,x10AAGunsNpc=0;//BattleShip Guns
    };
    
    class GameFunctions : public ShipCreation{
    	public:
    		
    		void Encounters(){
    			
    		}
    		
    		void ShipAttackSetup(){
    			
    			if(ShipClassSelect==1){//Carrier
    			
    				if(Level1Select==1){
    					
    					PlayerAAGuns=randVal(15,100);
    				}
    				if(Level1Select==2){
    					
    					PlayerATGuns=randVal(50,200);
    				}
    				
    				if(Level2Select==1){
    					
    					PlayerFighters=100;
    				}
    				if(Level2Select==2){
    					
    					PlayerTorpedo==100;
    				}
    			}
    			if(ShipClassSelect==2){//BattleShip
    				
    				if(Level1Select==1){
    					
    					MM204CannonsPlayer=200;
    				}
    				if(Level1Select==2){
    					
    					x10AAGunsPlayer=randVal(30,100);
    				}
    				
    				if(Level2Select==1){
    					
    					MM106CannonsPlayer=100;
    				}
    				if(Level2Select==2){
    					
    					x6AAGunsPlayer=randVal(10,100);
    				}
    			}
    			
    			//------------------NPC----------------------------------
    			
    			if(NpcShipClass==1){//Carrier
    				NpcShipHealth=3500;
    			
    				if(NpcLevel1==1){
    					
    					NpcAAGuns=randVal(15,100);
    				}
    				if(NpcLevel1==2){
    					
    					NpcATGuns=randVal(50,200);
    				}
    				
    				if(NpcLevel2==1){
    					
    					NpcFighters=100;
    				}
    				if(NpcLevel2==2){
    					
    					NpcTorpedo==100;
    				}
    			}
    			if(NpcShipClass==2){//BattleShip
    				NpcShipHealth=5000;
    				
    				if(NpcLevel1==1){
    					
    					MM204CannonsNpc=200;
    				}
    				if(NpcLevel1==2){
    					
    					x10AAGunsNpc=randVal(30,100);
    				}
    				
    				if(NpcLevel2==1){
    					
    					MM106CannonsNpc=100;
    				}
    				if(NpcLevel2==2){
    					
    					x6AAGunsNpc=randVal(10,100);
    				}
    			}
    		}
    		
    		void Battle(){
    			ShipAttackSetup();
    			cout<<ShipHealth<<" 3"<<endl;
    			do{
    				
    				cout<<endl;
    				
    				if(PlayerTorpedo||PlayerFighters>0){
    					
    					cout<<"  Our Aircraft are going in! \n";
    					sleep(3);
    					PlayerFighters-=x10AAGunsNpc;
    					PlayerFighters-=x6AAGunsNpc;
    					PlayerFighters-=NpcAAGuns;
    					PlayerFighters-=NpcFighters;
    					PlayerTorpedo-=x10AAGunsNpc;
    					PlayerTorpedo-=x6AAGunsNpc;
    					PlayerTorpedo-=NpcAAGuns;
    					PlayerTorpedo-=NpcFighters;
    					
    					if(PlayerTorpedo||PlayerFighters<=0){
    						
    						cout<<"  All fighters are down!! \n";
    					}else{
    						
    						cout<<"  Fighters Remaining:"<<PlayerFighters<<endl<<"  Torpedo Bombers Remaining: "<<PlayerTorpedo<<endl;
    					}
    					
    					NpcShipHealth-=PlayerFighters+PlayerTorpedo;
    					sleep(3);
    				}
    				
    				sleep(3);
    				
    				cout<<"  Prepare to fire! \n";
    				sleep(3);
    				cout<<"  FIRE!!!! \n";
    				sleep(3);
    				
    				NpcShipHealth-=MM106CannonsPlayer+MM204CannonsPlayer+PlayerATGuns;
    				
    				cout<<"  HIT!! \n"<<"  THEY'RE PREPARING TO FIRE!!! \n";
    				sleep(3);
    				
    				ShipHealth-=MM106CannonsNpc;
    				ShipHealth-=MM204CannonsNpc;
    				ShipHealth-=NpcATGuns;
    				
    				cout<<"  THEY HIT US!!! \n";
    				sleep(3);
    				
    				if(NpcFighters||NpcTorpedo>0){
    					
    					cout<<"  They're sending in their aircaft!! \n";
    					
    					NpcFighters-=PlayerAAGuns+x10AAGunsPlayer+x6AAGunsPlayer+PlayerFighters;
    					NpcTorpedo-=PlayerAAGuns+x10AAGunsPlayer+x6AAGunsPlayer+PlayerFighters;
    					
    					if(NpcFighters||NpcTorpedo<=0){
    						
    						cout<<"  We shot down all their aircraft! \n";
    					}
    					
    					ShipHealth-=NpcFighters+NpcTorpedo;
    					sleep(3);
    				}
    				
    				if(ShipHealth<=0){
    					
    					cout<<"  Abandon ship!! \n";
    				}
    				if(NpcShipHealth<=0){
    					
    					cout<<"  We sunk the Hostile Ship!! \n";
    				}
    				
    				cout<<"  Our health is: "<<ShipHealth<<"       Their Health is: "<<NpcShipHealth<<endl<<endl;
    				
    			}while(NpcShipHealth>0&&ShipHealth>0);
    		}
    		
    		void Explore(){
    			
    			int PortCoolDown=0;
    			cout<<ShipHealth<<" 4"<<endl;
    			for(int Fuel=100;Fuel>0;Fuel--){
    				while(ShipHealth>0){
    					cout<<ShipHealth<<" 5"<<endl;
    					int PlayerDecision;
    					cout<<"  Do you want to: (1) Set Sail (2) Head into port (3) View Ship \n \n";
    					cin>>PlayerDecision;
    					cout<<ShipHealth<<" 6"<<endl;
    					
    					if(PlayerDecision==1){
    						
    						for(int MovesPerTurn=10;MovesPerTurn>0;MovesPerTurn--){
    							
    							PortCoolDown++;
    							
    							if(PortCoolDown>10){
    								
    								PortCoolDown=10;
    							}
    							
    							int HostileChance=randVal(0,100);
    							int FindChance=randVal(0,100);
    							
    							cout<<"  Traveling \n";
    							
    							if(HostileChance>=80){
    								
    								cout<<"  Hostile ship encounterd!! \n";
    								sleep(3);
    								Battle();
    							}
    							
    							sleep(2);
    						}
    					}
    					if(PlayerDecision==2){
    						
    						if(PortCoolDown==10){
    							
    							cout<<"  Heading in to port for repairs! \n";
    							sleep(3);
    							cout<<"  Repaired \n";
    							
    							if(ShipClassSelect==1){
    								
    								ShipHealth=3500;
    							}
    							if(ShipClassSelect==2){
    								
    								ShipHealth=5000;
    							}
    						}else{
    							
    							cout<<"  We were just at the port! \n";
    						}
    					}
    				}
    			}
    		}
    };
    
    int main(){
    	
    	srand(time(NULL));
    
    	ShipCreation Cobj;
    	GameFunctions Gobj;
    	
    	int ShipSelect;
    	
    	cout<<"  Do you want a (1) Carrier (2) or a Battleship? \n";
    	cin>>ShipSelect;
    	
    	if(ShipSelect==1){
    		Cobj.CarrierPlayer();
    		Cobj.ShipAssemble();
    		cout<<Cobj.ShipHealth<<" 7";
    	}else if(ShipSelect==2){
    		Cobj.BattleShipPlayer();
    		Cobj.ShipAssemble();
    		cout<<Cobj.ShipHealth<<" 8";
    	}
    	
    	sleep(1);
    	cout<<endl;
    	
    	cout<<Cobj.ShipHealth<<" 9"<<endl;
    	Gobj.Explore();
    	
    	return 0;
    }

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Why does ShipHealthequal one thing and then it equals some random number every ti

    Code:
    ShipCreation Cobj;
    GameFunctions Gobj;
    ...
    cout<<Cobj.ShipHealth<<" 9"<<endl;
    Gobj.Explore();
    Gobj and Cobj are different variables - even though GameFunctions class is derived from ShipCreation class. Changes to Cobj have no effect on Gobj. So changes to Cobj::ShipHealth doesn't effect Gobj::ShipHealth as these are distinct variables. If you remove the variable Cobj and reference Gobj where Cobj is referenced in main() then it will work as expected.

    Note that in your code
    Code:
    NpcShipHealth == 3500;
    ...
    PlayerTorpedo == 100;
    You have equality test rather than the expected assignment!

    Also note that it isn't good practice to have class variables public. These are usually either private or protected if to be allowed to be changed by a derived class.

    PS. There are no constructors for the classes so the class variables aren't being initialised. It is good practice to initialise class variables to a known default value when a class is instantiated.

    Also rather than have test for 1, 2 etc through the code it is better if you define specific const variables for these values (eg battle, carrier etc) and use these names instead of 1 or 2. or you could use an enum.
    Last edited by 2kaud; April 20th, 2017 at 05:15 AM. Reason: PS
    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)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Why does ShipHealthequal one thing and then it equals some random number every ti

    (Dev-c++ debugger is kinda bad)
    What os are you using?
    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)

  4. #4
    Join Date
    Mar 2017
    Posts
    12

    Re: Why does ShipHealthequal one thing and then it equals some random number every ti

    Quote Originally Posted by 2kaud View Post
    What os are you using?
    Windows 10.

    Also, thanks for your other post, it fixed my issue. Thanks!

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: [RESOLVED] Why does ShipHealthequal one thing and then it equals some random numb

    If using Windows 10, why not use Visual Studio 2017 Community Edition. It's free from https://www.visualstudio.com/downloads/
    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)

  6. #6
    Join Date
    Mar 2017
    Posts
    12

    Re: [RESOLVED] Why does ShipHealthequal one thing and then it equals some random numb

    Quote Originally Posted by 2kaud View Post
    If using Windows 10, why not use Visual Studio 2017 Community Edition. It's free from https://www.visualstudio.com/downloads/
    Because I'm so used to using #include <unistd.h> (as well as other linux header files) and finding an alternative header file is kinda a pain. Also, I prefer to stay away from visual studio as I'm also on my main gaming machine and visual studio runs in the background. Another thing is, that uninstalling it is a real pain

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