I am trying to make a calculator in C++ but I keep running into errors. Please help me.

Here is the code:

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main()

{ //what the variables are
double calculation;
double sign;
double number1;
double number2;


    //the questions
cout<<"Enter a Number"<< endl;
cin>> number1;

cout<<"Enter Another Number"<< endl;
cin>> number2;

cout<<"Enter what you would like to do with these numbers"<< endl;
cout<<"For example * to multiply, / to divide, - to subtract, and + to add"<< endl;
cin>> sign;

    //the calculations and answer
    if  {sign=*} 
            calculation=number1*number2;
                cout<<number1<<"*"<<number2"="<<calculation<<endl;

    if  {sign=/}
            calculation=number1/number2;
                cout<<number1<<"/"<<number2"="<<calculation<<endl;

    if  {sign=-} 
            calculation=number1-number2;
                cout<<number1<<"-"<<number2"="<<calculation<<endl;

    else    {sign=+}   
                calculation=number1+number2;
                    cout<<number1<<"+"<<number2"="<<calculation<<endl;


return 0;
}

Recommended Answers

All 4 Replies

declare sign as a char and not a double cause it makes no sense to have sign defined as double when its a character you know? try that and it should work but say sign == '*' and ect for the other operators

Your compiler should choke on that input. The diagnostics may or may not be useful, you have not indicated.
A proper if construct looks something like:

if (condition) {
   // code for this condition
} else if (some_other_condition) {
   // code here
} else {
   // catch-all case in the if-else tree
}

In your case you want your conditions to look like variable == value or, more specifically, sign == '*'

Thank you all for your advice. I finally got it to work!

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.