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?

Recommended Answers

All 2 Replies

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

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

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.