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.
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.
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.
Re: Help with C++ Basic Program
Quote:
Originally Posted by
leon6576
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
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.
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.
Re: Help with C++ Basic Program
Quote:
Originally Posted by
leon6576
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?
Quote:
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
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
Re: Help with C++ Basic Program
Wow I wish I could write a program like that, and you call yourself a noob ?