What am I doing wrong? I have been doing super simple C++ for a while but when I got the idea to actually try and make something with it, I wasn't sure where to start. But it's very basic on what it's got to do, I just don't have the knowledge to fix it and my book that I'm learning from doesn't help me.
Here's the code:
// Code Of Conduct.cpp
//
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int swingSword = 1;
int stabSword = 2;
int dodge = 3;
int kick = 4;
int enemyHealth = 100;
int playerHealth = 100;
int main()
{
char move;
do
{
cout << "What's your move?\n";
cout << "1 - Swing Sword\n";
cout << "2 - Stab with Sword\n";
cout << "3 - Dodge enemy attack\n";
cout << "4 - Kick enemy\n";
cout << "Your move: ";
cin >> move;
if(move = 1)
{
cout << "You swung your sword at your enemy.\n";
cout << "Your enemy takes 20 damage!\n";
enemyHealth -= 20;
}
if(move = 2)
{
cout << "You stabbed at your enemy with your sword.\n";
cout << "Your enemy takes 18 damage!\n";
enemyHealth -= 18;
}
if(move = 3)
{
cout << "You dodged your enemy's attack.\n";
cout << "Your enemy cunningly cut your side and you took 20 damage and began bleeding\n";
playerHealth -= 20;
playerHealth = --playerHealth;
}
if(move = 4)
{
cout << "You kicked your enemy.\n";
cout << "Your enemy stumbles back but takes no damage.\n";
}
else
{
cout << "You made an illegal move.\n";
}
if(playerHealth == 0)
{
cout << "You died. Game Over.\n";
return 0;
}
} while (move != 4 ||move != 3 ||move != 2 ||move != 1);
return 0;
}
The problem is in the end of the 'do' loop where I have tried to end it with a 'while' loop. I try to run the program as I have it but no matter what the user input is, everything repeats.
while (move != 4 ||move != 3 ||move != 2 ||move != 1);
This statement will always evaluate to true, because one of the conditions that you're testing for will always be true. There is no way that move can be equal to 1 and 2 and 3 and 4 all at the same time, so move will always be not equal to some of those values.
These are just plain ugly and not really scalable - what if you had 10 menu options?
The fact that a do loop always executes at least once should not be the motivation for preferring them over a while or for loop IMHO. K&R (who invented C) don't like them either. There are some situations where they are valid, but I don't think this is one of them. All 3 types of loops can be re-written in one of the others forms, so that is what I prefer to do with do loops - sometimes at the price of 1 extra variable.
I would have a boolean controlled while loop with a switch inside which has a quit case and default clause to deal with bad input:
void ShowMenu();
bool Quit = false;
char move = 'z';
while(!Quit) {
if(playerHealth == 0) {
cout << "You died. Game Over.\n";
return 0;
}
if (enemyHealth == 0) {
//your code here
}
ShowMenu();
cin >> move;
switch (move) {
case'1':
cout << "You swung your sword at your enemy.\n";
cout << "Your enemy takes 20 damage!\n";
enemyHealth -= 20;
break;
case'2':
//your code here
break;
case'3':
//your code here
break;
case'4':
//your code here
break;
case'5': //user wants to quit
Quit = true; //execution continues after the switch
//or do this to end program
//return 0;
break;
default:
std::cout << "Bad Input, Try again " << std::endl;
break;
}
}
return 0;
} //end of main
void ShowMenu() {
cout << "What's your move?\n";
cout << "1 - Swing Sword\n";
cout << "2 - Stab with Sword\n";
cout << "3 - Dodge enemy attack\n";
cout << "4 - Kick enemy\n";
cout << "5 - Quit\n\n";
cout << "Your move: " << endl; //flush buffer with endl occasionally
}
Seen as there is only a small amount of code in the switch cases, I just left it there, but normally one would call a function from each of the cases if there was more than say 10LOC.
I'm not very far in C++ so I'm not familiar with using 'void' things.
But I noticed when I tried to see what your code did, Visual Studio 10 had complaints about declarations.
Was there supposed to be #include <iostream> or usingnamespace std;?
The 'switch' is something I have learned recently and have a better understanding of it than things I have learned earlier. I understood what it was doing but as I have said in my second paragraph;
complaints about declarations
Can anyone help me get my original code to a format I can understand, or do I need to go further into the book?
I didn't post all of the code - just enough to give you an idea. Clearly there has to be include's & main etc. I leave it up to you to insert my code into yours & make any necessary changes, although you should note that lines 10 -16 in your code won't be needed. Line 1 of my needs to go before main() in your code.
void is a return type and just means the function doesn't return anything. The ShowMenu function just prints out the menu, so doesn't need to return any value.
You are nearly there, have a go at inserting my code, then post it so we can see how you went.
@ superRazor:
First things first. I definitely prefer the coding of TheIdeasMan.
But you wanted keep your original coding, this is how I changed/editied your code: