Jump to content

If statement problem

- - - - -

  • Please log in to reply
9 replies to this topic

#1
robbiewoods05

robbiewoods05

    Newbie

  • Members
  • PipPipPip
  • 40 posts
  • Learning:C++, C#, Haskell
Hey, I've been working on a text based game for two days now and I seem to have hit a snag. I've written an if statement to check if a bool is true or false but no matter what I cannot get it to register as false. I will paste the code for the if statement
 if (readyYesNo == "Yes" || "yes" || "y" || "Y" || "Yeah")

          {

                  areYouReady = true;

                    

          }

          else if (readyYesNo == "No" || "no" || "n" || "N")

          {

                  areYouReady = false;

          }       

          else { cout << "Please enter a valid response"; }

          if (areYouReady)

          {

                  cout << "Here we go...\n"; 

                  cin.get();

                  cout << "Teleporting...\n";

                  cin.get(); 

                  cout << "You wake up on a damp forest floor. The smell of rain fills your nostrils. You hear the sound of a river to your right.\n";

                  cin.get();

                  cout << "Enter a direction...";

                  cin >> movementDirection; 

                if (movementDirection == "right")

                {

                          cout << "You come across a murky river. You bend down to examine the river. You jump, startled, as you see a face that is not your own.\n"; 

                          system("pause");            

                }

                else if (movementDirection == "left")

                {

                     return 0; //Remember to add things here 

                }

                else if (movementDirection == "forward")

                {

                     return 0; // Remember to add things here

                }

                else if (movementDirection == "back")

                {

                     return 0; 

                }

                else { cout << "Enter a valid direction"; } 

If you could help me, that'd be great. Thanks.

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Sadly, if's don't work like this:

if (readyYesNo == "Yes" || "yes" || "y" || "Y" || "Yeah")

You'll have to modify them to:

if (readyYesNo == "Yes" || readyYesNo == "yes" || readyYesNo == "y" || readyYesNo == "Y" || readyYesNo == "Yeah")

I assume readyYesNo variable is an instance of std::string, if not then this won't work.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
robbiewoods05

robbiewoods05

    Newbie

  • Members
  • PipPipPip
  • 40 posts
  • Learning:C++, C#, Haskell
ohhhh thank you :L Yes it is a std string variable. This is my second day of c++ i've a little experience in c and objective c and quite a bit in c# but i just need a bit of a refresher, haven't done "manual" coding for a while

#4
robbiewoods05

robbiewoods05

    Newbie

  • Members
  • PipPipPip
  • 40 posts
  • Learning:C++, C#, Haskell
if (movementDirection == "right")

                {

                          cout << "You come across a murky river. You bend down to examine the river. You jump, startled, as you see a face that is not your own.\n";

                          system("pause");

                          cout << "You clear your head. You are facing west. A voice in your head says 'Go to the castle of Protox. It is in the Hydrangian mountain range to the north'";

                          cin.get():

                          cout << "Enter a direction: "; 

                          cin << movementDirection; 

                          if (movementDirection == MOVE_RIGHT)

                          {

                              

                          }   

                          else if (movementDirection == MOVE_LEFT)

                          {

                              

                          }

                          else if (movementDirection == MOVE_FORWARD)

                          {

                              

                          }

                          else if (movementDirection == MOVE_BACK)

                          {

                              

                          }

                          else { cout << "Please enter a valid direction." }

                }

                else if (movementDirection == MOVE_LEFT)

                {

                     return 0; //Remember to add things here

                }

                else if (movementDirection == MOVE_FORWARD)

                {

                     return 0; // Remember to add things here

                }

                else if (movementDirection == MOVE_BACK)

                {

                     return 0;

                }

                else { cout << "Enter a valid direction"; }
how would i make it so, if a user got to the "enter a valid direction" bit, how would i send them back to the start of the if statement?

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You are doing this:
cin << somevariable

The redirection has to be the other way around to place it in the variable.

Quote

how would i send them back to the start of the if statement?

It may be a little hard (you would have to rewrite the logic of your game) although a while loop should work fine as the game loop, while(finished), finished being a boolean of true or false.

You could check which level they are on each iteration of the loop, for example on level 3 you would check for input, and if they do an invalid input, do nothing and the loop will restart without having iterated the current level and thus restart it.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
robbiewoods05

robbiewoods05

    Newbie

  • Members
  • PipPipPip
  • 40 posts
  • Learning:C++, C#, Haskell
I'm sure << is out. You'd do
 cout << "Hello World!"; 
and
 cin >> helloWorld; 


Ahh I understand what you mean, but how would i implement it?

#7
bbqroast

bbqroast

    Codecall Addict

  • Members
  • PipPipPipPipPipPipPip
  • 548 posts
  • Location:/etc/passwd
Well you could have a loop running while loop = true, then once you are done with the loop you jut set loop to false.
Or you could write the code into a function...
This is so much better, this is like my second year of C++ (then again I'm a really lazy programmer!).
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#8
robbiewoods05

robbiewoods05

    Newbie

  • Members
  • PipPipPip
  • 40 posts
  • Learning:C++, C#, Haskell
How do you write a function in C++? I could do it in c# but when I tried doing the same code in C++ it didn't work. I know they are different languages but they are similar as they're a branch off of C

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

type indentifier ( parameters ) {

    body

}

Not much difference between C++ and C# functions. If I remember right, you have a ref keyword in C# for reference, in C++ this is done with & operator.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#10
robbiewoods05

robbiewoods05

    Newbie

  • Members
  • PipPipPip
  • 40 posts
  • Learning:C++, C#, Haskell
right, i wrote i function to set a bool to true and then a while loop so when said bool was true it'd continue with the game. Here's what i did:

bool levelComplete(bool& levelDone)

{

    levelDone = true;

    return levelDone;

}



int main()

{

    bool levelDone;

   

if (movementDirection == "right")

                {

                          cout << "You come across a murky river. You bend down to examine the river. You jump, startled, as you see a face that is not your own.\n";

                          system("pause");

                          cout << "You clear your head. You are facing west. A voice in your head says 'Go to the castle of Protox. It is in the Hydrangian mountain range to the north'";

                          cin.get(); 

 cout << "Enter a direction: ";

                          cin >> movementDirection;

                          if (movementDirection == MOVE_RIGHT)

                          {


                          }

                          else if (movementDirection == MOVE_LEFT)

                          {


                          }

                          else if (movementDirection == MOVE_FORWARD)

                          {


                          }

                          else if (movementDirection == MOVE_BACK)

                          {


                          }

                          else { cout << "Please enter a valid direction."; }

                          levelComplete(levelDone);

                          while (levelComplete)

                          {

                              cout << "hey";

                          }

                }

                else if (movementDirection == MOVE_LEFT)

                {

                     cout << "You look to your left, you see a thick wall of trees blocking your way. You decide to turn back";

                }

                else if (movementDirection == MOVE_FORWARD)

                {

                     return 0; // Remember to add things here

                }

                else if (movementDirection == MOVE_BACK)

                {

                     return 0;

                }

                else { cout << "Enter a valid direction"; }
however this doesn't give the required effect. Am I doing this wrong?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users