How can I break out of my do/while loop?

user173424 picture user173424 · Feb 27, 2010 · Viewed 9.5k times · Source
void GasPump::dispense()
{

        bool cont = true;
        char stop;

    do{
        cout << "Press any key, or enter to dispense.\n"
             << "Or press 0 to stop: \n";
        cin.get(stop);

        gasDispensed = gasDispensed + gasDispensedPerCycle;
        charges = costPerGallon*gasDispensed;
        displayGasNCharges();

        if(stop == 0)
            cont = false;

    } while(cont);

}

Doing an assignment, this is my first program to write with objects so bear with me. I just can't get the output of this code to turn out right. I need a way to get out of the loop, and what I'm using just isn't working. Any suggestions, hints or tips?

Answer

ChaosPandion picture ChaosPandion · Feb 27, 2010

Try comparing stop to the zero char.

stop == '0'

Also you can simplify your code by doing this.

void GasPump::dispense()
{
    char stop;

    do {
        cout << "Press any key, or enter to dispense.\n"
             << "Or press 0 to stop: \n";
        cin.get(stop);

        gasDispensed = gasDispensed + gasDispensedPerCycle;
        charges = costPerGallon*gasDispensed;
        displayGasNCharges();
    } while (stop != '0');
}