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

Threaded View

  1. #1
    Join Date
    Nov 2008
    Posts
    6

    Help with C++ Basic Program

    Hey Guys Im a currently a noob at C++ and I am working on this program which compiles but does not run the way I want it. Its just a basic program It doesnt go past the Malik book in Chapter 4. Anyway here's the code.


    Code:
    #include <iostream>
    using namespace std;
    
    
    int main() 
    {
    	char OrcState;
    	char Retreat;
    	int OrcHealth = 15;
    	bool EnemyPos();
    	int EnemyHealth = 10;
    
    
    	cout << "Enemy has been spotter. What would you like to do: " << endl;
    	cout << "A = Attack Enemy" << endl;
    	cout << "P = Ignore Enemy and resume Patroling" << endl;
    	cin >> OrcState;
    
    		 switch(OrcState)
    		 {
    			case 'A':
    			case 'a':
    				
    				
    				while(OrcHealth != 4)
    				{
    					cout << "Attacking Enemy" << endl;
    					cout << endl;
    					cout << "Hitting Enemy over the head" << endl;
    					EnemyHealth = EnemyHealth - 1;
    					cout << endl;
    					cout << "Enemy is Attacking" << endl;
    					cout << endl;
    					cout <<"Enemy is Attacking with Punch" << endl;
    					OrcHealth = OrcHealth - 1;
    					cout << endl;
    					cout << "Enemy Health: " << EnemyHealth << endl;
    					cout << "Orc Health: " << OrcHealth << endl;
    					system("pause");	
    
    				}
    
    
    			if(OrcHealth < 5)
    			cout << "Health is Too Low!!!!! Run Away???" << endl;
    			cin >> Retreat;
    			switch (Retreat)
    			{
    			case 'y':
    			case 'Y': cout << "Health is Too Low!!!! Running Away to a safer Place"  << endl;
    				break;
    					
    			case 'n':
    			case 'N': cout << "Too Late to give up now. Must Finish This Fight" << endl;
    					  {
    					  	  while((Retreat != 'y') && (OrcHealth !=1))
    						  {
    						cout << "Attacking Enemy" << endl;
    						cout << endl;
    						cout << "Hitting Enemy over the head" << endl;
    						EnemyHealth = EnemyHealth - 1;
    						cout << endl;
    						cout << "Enemy is Attacking" << endl;
    						cout << endl;
    						cout <<"Enemy is Attacking with Punch" << endl;
    						OrcHealth = OrcHealth - 1;
    						cout << endl;
    						cout << "Enemy Health: " << EnemyHealth << endl;
    						cout << "Orc Health: " << OrcHealth << endl;
    						system("pause");		
    						  }
    						
    
    				
    					if (EnemyHealth < 7)
    					{
    					cout << "Enemy's Health is low!!! Enemy is Running Away" << endl;			
    					}
    
    					if(OrcHealth < 1)	
    					{
    					cout <<" YOU ARE DEAD!!!!" << endl;
    					}
    
    					if(EnemyHealth < 1)	
    					{
    					cout << "Enemy has been defeated. Returning to patrol position." << endl;	
    					}
    					  }
    			}				
    			return 0;
    
    				
    		case 'p':
    		case 'P':
    			cout << "ignoring enemy and continuing to patrol premises" << endl;
    
    			break;
    			}
     return 0;
    }

    What happens is that it only reponds to the OrcHealth Whenever it reaches its design number but for the EnemyHealth it doesn't respond it just keeps going until it reaches the other one. The whole program runs and compiles but when it gets to the part where EnemyHealth is suppose to reach a certain number it ignores it and continues. Any Help with this subject would be greatly appreciated.
    Last edited by leon6576; November 26th, 2008 at 01:54 PM. Reason: Add code tags

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