Why is this case not blocked?

I just started trying C ++ a couple of weeks ago. I have a pretty decent grip on Java before trying to use C ++. Many people have told me that they are really similar in terms of syntax.

At the bottom there is a switch statement that initiates a battle scene. Whenever I choose a fight option, it just closes the program.

Here is my code:

#include "stdafx.h" #include <iostream> #include <cstdlib> // For rand() #include <string> #include <sstream> #include <algorithm> // transform() #include <cctype> // toupper(), tolower() #include <functional> // ptr_fun() #include <time.h> // PUT S*** BELOW THIS POINT //____________________________________________________________________________ using namespace std; int main() { /* Expirimental text based adventure game. * Mainly being used for practice methods. * Developed by Zack Cook. Generic title. Bad story. Bad interactions. Lots and lots of bad, bad code. Be warned. */ string charName; string charChoice; int charDecision; int playerHealth = 100; int randomNumber; int orcHealth = 100; srand (time(NULL)); cout << "_____________________________________________" << endl; cout << "| ##### ######### ### ### ### |" << endl; cout << "| ### ### ### ### ### ### ### |" << endl; cout << "| ### ### ### ### ##### |" << endl; cout << "| ### ### ######### ### |" << endl; cout << "| ### ### ### ### ### ### |" << endl; cout << "| ##### ### ### ### ### |" << endl; cout << "_____________________________________________" << endl; cout << "" << endl; cout << "" << endl; cout << "Welcome player. What is your name?" << endl; getline(cin, charName); yesOrNo: cout << "Hello " << charName << ". Are you ready to begin?" << endl; getline(cin, charChoice); transform( charChoice.begin(), charChoice.end(), charChoice.begin(), toupper ); cout << charChoice << endl; if(charChoice == "YES"){ cout << "Good. Let begin." << endl; } else if(charChoice == "NO"){ system ("exit"); } else { cout << "That is not a good answer." << endl; goto yesOrNo; } cout << "Our story begins with a wanderer named " << charName << " passing through the small town of Hark Pass." << endl; cout << "A little cozy village with no more than 30 or so home stayers.\nThe village men work hard on the farms during the day." << endl; cout << "The women cater to the children, and other house hold chores.\nIn the evening, most of the village turns to The Rusty Trough for a bit of drink." << endl; cout << "As the sun starts to set, our wanderer, " << charName << ", starts foot towards The Rusty Trough." << endl; cout << "As " << charName << " enters the tavern, a heavily drunken Orc man stumbles towards the entrance." << endl; cout << "\"I've never seen you 'round here!\" The orc says to our wanderer. \"I think it time to teach these adventure types what we really think about 'em\"" << endl; cout << "" << endl; cout << "What will you do?" << endl; cout << "1| Get ready to fight!" << endl; cout << "2| Call for help!" << endl; cout << "3| Try to run!" << endl; cout << "4| Do nothing at all!" << endl; cout << "5| Try to reason!" << endl; cin >> charDecision; switch(charDecision) { case '1': do{ cout << "FIGHT" << endl; randomNumber = rand() % 100 + 1; if(randomNumber >= 50){ orcHealth = orcHealth - (randomNumber - (randomNumber / 5)); cout << "You hit the orc! He now has " << orcHealth << " life left!" << endl; } else { playerHealth = playerHealth - (randomNumber - (randomNumber / 5)); cout << "The orc hit you! You now have " << playerHealth << " life left!" << endl; } }while(playerHealth || orcHealth != 0); break; default: break; } return 0; } 
+4
source share
4 answers

Your switch statement uses int charDecision with '1' , which is a char .

You read from standard input in int , which means that charDecision will contain 1. Then you compare this value 1 with '1' , which translates to 49 when you press int . This way your entry will never match (unless you enter 49).

Fix: compare with 1 or make charDecision a char .

+7
source

The variable charDecision declared as int .

The standard C ++ I / O stream classes (including cin and cout , by the way) are relatively intelligent and "do the right thing" depending on the type of variable you give it.

When the expression cin >> charDecision , cin reads everything that looks like a number and converts it into its own representation of the number. Thus, when you enter 1 in this menu, what is stored is literal 1. Your switch tests the letter character '1' , which has an integer value of 49, so it will not match, since 49! = 1.

You need to either change the charDecision type to char , or check the number 1 instead of the '1' character.

+5
source

while (playerHealth != 0 || orcHealth != 0) I think this is a problem.

0
source

Your main problem is to use goto, get rid of it!

 yesOrNo: cout << "Hello " << charName << ". Are you ready to begin?" << endl; getline(cin, charChoice); transform( charChoice.begin(), charChoice.end(), charChoice.begin(), toupper ); cout << charChoice << endl; if(charChoice == "YES"){ cout << "Good. Let begin." << endl; } else if(charChoice == "NO"){ system ("exit"); } else { cout << "That is not a good answer." << endl; goto yesOrNo; } 

can be replaced by

 do { cout << "Hello " << charName << ". Are you ready to begin?" << endl; getline(cin, charChoice); transform( charChoice.begin(), charChoice.end(), charChoice.begin(), toupper ); cout << charChoice << endl; if(charChoice == "YES"){ cout << "Good. Let begin." << endl; startGame();//it is the function where you should place your game logic, the "switch" block in your case //if you want to get back to main menu after the end of the game you can delete next line: break;//leave this loop } else if(charChoice == "NO"){ break;//leave this loop } else { cout << "That is not a good answer." << endl; } } while (true); 

You can also return to the main menu after the end of the game.
I also recommend that you divide the game logic into different functions, the code will be much easier to write and maintain. I would also recommend rewriting your game in an object-oriented way after you finish, this may be good practice for you. And forget about goto , it can turn any code into a piece of crap.

-2
source

Source: https://habr.com/ru/post/1497172/


All Articles