HiveBrain v1.2.0
Get Started
← Back to all entries
debugcppModerate

Dividing 2 numbers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numbersdividingstackoverflow

Problem

The problem is to input, divide 2 ints and display the result.

Please, review my program. It worth noting, I've intentionally used goto, not while.

int main(){
    int a, b;

ab_input:
    cout > a >> b;
    try {
        if (!cin) throw std::runtime_error("Bad input");
        if (b == 0) throw std::runtime_error("Dividing by zero");
    } 
    catch (std::runtime_error er) {
        cin.sync();
        cin.clear();
        std::cerr > c;
        if (cin && c == 'y') goto ab_input;
        else return -1;
    }
    cout << "a/b = " << a / b << endl;

    return 0;
}

Solution

Some alternatives to goto:

int main(){
    int a, b;

    for (;;) { // until success
        cout > a >> b;
        try {
            if (!cin) throw std::runtime_error("Bad input");
            if (b == 0) throw std::runtime_error("Dividing by zero");
            break; // success!
        } 
        catch (const std::runtime_error& ex) {
            cin.sync();
            cin.clear();
            std::cerr > c;
            if (cin && c == 'y') continue; // try again!
            else return -1;
        }
    }
    cout << "a/b = " << a / b << endl;

    return 0;
}


or:

static bool tryAgain(const char* errorMessage){
    cin.sync();
    cin.clear();
    std::cerr > c;
    return (cin && c == 'y'); // try again!
}

int main(){
    int a, b;

    const char* errorMessage;
    do {
        cout > a >> b;
        // set errorMessage.
        if (!cin) errorMessage = "Bad input";
        else if ((b == 0)) errorMessage = "Dividing by zero";
        else errorMessage = 0;
        if (errorMessage && !tryAgain(errorMessage)) return -1; // fail!
    } while (errorMessage);
    cout << "a/b = " << a / b << endl;

    return 0;
}


Or:

int main(){
    int a, b;

    for (;;) { // Until explicit break on success.
        cout > a >> b;
        // set errorMessage.
        const char* errorMessage;
        if (!cin) errorMessage = "Bad input";
        else if ((b == 0)) errorMessage = "Dividing by zero";
        else break; // success!
        if (!tryAgain(errorMessage)) return -1; // fail!
    }

    cout << "a/b = " << a / b << endl;

    return 0;
}


Also you might like to change the last statement to:

cout << "a/b = " << (double)a / b << endl;



I would also mention that returning -1 is normally a bad idea. The shell interprets negative numbers in weird ways

All of the Exit status - References suggest 0 for success and non-zero (but positive) for failure:

  • Some (Unix and DOS) references suggest that the range of valid codes is 0..255



  • Another (HP OpenVMS) says that bits 29..31 are reserved and must be zero



  • The Windows System Error Codes range up to 15999 (I guess you might want to return something like ERROR_INVALID_PARAMETER).

Code Snippets

int main(){
    int a, b;

    for (;;) { // until success
        cout << "Enter 2 numbers: ";
        cin >> a >> b;
        try {
            if (!cin) throw std::runtime_error("Bad input");
            if (b == 0) throw std::runtime_error("Dividing by zero");
            break; // success!
        } 
        catch (const std::runtime_error& ex) {
            cin.sync();
            cin.clear();
            std::cerr << ex.what() << endl;
            cout << "Try again? y/n: ";
            char c; 
            cin >> c;
            if (cin && c == 'y') continue; // try again!
            else return -1;
        }
    }
    cout << "a/b = " << a / b << endl;

    return 0;
}
static bool tryAgain(const char* errorMessage){
    cin.sync();
    cin.clear();
    std::cerr << errorMessage << endl;
    cout << "Try again? y/n: ";
    char c; 
    cin >> c;
    return (cin && c == 'y'); // try again!
}

int main(){
    int a, b;

    const char* errorMessage;
    do {
        cout << "Enter 2 numbers: ";
        cin >> a >> b;
        // set errorMessage.
        if (!cin) errorMessage = "Bad input";
        else if ((b == 0)) errorMessage = "Dividing by zero";
        else errorMessage = 0;
        if (errorMessage && !tryAgain(errorMessage)) return -1; // fail!
    } while (errorMessage);
    cout << "a/b = " << a / b << endl;

    return 0;
}
int main(){
    int a, b;

    for (;;) { // Until explicit break on success.
        cout << "Enter 2 numbers: ";
        cin >> a >> b;
        // set errorMessage.
        const char* errorMessage;
        if (!cin) errorMessage = "Bad input";
        else if ((b == 0)) errorMessage = "Dividing by zero";
        else break; // success!
        if (!tryAgain(errorMessage)) return -1; // fail!
    }

    cout << "a/b = " << a / b << endl;

    return 0;
}
cout << "a/b = " << (double)a / b << endl;

Context

StackExchange Code Review Q#45423, answer score: 12

Revisions (0)

No revisions yet.