#include <iostream>
#include <windows.h>

using namespace std;

float v1;
float v2;
int oper;
float ans;

int add()
{
    v1 + v2 == ans;
}

int sub()
{
    v1 - v2 == ans;
}

int mult()
{
    v1 * v2 == ans;
}
int div()
{
    v1 / v2 == ans;
    
}

int main()
{
    cout<< "Value 1 >>";
    cin>> v1;
    cout<< "Value 2 >>";
    cin>> v2;
    cout<< "Operator >>";
    cin>> oper;
    
    if (oper == '+')
    {
             add();
             cout<<ans;
             
             return(0);
             }
}

Is this a proper way to do a simple calculator like this? It works up to the if statment. It just closes and doesnt call the add int to assess the ans. Im new to programming and am curious what im doing wrong and if im even going about this the right way. Thanks

Recommended Answers

All 2 Replies

int add()
{
    v1 + v2 == ans;
}

You have the same problems for all of your functions. One, this line:

int add ()

If the word in red above is anything but void , you need to have the keyword return in your function. In this case, since you say you are returning an int, you need to return an integer. You don't here.

Second problem:

int add()
{
    v1 + v2 == ans;
}

== is the comparison operator, not the assignment operator. You probably wanted the assignment operator, which is =.

int add()
{
    v1 + v2 = ans;
}

but that won't work either. if what you are trying to do is assign ans to equal v1 + v2 , you'd want this:

void add()
{
    ans = v1 + v2;
}

Note that I have changed the int to void since you aren't returning anything. So your revised program looks like this:

#include <iostream>
#include <windows.h>

using namespace std;

float v1;
float v2;
char oper;
float ans;

void add()
{
    ans = v1 + v2;
}


int main()
{
    cout<< "Value 1 >>";
    cin>> v1;
    cout<< "Value 2 >>";
    cin>> v2;
    cout<< "Operator >>";
    cin>> oper;
    
    if (oper == '+')
    {
             add();
             cout<<ans;
             
            }
    return(0);
 
}

Note that I also changed oper from type int to type char since you are taking a non-digit (+) as input and using oper as a character.

The other three functions have the same problem as your add function. Change the int to a void, change the == to =, and swap what's on the right and left sides of the = sign.

Finally, take the return (0) out from inside the if statement and put it at the end since you want that line to execute no matter what.

awsome! its working, thanks a lot =D now to finish it. Thanks again

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.