0

I am new and beginner in programming and dont quiet understand how to post my question here. Hope this works. Anyways, im starting from basic and just learnt the "if" statement. But when i tried to create my own version its not working. My program below is showing all three cout results even if i enter only one option(attack or run or hide). It was working fine when there was just one "if" statement. I tried the "else if" too but then it only printed out the result "you have attacked" no matter what i chose. :( I used the search bar for similar questions that might have already been answered but didnt find much that could help me. If there are similar questions then i'd appreciate if you could point me towards it, although i'd really be grateful if you could point out what my mistakes are specifically. Thnx~

#include <iostream>
#include <string>

using namespace std;

int main(){

cout<<"Welcome to the jungle!!!"<<endl;
cout<<"--------"<<endl;
cout<<"Enemies approaches! \n Choose your next move! \n";
cout<<"Attack / Run / Hide \n\n\n";

string choice;
cin>>choice;

if(choice=="Attack"||"attack")
{
    cout<<"You have attacked!"<<endl;
}
if(choice=="Run"||"run")
{
    cout<<"You start running!"<<endl;
}

if(choice=="Hide"||"hide")
{
    cout<<"You hide in a cave!"<<endl;
}

return 0;
}
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
Sanrai
  • 5
  • 2

3 Answers3

1
if(choice=="Attack"||"attack")

does not mean "if choice contains "Attack", or choice contains "attack", do the thing".

It means "if choice contains "Attack", or "attack" is true, do the thing".

In pseudo-code, you expect:

if choice is the string "Attack"
    or choice is the string "attack"
        do a thing

but the or splits the statement into halves, and the second half doesn't mention the variable choice at all. It breaks down more like

if choice is the string "Attack"
    or the string "attack" on its own is somehow true, whatever that might mean
        do a thing

Since "attack" is a pointer to a char array, being true means it is not NULL, which is always the case, so this branch is always entered.

To express what you mean, write instead:

if(choice=="Attack"||choice=="attack")
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
  • Thanks for the explanation. Although i dont quiet understand it all atm,i am sure i will get it later on. :) – Sanrai Jul 10 '17 at 09:31
  • Got it. :) Thank you everyone for your help. – Sanrai Jul 10 '17 at 09:52
  • Also, please reconsider your use of what are often considered bad practices: [`using namespace std;`](http://stackoverflow.com/q/1452721/1171191) and [`endl`](http://chris-sharpe.blogspot.co.uk/2016/02/why-you-shouldnt-use-stdendl.html) (those are links to explanations). – BoBTFish Jul 10 '17 at 09:58
  • Ah,I think i understood those links a bit....might have to go through again for some time. – Sanrai Jul 10 '17 at 10:25
  • quick question and sry for goin off topic a bit. But it seems i need to put std:: in front of string(tried in my above program), whereas int doesnt need std (i assumed its needed cuz both are datatype but i get error if i put std::int). Is there a link or something which shows which terms i need to put std:: in front of? thnx – Sanrai Jul 10 '17 at 10:43
  • @Sanrai [Types that are built into the language do **not** need a namespace](http://en.cppreference.com/w/cpp/language/types). [Types from the Standard Library **do** need the namespace `std` prefix](http://en.cppreference.com/w/) (e.g. [`std::string`](http://en.cppreference.com/w/cpp/string)). If you use types from other libraries, they will probably have a different namespace, e.g. [`boost::any`](http://www.boost.org/doc/libs/1_64_0/doc/html/boost/any.html). – BoBTFish Jul 10 '17 at 11:02
0

You must use

if(choice=="Attack"||choice=="attack")
{
    cout<<"You have attacked!"<<endl;
}

OR( || ) is a boolean operator. So Anything other than 0 gives an impression of 1 in boolean. Thus the syntax in if code is always executed. This is the reason for in the else if case first statement is always executed (and further statements are skipped).

Also, using namespace std; is a bad practice since it makes our code confined to just one std library. instead you must use std:cout;

  • Oh i see now what was goin with "else if". thanks. uh using namespace std is bad practice? – Sanrai Jul 10 '17 at 09:48
  • Yes, because as a developer you will be working with many namespaces. Using namespace std confines you to just one of them. You could read about them here: https://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm –  Jul 10 '17 at 11:43
  • @Sanrai if you found my answer useful please upvote it :) –  Jul 13 '17 at 23:49
0

Welcome to C/C++ world.

As you have already read in tutorials or book, the content inside the parens of an if has to be a boolean value. Now, a lot of stuff can be converted automatically to bool and as a condition for an if. In particular, anything that can be converted to an int will, in turn, have a bool value attached to it. Pointers are of such kind.

  • If the value is different from 0 then the result will be -> true
  • 0 otherwise.

Writing if(choice=="Attack"||"attack") you are telling the compiler to execute the content of the if either if the content of choice is equal to the string Attack or if the the pointer to the underlying const char* attack is not zero.

attack is a valid string which mean its value is not zero which in turn means you are getting a true out of it. Same goes for all the others ifs. The second part of all you ifs are true cause they are not null pointers.

Change the all the ifs as the following: if(choice=="Attack"||choice=="attack") and the problem will be fixed.

Davide Spataro
  • 7,319
  • 1
  • 24
  • 36
  • Ah.... i get it now i think. The ||"attack" is not read as string but as true or false. Thanks a lot. – Sanrai Jul 10 '17 at 09:44