I have been using c++ for around an hour and a half but I am having problems with my calculator, It doesent work at all, it compiles though. here can you fix it up tell me what i did wrong.

// Calculator.cpp : Defines the entry point for the console application.
//

#include<iostream>
using namespace std;
int main()
{
    float a;
    int b;
    int c;
    int d;
    float e;
    cout<<"+\n";
    cout<<"-\n";
    cout<<"*\n";
    cout<<"/\n";
    cin>>a;
    if(a=='+')
    cout<<"enter number 2";
    cin>>b;
    cout<<"Number 1 and 2 is"<<(a+b);
    cin>>a;
    if(a=='-')
    cout<<"enter number 2";
    cin>>d;
    cout<<"Number 1 and 2 is"<<(a-c);
    cin>>a;
    if(a=='*')
    cout<<"enter number 2";
    cin>>d;
    cout<<"Number 1 and 2 is"<<(a*d);
    cin>>a;
    if(a=='/')
    cout<<"enter number 2";
    cin>>e;
    cout<<"Number 1 and 2 is"<<(a/e);
    system("pause")
    return 0;
}

Why have you declared a as float if you want to take the operator ie + - * / ? You should declare it as char .
Also, if a is the operator what is the meaning of a-b , a-c , a*d , and a/e ?
Looks like you dont have a clear idea of what to program. First write down what you want to do, what the user inputs should be, the sequence of user inputs. Then start programming.

can you fix it up for me, so i know what it should look like.

You should make your menu function look a little more like this:

cout<<"Please select which mathematical function you would like to do\n";
cout<<"1. Addition\n";
cout<<"2. Subtraction\n";
cout<<"3. Division\n";
cout<<"4. Multiplication\n";
cin>>a;
if (a==1)
 {
  cout<<"Enter your first number\n";
  cin>>firstnumber;
  cout<<"Enter your second number\n";
  cin>>secondnumber;
  firstnumber+secondnumber=result;
  cout<<"your result is "<<result<<" .";
  }

And do that for the rest except with firstnumber-,*,or/ secondnumber=result, and make sure to declare those integers above the if statement with your other variables. Also test this, I haven't done c++ in about a year now but if its wrong what is wrong is the order of the firstnumber+secondnumber=result; so if it says something about that make it result=firstnumber+secondnumber. But yeah its alot easier IMO to do menus with integers, you can use char's but its usually easier just to put a number by the menu item and say choose what you would like to do.

Just like what WolfPack said, +-/* is a character and therefore your variable a should declare as char. Beside that, (a+b) (a-c) (a*d) (a/e) in your coding has no meaning at all. The variable a is representing the operator(+-/*) thus it can't be use to show the result. Then you will need to declare another 2 different variables to store the value needed for the calculation. Not only that, I also noticed that your if statement doesn't have a curly bracket at all to show that particular body function is under which specific if condition. Only a single line body function statement that you can omit the curly bracket under if statement.

The method Bwell shows is one of the method of different logic which is useful also. Here is the part of the coding that I have made correction in your coding. Hope you understand where is your mistake.

char a;
int x;
int y;
 
cout<<"+\n";
cout<<"-\n";
cout<<"*\n";
cout<<"/\n";
 
cout<<"Choose an operator: "; 
cin>>a;
 
if(a=='+')
{
   cout<<"Enter number 1: ";
   cin>>x;
   cout<<"Enter number 2: ";
   cin>>y;
   cout<<"Addition of number 1 and 2 is"<<(x+y) << endl;
 }

Hope it helps.

I have been using c++ for around an hour and a half but I am having problems with my calculator, It doesent work at all, it compiles though. here can you fix it up tell me what i did wrong.


HI, YOU GOT PRETTY MUCH THE IDEA. FIND BELOW A SAMPLE USING THE + OPERATION.
READ THE COMMENTS AND ONCE YOU UNDERSTAND WHAT IS GOING ON HERE YOU SURE SHOULD BE ABLE TO FINISH YOUR PROGRAM. WHEN YOU ARE DONE YOU CAN POST IT AGAIN IF YOU WANT ME TO REVIEW IT.
LET ME KNOW IF YOU HAVE TROUBLE UNDERSTANDING THE SHORT PROGRAM BELOW AND I WILL EXPLAIN FURTHER ;)

// Calculator.cpp : Defines the entry point for the console application.
#include<iostream>
using namespace std;
int main()
{
char a; //USING CHAR DATA TYPE BECAUSE THE OPERATION TYPE IS A CHARACTER TYPE
 
int b=0; //INITIALIZE EVERYTHING TO 0
int c=0;
int d=0;
 
cout<<" + \n";
cout<<" - \n";
cout<<" * \n";
cout<<" / \n";
 
//LET THE USER KNOW WHAT HE/DHE SHOULD DO NEXT
cout<<"\nEnter type of operation: ";
cin>>a; //GET INPUT FROM USER
if(a=='+') //CONFRIM OPERATION TO BE PERFORMED
cout<<"\nEnter two numbers ";//PROMPT USER TO ENTER NUMB.
 
cin>>b>>c; //GET INPUT FROM USER 
//1ST NUMBER GOES IN b 2ND NUMBER GOES IN c
 
//WE PRINT OUT THE RESULT OF THE REQUESTED OPERATION
cout<<"\nThe result of --> "<<b<< " + "<< c<<" is = "<< (b+c);
 
return 0;
}
SAMPLE OUTPUT
 
+
-
*
/
Enter type of operation: +
Enter two numbers 2 6
The result of --> 2 + 6 is = 8

--------------------------------------------------------------------------


I have been using c++ for around an hour and a half but I am having problems with my calculator, It doesent work at all, it compiles though. here can you fix it up tell me what i did wrong.

// Calculator.cpp : Defines the entry point for the console application.
//
 
#include<iostream>
using namespace std;
int main()
{
    float a;
    int b;
    int c;
    int d;
    float e;
    cout<<"+\n";
    cout<<"-\n";
    cout<<"*\n";
    cout<<"/\n";
    cin>>a;
    if(a=='+')
    cout<<"enter number 2";
    cin>>b;
    cout<<"Number 1 and 2 is"<<(a+b);
    cin>>a;
    if(a=='-')
    cout<<"enter number 2";
    cin>>d;
    cout<<"Number 1 and 2 is"<<(a-c);
    cin>>a;
    if(a=='*')
    cout<<"enter number 2";
    cin>>d;
    cout<<"Number 1 and 2 is"<<(a*d);
    cin>>a;
    if(a=='/')
    cout<<"enter number 2";
    cin>>e;
    cout<<"Number 1 and 2 is"<<(a/e);
    system("pause")
    return 0;
}

you do the followin:
*declare two variables which act as two opeands
*a char variable that recieves the type of operation(+,-./,*)
*make use of switch statement and perform opeations as specified by the operator..........
its really simple....

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.