### Simple Calculator NOT WORKING!!!

closed account (LN7oGNh0)
Basically, the calculator i programmed does not work.
I did some forward declarations of the functions that I was going to use in the program but when it did not compile, it had error LNK2019??? I think it might have something to do with the forward declarations. Also, I don't want an improvement of the program, just the solution.
Here it is:

#include <iostream>

using namespace std;

//Foward declaration of functions that will be used when calculating
int plus(int x, int y);
int multiplies(int x, int y);
int divides(int x, int y);
int minus(int x, int y);

int main()
{
//Get user first number
int number;
cout << "Please enter a number here.";
cin >> number;

//Get user choice of operation
char operation;
cout << "Choose an operation:\n + - / *";
cin >> operation;

//Get user second coice of number
int number2;
cout << "Please choose your second number.";
cin >> number2;

//Calculate users choices
int x, y;
if (operation = '+')
{
cout << "The answer is: " << plus(x, y);
}

else if (operation = '-')
{
cout << "The answer is: " << minus(x, y);
}

else if (operation = '/')
{
cout << "The answer is: " << divides(x, y);
}
else (operation = '*');
{
cout << "The answer is: " << multiplies(x, y);
}

return 0;
cin.get();
}

Thanks.
this makes it look better, and is easier to the eye

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include using namespace std; //Foward declaration of functions that will be used when calculating int plus(int x, int y); int multiplies(int x, int y); int divides(int x, int y); int minus(int x, int y); int main() { //Get user first number int number; cout << "Please enter a number here."; cin >> number; //Get user choice of operation char operation; cout << "Choose an operation:\n + - / *"; cin >> operation; //Get user second coice of number int number2; cout << "Please choose your second number."; cin >> number2; //Calculate users choices int x, y; if (operation = '+') { cout << "The answer is: " << plus(x, y); } else if (operation = '-') { cout << "The answer is: " << minus(x, y); } else if (operation = '/') { cout << "The answer is: " << divides(x, y); } else (operation = '*'); { cout << "The answer is: " << multiplies(x, y); } return 0; cin.get(); }``````
I stripped down your code to what you should have started with. Try to run this, it doesn't work, why ? Because that is not how you declare a variable int.

When you were writing this did you even try to compile it as you went ????

Start small, write something test it. it's easier to fix later if you know the last thing you wrote worked and this new thing doesn't work...

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include using namespace std; //Foward declaration of functions that will be used when calculating int plus(int x, int y); // incorrect int multiplies(int x, int y); // incorrect int divides(int x, int y); // incorrect int minus(int x, int y); // incorrect int main() { //Get user first number int number; cout << "Please enter a number here."; cin >> number; //Get user choice of operation char operation; cout << "Choose an operation:\n + - / *"; cin >> operation; //Get user second coice of number int number2; cout << "Please choose your second number."; cin >> number2; cout << plus << endl; // remove this cout << x << endl; // remove this cout << y << endl; // remove this cout << number << " " << operation << " " << number2 << endl; return 0; } ``````

*edit* one more suggestion, when you
`int number;`
you should always set it's value, normally we use =0.
I know your about to change it but what if the user does something you don't expect, like types in the letter A instead of a number ? your going to have one hell of a time figuring that out if you don't set it's value to start.

Last edited on
closed account (LN7oGNh0)
Thanks!!!
I am still a noob (1st year) at this stuff so my word on this is probably not going to be the truth. But it looks like your aren't using your functions properly, meaning that really helping here.
http://www.cplusplus.com/doc/tutorial/functions/
^^ tutorial on the sort of thing you are doing ^^
I hope this is what you were looking for.
so focused on what didn't work forgot about solution

try
 ``1234567891011121314151617181920212223242526272829`` `````` #include using namespace std; int main() { //Get user first number int number; cout << "Please enter a number :"; cin >> number; //Get user choice of operation char operation; cout << "Choose an operation:\n + - / *"; cin >> operation; //Get user second coice of number int number2; cout << "Please choose your second number : "; cin >> number2; if (operation = '+') { cout << "The answer is: " << number+number2 << endl; } return 0; } ``````

and so on and so forth, I need coffee, cya later.
I haven't seen anyone answer this correctly yet but this is why your program isn't working, your functions
 ``1234`` ``````int plus(int x, int y); int multiplies(int x, int y); int divides(int x, int y); int minus(int x, int y);``````

these are already functions in C++, so it is throwing you an error, try changing the names of them.

Also you forward declared them but you never implemented them inside of your program
 ``1234`` ``````int adding(int x, int y) { return x + y; }``````
closed account (LN7oGNh0)
I thought I had fixed the problem... but actually, I only fixed a bit. The functions are fixed, but now I have a new error which is saying that I have not initialized a variable. (x and y).
Here is my improved code.

#include <iostream>

using namespace std;

//Foward declaration of functions that will be used when calculating
int plus(int x, int y)
{
return x + y;
}
int multiplies(int x, int y)
{
return x * y;
}
int divides(int x, int y)
{
return x / y;
}
int minus(int x, int y)
{
return x - y;
}

int main()
{
//Get user first number
int number;
cout << "Please enter a number here.";
cin >> number;

//Get user choice of operation
char operation;
cout << "Choose an operation:\n + - / *";
cin >> operation;

//Get user second choice of number
int number2;
cout << "Please choose your second number.";
cin >> number2;

//Calculate users choices
int x, y;
if (operation = '+')
{
cout << "The answer is: " << plus(x, y);
}

else if (operation = '-')
{
cout << "The answer is: " << minus(x, y);
}

else if (operation = '/')
{
cout << "The answer is: " << divides(x, y);
}
else (operation = '*');
{
cout << "The answer is: " << multiplies(x, y);
}

return 0;
cin.get();
}

Sorry if anyone had posted a solution up. I was in the shower than I started programming right when I got out, and forgot about this post.
These are the function prototypes. They tell the compiler what type pf parameters the functions take, and what type it will return.
 ``1234`` ``````int plus(int x, int y); int multiplies(int x, int y); int divides(int x, int y); int minus(int x, int y);``````

However, I don't see anywhere the actual code which defines what the functions actually do. If you just want the program, to compile, you could add skeleton code, after function main(), and fill in the important stuff later.

For example, you could add this:
 ``123456`` ``````int plus(int x, int y) { // Just a temporary outline // details need to be coded here return 0; }``````

... and so on.
As I mentioned above the problem is that plus, minus, and so on are already declared in std so its giving you the ambiguous error, you need to change the names of them.
closed account (LN7oGNh0)
I improved the code before you posted.

I added:
{
return x + y;
}
and changed the operation for each function.

What I need to know is how to initialize variables x and y.
It says I used it before I have initialized them.
closed account (LN7oGNh0)
But, when I had debugged my program, it says that variables x and y are being used without being initialized??? What does that have to do with plus and minus?
Well also you have the user input set to number and number2, but then pass x and y to the function, without them being initialized.

Just pass number and number2 to the functions
 ``1234`` ``````if (operation = '+') { cout << "The answer is: " << plus(number, number2); }``````
closed account (LN7oGNh0)
NEVER MIND!!! I fixed it.
Thank you everyone for helping me!!!
If there's any clash of names in the std namespace, its a good reason not to have `using namespace std;`

Although lots of sample code uses this for simplicity, it shouldn't generally be used.
Better to use the scope resolution where required, such as `std::cout`, or a compromise, just have `using std::cout;` and so on for the specific items you need.
Last edited on
Topic archived. No new replies allowed.