### Basic Calculator Code... doesn't work. Help please?

/* This is my code for a basic calculator. Im learning from a book called "Sam's Teach yourself in an hour a day" and I've had it for three days. Can someone look over my code please.*/
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````#include #include using namespace std; float FirstNumber = 0.0; float SecondNumber = 0.0; string MathSymbol; float MultResult = FirstNumber * SecondNumber; float DivResult = FirstNumber / SecondNumber; float AddResult = FirstNumber + SecondNumber; float SubResult = FirstNumber - SecondNumber; int main() { cout << "Enter first number: "; cin >> FirstNumber; cout << "Enter multiplication symbol: "; cin >> MathSymbol; cout << "Enter second number: "; cin >> SecondNumber; if (MathSymbol = x) { Multiplication(); } if (MathSymbol = +) { Addition(); } if (MathSymbol = -) { Subtraction(); } if (MathSymbol = /) { Division(); } return 0; } void Multiplication() { cout << FirstNumber << " x " << SecondNumber << "= " << MultResult << endl; } void Addition() { cout << FirstNumber << " + " << SecondNumber << "= " << AddResult << endl; } void Subtraction() { cout << FirstNumber << " - " << SecondNumber << "= " <
Last edited on
Hi,

1) You need to include the <iostream> header file which is used for input/output operations.

2) Enclose the single characters in ' ', e.g. '+', '-', etc. Also, you need to decide, are you going to use std::string or char? Pick only one.

3) This

 ``12`` `````` if (MathSymbol = +)``````

is an assignment, not a comparison. In order to compare two values, you need to use ==
 ``12`` `````` if (MathSymbol == +)``````
Last edited on
Thank you so much however I am still receiving errors saying the other functions cannot be identified.
This is my revised code:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include using namespace std; float FirstNumber = 0.0; float SecondNumber = 0.0; char MathSymbol; float MultResult = FirstNumber * SecondNumber; float DivResult = FirstNumber / SecondNumber; float AddResult = FirstNumber + SecondNumber; float SubResult = FirstNumber - SecondNumber; int main() { cout << "Enter first number: "; cin >> FirstNumber; cout << "Enter multiplication symbol: "; cin >> MathSymbol; cout << "Enter second number: "; cin >> SecondNumber; if (MathSymbol == 'x') { MultiplyNumbers(); } if (MathSymbol == '+') { AddNumbers(); } if (MathSymbol == '-') { SubtractNumbers(); } if (MathSymbol == '/') { DivideNumbers(); } return 0; } void MultiplyNumbers() { cout << FirstNumber << " x " << SecondNumber; cout << "= " << MultResult << endl; } void AddNumbers() { cout << FirstNumber << " + " << SecondNumber; cout << "= " << AddResult << endl; } void SubtractNumbers() { cout << FirstNumber << " - " << SecondNumber; cout << "= " <
It's because of missing function prototypes

 ``123456789`` `````` ... float AddResult = FirstNumber + SecondNumber; float SubResult = FirstNumber - SecondNumber; void MultiplyNumbers(); void AddNumbers(); void SubtractNumbers(); void DivideNumbers();``````

Also, compute the values inside every function, otherwise you will always receive 0 as a result.

 ``123456`` ``````void MultiplyNumbers() { cout << FirstNumber << " x " << SecondNumber; float MultResult = FirstNumber * SecondNumber; cout << "= " << MultResult << endl; }``````

and so on for the rest of the functions.
I was wondering if anyone can make a calculator shorter then this:

 ``12345678910111213141516171819`` `````` #include using namespace std; int main () { float v1, v2; char v3; while (1) { cout <<"\n\n\n"; cin >>v1>>v3>>v2; if (v3 == '-') cout << v1 - v2; if (v3 == 'x' || v3 == '*') cout << v1*v2; if (v3 == '+' ) cout <

I know I can make main (); a void type and remove return 0;, but that's not good practice. I can take out using namespace std but would have to use the scope operator, which is more tedious and not common for iostream. I could also take out the loop, but it would not repeat.
I realize the if statements is not economical.
Thank you TheGrayWolf it works now and I can actually understand my mistake.
Although I'm still getting an error saying : one or more multiply defined symbols found.
revised code:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#include using namespace std; float FirstNumber = 0.0; float SecondNumber = 0.0; char MathSymbol; float MultResult = FirstNumber * SecondNumber; float DivResult = FirstNumber / SecondNumber; float AddResult = FirstNumber + SecondNumber; float SubResult = FirstNumber - SecondNumber; void MultiplyNumbers(); void AddNumbers(); void SubtractNumbers(); void DivideNumbers(); int main() { cout << "Enter first number: "; cin >> FirstNumber; cout << "Enter multiplication symbol: "; cin >> MathSymbol; cout << "Enter second number: "; cin >> SecondNumber; if (MathSymbol == 'x') { void MultiplyNumbers(); } if (MathSymbol == '+') { void AddNumbers(); } if (MathSymbol == '-') { void SubtractNumbers(); } if (MathSymbol == '/') { void DivideNumbers(); } return 0; } void MultiplyNumbers() { cout << FirstNumber << " x " << SecondNumber; float MultResult = FirstNumber * SecondNumber; cout << "= " << MultResult << endl; } void AddNumbers() { cout << FirstNumber << " + " << SecondNumber; float AddResult = FirstNumber + SecondNumber; cout << "= " << AddResult << endl; } void SubtractNumbers() { cout << FirstNumber << " - " << SecondNumber; float SubResult = FirstNumber - SecondNumber; cout << "= " << SubResult << endl; } void DivideNumbers() { cout << FirstNumber << " / " << SecondNumber; float DivResult = FirstNumber / SecondNumber; cout << "= " << DivResult << endl; }``````
That's because we've already defined these

 ``12345`` `````` float MultResult = FirstNumber * SecondNumber; float DivResult = FirstNumber / SecondNumber; float AddResult = FirstNumber + SecondNumber; float SubResult = FirstNumber - SecondNumber;``````

in the beginning. Just remove them :)
still receiving the same error
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include using namespace std; float FirstNumber = 0.0; float SecondNumber = 0.0; char MathSymbol; void MultiplyNumbers(); void AddNumbers(); void SubtractNumbers(); void DivideNumbers(); int main() { cout << "Enter first number: "; cin >> FirstNumber; cout << "Enter multiplication symbol: "; cin >> MathSymbol; cout << "Enter second number: "; cin >> SecondNumber; if (MathSymbol == 'x') { void MultiplyNumbers(); } if (MathSymbol == '+') { void AddNumbers(); } if (MathSymbol == '-') { void SubtractNumbers(); } if (MathSymbol == '/') { void DivideNumbers(); } return 0; } void MultiplyNumbers() { cout << FirstNumber << " x " << SecondNumber; float MultResult = FirstNumber * SecondNumber; cout << "= " << MultResult << endl; } void AddNumbers() { cout << FirstNumber << " + " << SecondNumber; float AddResult = FirstNumber + SecondNumber; cout << "= " << AddResult << endl; } void SubtractNumbers() { cout << FirstNumber << " - " << SecondNumber; float SubResult = FirstNumber - SecondNumber; cout << "= " << SubResult << endl; } void DivideNumbers() { cout << FirstNumber << " / " << SecondNumber; float DivResult = FirstNumber / SecondNumber; cout << "= " << DivResult << endl; }``````

And thank you for all your help!
You are wellcome, but oh my, i must be falling asleep.

 ``12345`` `````` if (MathSymbol == 'x') { void MultiplyNumbers(); }``````

Omit the void from the function calls.
Hi Tyler, I slightly tweaked your code, it looks like this now:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` ``````#include using namespace std; float FirstNumber = 0.0; float SecondNumber = 0.0; char MathSymbol; float MultResult = FirstNumber * SecondNumber; float DivResult = FirstNumber / SecondNumber; float AddResult = FirstNumber + SecondNumber; float SubResult = FirstNumber - SecondNumber; void MultiplyNumbers(); void AddNumbers(); void SubtractNumbers(); void DivideNumbers(); int main() { cout << "Enter first number: "; cin >> FirstNumber; cout << "Enter operation symbol: "; cin >> MathSymbol; cout << "Enter second number: "; cin >> SecondNumber; if (MathSymbol == 'x' || MathSymbol == '*') { MultiplyNumbers(); } if (MathSymbol == '+') { AddNumbers(); } if (MathSymbol == '-') { SubtractNumbers(); } if (MathSymbol == '/') { DivideNumbers(); } return 0; } void MultiplyNumbers() { cout << FirstNumber << " x " << SecondNumber; float MultResult = FirstNumber * SecondNumber; cout << "= " << MultResult << endl; } void AddNumbers() { cout << FirstNumber << " + " << SecondNumber; float AddResult = FirstNumber + SecondNumber; cout << "= " << AddResult << endl; } void SubtractNumbers() { cout << FirstNumber << " - " << SecondNumber; float SubResult = FirstNumber - SecondNumber; cout << "= " << SubResult << endl; } void DivideNumbers() { cout << FirstNumber << " / " << SecondNumber; float DivResult = FirstNumber / SecondNumber; cout << "= " << DivResult << endl; } ``````

The differences are that in a cout << statement, I changed "multiplication"
to "operation" signifying to a user that you can do more then multiplication.
I also the if condition that checked to see if it multiplication.
Originally, it checked to see if the character was 'x' no it also checks for '*'
I did this by using || its the OR operator.
the problem i think was giving you an error was your call to the function.

You included the function type ` void DivideNumbers (); `

When your calling a function, you do not need to include its type, only its name, so it would be
` DivideNumbers (); `

It was like that for all of them, so I changed that. hope it works :)

Oh ya, funny thing I saw on YouTube:

It worked! Thank you for all your help.
Topic archived. No new replies allowed.