My c++ calculator script won't work. Please help.

Joined
Feb 3, 2023
Messages
4
Reaction score
0
I am very new to c++ and have been working on a simple four function calculator. To choose an operation, you type a number (1 through 4) and hit enter. I used if and else if statements to process what operation has been selected, but it always acts like I pressed 1. It might be my compiler, but I don't know. I have attached a text file containing the code that I used. Any feedback would be appreciated. Thanks!
 

Attachments

  • Calc.txt
    1.2 KB · Views: 14
Joined
Jan 30, 2023
Messages
107
Reaction score
13
I am very new to c++ and have been working on a simple four function calculator. To choose an operation, you type a number (1 through 4) and hit enter. I used if and else if statements to process what operation has been selected, but it always acts like I pressed 1. It might be my compiler, but I don't know. I have attached a text file containing the code that I used. Any feedback would be appreciated. Thanks!
In C++, the assignment operator = is used to assign a value to a variable. To compare values, use the comparison operator ==. In your code, the if and else if conditions are always evaluating to true because you are assigning values to a instead of comparing them.

Here i have rewrite you code :

C++:
#include <iostream>

using namespace std;

int main()
{
    int operation;
    double num1, num2, result;

    cout << "Select an operation: 1) Addition, 2) Subtraction, 3) Multiplication, 4) Division: ";
    cin >> operation;

    cout << "Enter the first number: ";
    cin >> num1;
    cout << "Enter the second number: ";
    cin >> num2;

    if (operation == 1)
    {
        result = num1 + num2;
        cout << "The sum is: " << result << endl;
    }
    else if (operation == 2)
    {
        result = num1 - num2;
        cout << "The difference is: " << result << endl;
    }
    else if (operation == 3)
    {
        result = num1 * num2;
        cout << "The product is: " << result << endl;
    }
    else if (operation == 4)
    {
        result = num1 / num2;
        cout << "The quotient is: " << result << endl;
    }
    else
    {
        cout << "Invalid input. Try again." << endl;
    }

    return 0;
}

Try it now
 
Joined
Nov 24, 2022
Messages
73
Reaction score
7
And remember about memory of operations. Undo several times would be nice. And of course not only outputs but whole user actions, so there would be possibility to analize what he tryied to ,wheter he suceded or not, time while he did his operations.
Had the same task while I was at university and didn't even try to do it(we had 2 hours for it - of course not expanded as in my version, I don't write half of programs).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top