Question:
C++ Errors PLEASE HELP?
anonymous
2011-05-19 06:52:38 UTC
Can you please look over the code that I have written? I am just starting C++ programming and I am having a few issues.

#include
using namespace std;
int addition (int a,int b)
{
int c;
c = a + b;
return c;
}

int subtraction (int d,int e)
{
int f;
f = d - e;
return f;
}

int multiplication (int g,int h)
{
int i;
i = g * h;
return i;
}

int division (int j,int k)
{
int l;
l = j / k;
return l;
}

int main()
{
int m;

cout << "Welcome to Calculator!";
cout << "Would you like to use addition, subtraction, multiplication, or division?";
cin >> m;

if (m=="addition")
{
int n;
int o;
cout << "Multiply this number: ";
cin >> n;
cout << "By this number: ";
cin >> o;


cout << "The answer is: ";
cout << n * o;
}
}

It gives me this error:

error: ISO C++ forbids comparison between pointer and integer.

Does this mean that I cannot refer to the input that I recieved with an "if" statement?

I do realize that I can use lots of other methods, but I prefer to do "if" statements at the moment due to the fact that I just started.
Five answers:
?
2011-05-19 07:06:19 UTC
m is an object of type int (integer, whole number). It cannot store a string or be compared to a string.



change



     int m;



to



     std::string m;



and this will work, see sample run here: https://ideone.com/oRJfo
cja
2011-05-19 07:14:46 UTC
It means that this statement doesn't make sense:



    if (m=="addition")



You've declared m to be an int, and you're trying to compare that with "addition", which is a pointer to char. The prompt you give doesn't indicate to the user what should be entered. Maybe it means to enter the entire word: addition, multiplication, etc., which could work, but would be extremely inconvenient for the user. A better prompt would be something like this:



Select operation (a = add, s = subtract, m = multiply, d = divide):



To get the response, you could declare:



string resp;



and then do this after issuing the prompt:



getline(cin, resp);



It's best to have defined constants for your expected responses:



const char ADD('a');

etc.



Then to determine what the user wants to do:



    if (resp[0] == ADD) {

        // do addition

    } else if (resp[0] == SUB) {

       // do subtraction

    } . . . etc.
oops
2011-05-19 07:25:55 UTC
On a side note, you don't need to give every variable a different name, as long as they reside in separate scopes(e.g. separate functions). In other words:



int addition (int a,int b)

{

    int c;

    c = a + b;

    return c;

}



int subtraction (int a,int b)

{

    int c;

    c = a - b;

    return c;

}



Even though the above functions have the exact same variable names, there is no problem, because a function defines it's own scope.



Read about scope here:

http://msdn.microsoft.com/en-us/library/b7kfh662%28v=VS.100%29.aspx
roger
2011-05-19 07:50:13 UTC
in addition to what oops said



you do not need any extra variables in your functions



int addition (int a,int b)

{

return a + b;

}



int subtraction (int a,int b)

{

return a - b;

}
anonymous
2016-04-30 07:32:06 UTC
I am not sure but it seems you have problem with IE8 source codes and MS handler files. try installing Microsoft Visual C++ re distributable 2005 and you haven't mentioned your service pack?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...