Input loop always does first option

There is a loop from lines 750-970 that has 2 options in an if statement for when you input a string for the variable "input". When it asks you to input something it does the first option of "run" no matter what you type in. Could I please have help in solving this issue? I removed lots of code and every z# function is a different area on a map.
<
#include "stdafx.h"
#include "Main.h"
#pragma warning(disable : 4996)

using namespace std;
random_device rd;
CONSOLE_FONT_INFOEX cfi;

//VARIABLES
string input;
int facing;
int z5location;
bool z1first = true;
bool searchWreck = true;
int Wood;
int Cloth;
int Hydration = 100;
bool Flag = false;
bool loop = true;
int hp = 25;
bool z3death = false;

int z3();

int z3() {
if (z3death == true) {
cout << "You look up the hill, and see a heap near the ruin." << endl;
Sleep(3200);
cout << "As you get near you realize it's a dead body. ";
Sleep(2400);
cout << "Your dead body." << endl;
Sleep(3500);
cout << "You collect the items from your corpse and depart." << endl;
Sleep(2500);
system("CLS");
Sleep(1500);
cout << "Where would you like to go?" << endl;
while (true) {
cin >> input;
if (facing == 2) {
if (input == "left" || input == "goleft") {
cout << "You hike down the hill, shaken to the bones by what you just experienced." << endl;
Sleep(3550);
Hydration -= 20;
hp++;
facing = 1;
z4();
break;
}
else if (input == "back" || input == "goback" || input == "backwards" || input == "gobackwards") {
cout << "You hike down the hill, shaken to the bones by what you just experienced." << endl;
Sleep(3550);
Hydration -= 20;
hp++;
facing = 4;
z1();
break;
}
else {
cout << "No" << endl;
}
}
else if (facing == 3) {
if (input == "right" || input == "goright") {
cout << "You hike down the hill, shaken to the bones by what you just experienced." << endl;
Sleep(3550);
Hydration -= 20;
hp++;
facing = 4;
z1();
break;
}
else if (input == "back" || input == "goback" || input == "backwards" || input == "gobackwards") {
cout << "You hike down the hill, shaken to the bones by what you just experienced." << endl;
Sleep(3550);
Hydration -= 20;
hp++;
facing = 1;
z4();
break;
}
else {
cout << "No" << endl;
}
}
}
}
system("CLS");
cout << "Your hydration is now at " << Hydration << "%." << endl;
Sleep(2600);
cout << "As you near the ruin atop the hill you get an aweful feeling..." << endl;
Sleep(3800);
system("CLS");
Sleep(1000);
cfi.cbSize = sizeof(cfi);
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 42;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
std::wcscpy(cfi.FaceName, L"DINPro-regular");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
cout << "Something is wrong." << endl;
Sleep(2200);
system("CLS");
cfi.cbSize = sizeof(cfi);
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 60;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
std::wcscpy(cfi.FaceName, L"DINPro-regular");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
cout << "VERY";
Sleep(1750);
cout << " WRONG." << endl;
Sleep(1750);
system("CLS");
cfi.cbSize = sizeof(cfi);
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 40;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
std::wcscpy(cfi.FaceName, L"DINPro-regular");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
cout << "Your vision becomes strained, everything is getting dark, FEND IT OFF!" << endl;
Sleep(4200);
cout << "You fall to your knees and look behind you..." << endl;
Sleep(2400);
cout << "Something is there." << endl;
Sleep(1800);
system("CLS");
cout << "It's like a shadow made of thick strands of matter, its like it's \nthere but not there, shaped like a man nearly 8 feet tall." << endl;
Sleep(6300);
cout << "What do you do?!" << endl;
Sleep(1000);
cout << "Dear god!";
Sleep(750);
cout << " WHAT DO YOU DO?!" << endl;
while (true) {
cin >> input;
if (input == "run" || input == "escape" || input == "runaway" || "getaway" || input == "flee") {
system("CLS");
cfi.cbSize = sizeof(cfi);
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 32;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
std::wcscpy(cfi.FaceName, L"DINPro-regular");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
cout << "ERROR" << endl;
Sleep(1000);
cout << ".-- .... .- \n- / ....\n .- ...- . / -.-- --- ..-\n / -.. --- -. . \n..--.." << endl;

z3death = true;
Sleep(200);
cfi.cbSize = sizeof(cfi);
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 32;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
std::wcscpy(cfi.FaceName, L"DINPro-regular");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
system("CLS");
if (facing == 2) {
z1();
break;
}
else if (facing == 3) {
z4();
break;
}
}
else if (input == "attack" || input == "fight" || input == "charge" || input == "killit" || input == "kill") {
system("CLS");
cout << "You jump to your feet and charge at the shadow full speed!" << endl;
Sleep(2600);
cout << "You take the most powerful punch you can muster and swing with all your might!" << endl;
Sleep(4000);
cout << "Poof. It's gone. Along with your energy." << endl;
Sleep(2500);
cout << "You fall down face first." << endl;
Sleep(2500);
system("CLS");
Sleep(5000);
cout << "'What happened...?'" << endl;
Sleep(1700);
cout << "You pat yourself clean, and walk away." << endl;
while (true) {
cin >> input;
if (facing == 2) {
if (input == "left" || input == "goleft") {
cout << "You hike down the hill, shaken by what you just experienced." << endl;
Sleep(3550);
Hydration -= 20;
hp++;
facing = 1;
z4();
break;
}
else if (input == "back" || input == "goback" || input == "backwards" || input == "gobackwards") {
cout << "You hike down the hill, shaken by what you just experienced." << endl;
Sleep(3550);
Hydration -= 20;
hp++;
facing = 4;
z1();
break;
}
else {
cout << "No" << endl;
}
}
else if (facing == 3) {
if (input == "right" || input == "goright") {
cout << "You hike down the hill, shaken by what you just experienced." << endl;
Sleep(3550);
Hydration -= 20;
hp++;
facing = 4;
z1();
break;
}
else if (input == "back" || input == "goback" || input == "backwards" || input == "gobackwards") {
cout << "You hike down the hill, shaken by what you just experienced." << endl;
Sleep(3550);
Hydration -= 20;
hp++;
facing = 1;
z4();
break;
}
else {
cout << "No" << endl;
}
}
}
}
else {
cout << "HURRY UP AND DO SOMETHING!" << endl;
}
}
return 0;
}
int main() {

cfi.cbSize = sizeof(cfi);
cfi.nFont = 0;
cfi.dwFontSize.X = 0;
cfi.dwFontSize.Y = 32;
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
std::wcscpy(cfi.FaceName, L"DINPro-regular");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
z1();
return 0;
}
>
Last edited on
There is a loop from lines 750-970


And which lines would those be?

Please use code tags. http://www.cplusplus.com/articles/z13hAqkS/


One can number the first line of code if it is a fragment, so you can get your posted code to start at line 750
Shouldn't the syntax be something like this in your 'if' statements ??

 
if ((input == "back") || (input == "goback")) 


not....
 
if (input == "back" || input == "goback") 
Last edited on
There is a loop from lines 750-970
The code you posted has less than 300 lines. WHen I try to compile it, I get numerous errors. It's very hard to help unless you post a program that can be compiled.

When it asks you to input something it does the first option of "run" no matter what you type in.

Looking through the code, I'll assume you're talking about this section:
1
2
3
        cin >> input;
        if (input == "run" || input == "escape" || input == "runaway" ||"getaway"
            || input == "flee") {
The problem is that the underlined expression is always true. It should be input == "getaway"

kavala wrote:
Shouldn't the syntax be something like this in your 'if' statements ??
if ((input == "back") || (input == "goback"))
== has higher precedence than || so the parentheses aren't necessary. However, they do make the intention clear and some programmers will do this routinely.
Shouldn't the syntax be something like this in your 'if' statements ??
if ((input == "back") || (input == "goback"))

== has higher precedence than || so the parentheses aren't necessary. However, they do make the intention clear and some programmers will do this routinely.


I thought this would be the case.
I always use the parentheses so no ambiguity.
Topic archived. No new replies allowed.