I have a function, kinda like a main menu. But no matter what it always picks the first statement, even if it doesn't match the parameters of it's if statement, so it nevers get to the exit game function. So I can't use my close game function, and I would like some help on how to fix it so that it will not just automatically pick the first statement.

Tldr: Main menu function is skipping else if statments and picking the first option.


Here's my code:

#include <iostream>
#include <fstream>
#include <Windows.h>
#include <cstdlib>
#include "battle.h"







using namespace std;

extern char name[50];
char mainmenu[50];
char game[50];
extern int strength;
extern int intelligence;
extern int dexterity;
extern int health;
extern int mana;
extern int damage;
extern int defense;
int k;
extern int experience;
int levelup;

int main () {
	HANDLE hConsole;
int k;
health = 20;
mana = 10;
strength = 5;
intelligence = 5;
dexterity = 5;
defense = 5;
 
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

k = 12;
    
  SetConsoleTextAttribute(hConsole, k);
  
	cout <<"Main menu:  New game, Load game, Exit game" <<endl;
	cin.getline(mainmenu, 50);
	if (mainmenu == "New" || "new" || "New Game" || "new game" || "New game" || "new Game" || "newgame" || "Newgame")
	{
		system("Cls");
	cout <<"Hello, what may I call you by?" <<endl;
	cin.getline (name, 50);
	cout <<"Hello " <<name <<", and welcome to the world of Zelanthia." <<endl;
	Sleep(1500);
	cout <<"Our world has been horribly overrun with monsters, and we need you to help!" <<endl;
	Sleep(2500);
	cout <<"Here is a practice fight for you.";
	
	Sleep(5000);
	battleFunction();
	
	}
	else if (mainmenu == "Exit" || "exit" || "Exit Game" || "exit game" || "Exit game" || "exit Game" || "exitgame" || "Exitgame")
         {
system("exit");
return 0;


}







	return 0;
}

Recommended Answers

All 3 Replies

Line 47: if (mainmenu == "New" || "new" || "New Game" || "new game" || "New game" || "new Game" || "newgame" || "Newgame")

- mainmenu == "New" <-- is a condition
- "new" <-- is NOT a condition
- "New Game" || "new game" || "New game" || "new Game" || "newgame" || "Newgame" <-- none of these are conditions
At least not the way you're expecting them. Unfortunately for you, compiler has interpreted them such.

The condition in the if statement will always be "true". It is due the operator precedence. The operator "==" will be evaluated before the || operators so whatever value mainmenu holds the statement will be true.

For example lets say you have this statement:

if (mainmenu == "New" || "new")

and lets say that the value of mainmenu is "some text"
mainmenu = "some text"
-> mainmenu == "New" will be false
-> "new" will be truth (you can check this by trying if("new") - the statement
will always be truth)
-> in the end you get (false || truth) and the result of this is truth

If you want the statement to work the proper way you could write something like this:

if (mainmenu == "New" || mainmenu == "new")

Thanks for that, I know I asked this question earlier sometime, just wanted to clarify as to why it wasn't working. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.