Andrew
Andrew

Reputation: 17

Why does this C++ calculator keep showing me errors?

So I'm pretty new to C++ and I'm making a multiplication/division calculator. Everything was working fine when it was just one operation but now its giving me a bunch of errors

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    char operation;
    cout << "Select an operation devision(d) multiplication(m)" << endl;
    cin >> operation;

    float firstNumber;
    cout << "Please enter a number ";
    cin >> firstNumber;
  
    float secondNumber;
    cout << "\nEnter a second number ";
    cin >> secondNumber;
    
    float answer; 
        if (operation = m) {
        answer = firstNumber * secondNumber;
        }
        else if (operation = d) {
            answer = firstNumber / secondNumber;
            cout << "\nYour answer is " << showpoint << fixed << setprecision(2) << answer;
        }

    system("pause>0");
}

Near the bottom where it's calculating based on the operation you put in, it says there these errors:

<source>: In function 'int main()':
<source>:19:25: error: 'm' was not declared in this scope; did you mean 'tm'?
   19 |         if (operation = m) {
      |                         ^
      |                         tm
<source>:22:30: error: 'd' was not declared in this scope
   22 |         else if (operation = d) {
      |                              ^
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:19:25: error: 'm' was not declared in this scope; did you mean 'tm'?
   19 |         if (operation = m) {
      |                         ^
      |                         tm
<source>:22:30: error: 'd' was not declared in this scope
   22 |         else if (operation = d) {
      |                              ^

Upvotes: 0

Views: 67

Answers (1)

Bill the Lizard
Bill the Lizard

Reputation: 405715

The main problems are in the statement if (operation = m) where you're doing assignment (=) instead of comparison (==). This is also using the (non-existent) variable m instead of the value "m" or 'm'.

Once you fix that in the multiplication section, you have the same problems in the division section. You're also only printing the output in the division section, but that's not really a syntax error, but a logic error. Move that outside the if statement to print the answer regardless of the operation chosen.

Upvotes: 1

Related Questions