Okay, I've just started learning C++ and I'm on the third chapter of my book which deals with control statements. Basically, I've learned how to create very basic text based games.

#include <iostream>
#include <string>

int main (void)
{
    using std::cout;
    using std::cin;
    using std::string;
    string options = "";
    bool light = false;
    do
    {
    cout << "You are trapped inside a room that is nearly bare. Aside from the \n"
    << "tiny candle, there is nothing lighting up the room. You can't see\n"
    << "very far, since the candle only allows to to see the door.\n";
    cin >> options;
    }
    while (!light);
    {
        if (options == "lightpaper" || options == "burnpaper" || options  == "feedflame" )
        {
            cout << "You light the paper on fire!";
            light = true;
        }
        else
            cout << "Everything remains dark";
    }
    return 0;
}

For some reason, it doesn't loop to "You are trapped inside a room (...)" if the first statement = false. Can anyone tell my why that is?

Edited 6 Years Ago by cosnersx: Need to change title to something more relevant

Your use of brackets indicates that you don't fully understand the do...while.
You're writing your code like:

do {
 ...something
} while( light == false );
{
 ...do something else
}

There are no statements in the do...while that will ever change the light boolean, and the part after the while( ... ); will only get executed once you break out of the do...while, which is never.

So you're probably looking to do something like:

cout << "you're in a dark room etc."
do {
    cout << "What would you like to do?\n";
    cin >> options;
    if( options == ... || options == ... ) {
        light = true;
        cout << "And there was light!\n";
    } else 
        cout << "The room stays as dark as it were\n";
    }
} while( light == false );

// Here, a correct option was given

Edited 6 Years Ago by thelamb: n/a

Thank you! I tested it and it worked! My book confuses me sometimes.

This question has already been answered. Start a new discussion instead.