CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help with C++ Basic Program

    We must be getting a half dozen variations on this post every day now.

    First, use code tags. It's explained in the before you post sticky.

    Second, learn to use the debugger. Learn it now. You can't program even the simplest program without it. It's use is not optional for anybody of any experience level.

  3. #3
    Join Date
    Nov 2008
    Posts
    6

    Re: Help with C++ Basic Program

    Can you be more specific on the learning the debugger? Also I joined so that I can get help with programming As I go I will eventually learn the rules of the forum so If i didnt use code tags that was my bad, I will learn to use them and use them from now on every time I have a programming question.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help with C++ Basic Program

    Quote Originally Posted by leon6576 View Post
    Can you be more specific on the learning the debugger?
    Are you using Visual C++? If you are, the IDE has a "Debug" menu. Study what that does, as this is the debugger.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Nov 2008
    Posts
    6

    Re: Help with C++ Basic Program

    Yea Im using Visual Studio and I know the debugger basically allows you to compile the program and in doing so it will tell what the problem is and where is the location of said problem. The code that i printed above works well with the debugger and there are no compiler errors it runs just fine. But my problem is that instead of doing one thing when its suppose to it doesnt work.

    I wanted to react to both characters instead of waiting for one to reach its peak and then move on to the next line of code. This is just practice and I want to get it right before I move on to the next chapter. I think the first post is specific enough but if its not let me know and I could write it another way. If no one can help me then also let me know so that I can go on other forums and seek help there.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help with C++ Basic Program

    Your understanding of the debugger is incorrect. It allows you to watch your program while it runs. You can watch as it executes line by line. You can see the variables and determine why it's making the decisions it's making. It's an indispensable tool.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help with C++ Basic Program

    Quote Originally Posted by leon6576 View Post
    Yea Im using Visual Studio and I know the debugger basically allows you to compile the program and in doing so it will tell what the problem is and where is the location of said problem.
    No, and that is nowhere close to what the debugger does.

    The debugger allows you to run your program a single step at a time. By doing this, you can watch variables, set breakpoints, etc. and see what the values are at every single step. What if your program were 10,000 lines long, as most of ours are much larger than that? How would you fix a problem then?
    The code that i printed above works well with the debugger and there are no compiler errors it runs just fine. But my problem is that instead of doing one thing when its suppose to it doesnt work.
    That is exactly what a debugger is supposed to help you with. Just hit the F10 key and you will see what it does.

    Also, why don't you re-edit your post to use code tags? The code you posted is unreadable without them.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Nov 2008
    Posts
    6

    Re: Help with C++ Basic Program

    I tried searching how to use code tags but havent been able to find them. Anyway I decided to go another direction with the program and will just move on to chapter 5. I have a basic understanding and Hopefully that will be enough.

    Thanks for your help guys

  9. #9
    Join Date
    Nov 2008
    Posts
    4

    Re: Help with C++ Basic Program

    Wow I wish I could write a program like that, and you call yourself a noob ?

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