-1

I am a beginner c++ programmer and this is one of my homework assignments and I got mostly everything done except this one last problem where when the user inputs a number divided by zero it should say "error" but instead I am getting inf as my output. I made an if statement that says if (num1 == 0 || num2 == 0) that it would say error but it is not!

#include <iostream>
#include <iomanip> 
using namespace std;



int main() {

    double num1 {};
    double num2 {};
    char input {};
    double result {};

    cout << "Enter your calculations: ";
    cin >> num1 >> input >> num2;

    cout << fixed << setprecision(2);

    if (input == '+') {
        result = num1 + num2;
    } else if (input == '-') {
        result = num1 - num2;
    } else if (input == '/') {
        result = num1 / num2;
    } else if (input == '*') {
        result = num1 * num2;
    } else if ( num1 == 0 || num2 == 0 ) 
        cout << "error";

     cout << "Answer: "<< result << endl;

    }
Nikhil Badyal
  • 1,589
  • 1
  • 9
  • 20
naustin288
  • 13
  • 1
  • 7
  • 6
    You need to check for zero before doing the division. – Brady Dean Nov 12 '19 at 23:27
  • 1
    Also you should test only `num2`. Dividing zero by a non-zero number is well-defined and gives zero. – walnut Nov 12 '19 at 23:29
  • 1
    `else if ( num1 == 0 || num2 == 0 )` is only checked if none of the above conditions were true. `else if (input == '/')` enters, so the zero-checking case does not run. – user4581301 Nov 12 '19 at 23:29
  • Fun fact: `else if (condition) {}` is actually `else { if (condition) {} }` More reading on that here: [Is “else if” a single keyword?](https://stackoverflow.com/questions/24373076/is-else-if-a-single-keyword) – user4581301 Nov 12 '19 at 23:32
  • @user4581301 Fun fact: it isn't. That's another way to write it, but it takes a different route in the parser to be so understood. – user207421 Nov 13 '19 at 00:24

3 Answers3

1

The divide by zero check should be within the division input block.

else if (input == '/') {
    if (num1 == 0 || num2 === 0) {
        cout << "error" << endl;
    }
}

In your code, since input is '/', any other else block will not be executed.

0

You can use std::isinf (1 / 0 => inf) and std::isnan (0 / 0 => nan) to check for infinity and Not-a-Number (regardless of which operation that caused it). You can also use std::isfinite to check that none of the previous two are true.

#include <cmath>     // std::isfinite
#include <iomanip>
#include <iostream>

int main() {
    double num1{};
    double num2{};
    char input{};
    double result{};

    std::cout << "Enter your calculations: ";
    std::cin >> num1 >> input >> num2;

    std::cout << std::fixed << std::setprecision(2);

    if(input == '+') {
        result = num1 + num2;
    } else if(input == '-') {
        result = num1 - num2;
    } else if(input == '/') {
        result = num1 / num2;
    } else if(input == '*') {
        result = num1 * num2;
    }

    std::cout << "Answer: ";

    if(std::isfinite(result))  // using std::isfinite
        std::cout << result;
    else
        std::cout << "error";

    std::cout << "\n";
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

You need to make sure your data is not 0 before you perform the operation.

One option is to move it to the beginning of your if statement like this:

if ( num1 == 0 || num2 == 0 ) {
    std::cout << "error";
} else if (input == '+') {
    result = num1 + num2;
} else if (input == '-') {
    result = num1 - num2;
} else if (input == '/') {
    result = num1 / num2;
} else if (input == '*') {
    result = num1 * num2;
} else { // if none of the "else"s apply, it might be nice to show an error
    std::cout << "error";
}

Or, if you only care about /, you can do it inside the division like this:

//...
} else if (input == '/') {
    if ( num1 == 0 || num2 == 0 ) {
        std::cout << "error";
    } else {
        result = num1 / num2;
    }
} else if (input == '*') {
//...