views:

166

answers:

2
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? Thanks!

+6  A: 

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');
}
ChaosPandion
Ooh! I didn't realize when using a decimal number as a char you had to put the quotes around it, good to know, thanks a lot bud!
blakejc70
@blakejc70 Since stop is a char and with C++ method overloading your call to cin.get is routed through to an input handling logic that will return a character code. So stop == '0' == 48 ( going by assumption of ASCII character set ).
David
+2  A: 

In this scenario, you pump gas one extra time after the user hits '0'. Assuming that this is not desired, you have what is known as an "off-by-one error." You can fix this (and eliminate the temporary variable) by rearranging your function as follows:

void GasPump::dispense()
{
    while (true) {
        cout << "Press any key, or enter to dispense.\n"
             << "Or press 0 to stop: \n";

        if (cin.get() == '0')
            break;

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

To avoid using a break statement, you can use the following construction:

bool GasPump::shouldDispenseGas()
{
    cout << "Press any key, or enter to dispense.\n"
         << "Or press 0 to stop: \n";
    return (cin.get() != '0');
}

void GasPump::dispense()
{
    while (shouldDispenseGas()) {
        gasDispensed = gasDispensed + gasDispensedPerCycle;
        charges = costPerGallon*gasDispensed;
        displayGasNCharges();
    }
}
Julius
Hey thanks for your help. I was wondering how to stop that, and I thought about using a break, but my textbook is telling me it's not good practice, although my professor never said that. What's your thoughts on this?
blakejc70
In general, I try to put the "break" logic into the comparison being down by the while or for statement. However, sometimes it's just easier to use a break. In this case, where it's incredibly clear what's going on, I don't think there's a problem with using break. That being said, you certainly can avoid using it while maintaining readability at the cost of writing a separate function (as shown in the edited response).
Julius
+1 The second version with the extra function gives me the warm fuzzies. :-)
Jeffrey L Whitledge