Ok I have been working on a basic calculator for C++. The only problem is that I don't know how to make it so if you divide by 0 it couts something. Anyone help. I know it would be possible with if/else statement. Heres the code so far.

#include <iostream>
using namespace std;
int main(){
cout << "Enter an Expression\n";
    int  first;
	int second;
	char function;
    int  answer;
    
    while (cin >> first >> function >> second) {
        switch (function) {
            case '+': answer = first + second; 
                      break;
            case '-': answer = first - second; 
                      break;
            case '*': answer = first * second; 
                      break;
            case '/': answer = first / second;
                      break;
            default : cout << "Only +, -, * or / allowed" << endl;
                      continue;
        }
        cout << "The Product is " << answer << endl << endl;
    }

	return 0;
}

Ok I have been working on a basic calculator for C++. The only problem is that I don't know how to make it so if you divide by 0 it couts something. Anyone help. I know it would be possible with if/else statement. Heres the code so far.

So you don't want to use an if-else statement? That seems like the easiest way. Or you want to use one but aren't sure how?

So you don't want to use an if-else statement? That seems like the easiest way. Or you want to use one but aren't sure how?

I would like to use one, but I am not sure how if you could help me out that would be great.

I would like to use one, but I am not sure how if you could help me out that would be great.

Set up two new variables. One, a boolean variable that is false if there are no errors and true if there are errors. The second should be a string variable that contains the error message. Before calculating the answer, check for errors. If there are no errors, calculate an answer. If there are, set the error variable to true and assign an error message. At the end, in the display, check that error flag. If it's false (no errors), display the answer. If it's true, display the error message.

#include <iostream>
#include <string>
using namespace std;
int main()
{
    cout << "Enter an Expression\n";
    int  first;
    int second;
    char function;
    int  answer;
    bool haveError;
    string errorMessage;
    
    while (cin >> first >> function >> second) 
    {
        haveError = false;
        switch (function) 
        {
            case '+': answer = first + second; 
                      break;
            case '-': answer = first - second; 
                      break;
            case '*': answer = first * second; 
                      break;
            case '/': if (/* some condition */)
                      {
                          haveError = true;
                          errorMessage = "Divide by Zero error";
                      }
                      else
                          answer = first / second;
                      break;
            default : cout << "Only +, -, * or / allowed" << endl;
                      continue;
        }
        if (/* check haveError variable */)
            // display answer
        else
            // display error message
    }
    
    return 0;
}

You can change the default branch of the switch statement to use the errorMessage and haveError variables or keep it the way it is. Similarly, you can have it so the error message is displayed in the '/' branch of the switch statement, rather than at the bottom as I have it, in the event of an error. In that case you wouldn't need these two variables and would use the continue statement in the '/' branch when there is an error, as you have in the default. I like doing it at the bottom without any continue statements, but it's personal preference.

Set up two new variables. One, a boolean variable that is false if there are no errors and true if there are errors. The second should be a string variable that contains the error message. Before calculating the answer, check for errors. If there are no errors, calculate an answer. If there are, set the error variable to true and assign an error message. At the end, in the display, check that error flag. If it's false (no errors), display the answer. If it's true, display the error message.

You can change the default branch of the switch statement to use the errorMessage and haveError variables or keep it the way it is. Similarly, you can have it so the error message is displayed in the '/' branch of the switch statement, rather than at the bottom as I have it, in the event of an error. In that case you wouldn't need these two variables and would use the continue statement in the '/' branch when there is an error, as you have in the default. I like doing it at the bottom without any continue statements, but it's personal preference.

I am still a beginner so please bear with me, but I am stuck with the

/* check haveError variable */

part.

I am still a beginner so please bear with me, but I am stuck with the

/* check haveError variable */

part.

Check whether haveError is true or false. If it's true, you want to display the error message. If it's false, there are no errors, so display the answer.

if (!haveError)  // haveError is false, so you don't have any errors
{
    // code to display the answer
}
else
{
    // code to display the error message
}

Thanks so much I finally got it working :)

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.