### Calculator help

Hi, im a beginner in c++ and i've been trying to do a simple calculator..
but when i try to compile i get an error saying:
error C4700: uninitialized local variable 'Result' used
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include "stdafx.h" #include using namespace std; int GetUserInput() { cout << "Please enter an integer: "; int Value; cin >> Value; cout << endl; return Value; } char GetMathematicalOperation() { cout << "Enter an operator(*, +, - or /): "; char nOperator; cin >> nOperator; cout << endl; return nOperator; } int CalculateResult(int intX, char nOperator, int intY) { if(nOperator == '*') intX * intY; if(nOperator == '+') intX + intY; if(nOperator == '-') intX - intY; if(nOperator == '/') intX / intY; return 0; } void PrintResult(int Result) { cout << "Your answer: " << Result << endl; } int main() { int input1 = GetUserInput(); char nOperator = GetMathematicalOperation(); int input2 = GetUserInput(); int Result = CalculateResult(input1, nOperator, input2); PrintResult(Result); }``````

Last edited on
It's because you're passing Result into the function CalculateResult, without having first initialised it.

CalculateResult looks weird to me. You're passing in Result, and then returning its value, without using it or changing it in any way. Is that really what you intended that function to do?
Oh lol, im an idiot :D

Thanks for the help!
You're welcome. We all make silly mistakes from time to time! I know I do...
Everything compiles.. but the answer is ALWAYS 0 for some reason. It's probably some stupid thing that i cant see right now but please help :D
You initialize Result with the value returned from CalculateResult(), and because you have that function return 0 this is the result you will always obtain. Try:
 ``1234567891011121314`` ``````int CalculateResult(int intX, char nOperator, int intY) { if(nOperator == '*') return intX * intY; else if(nOperator == '+') return intX + intY; else if(nOperator == '-') return intX - intY; else if(nOperator == '/') return intX / intY; else return 0; }``````

It would probably be easier to use a switch though.
You can try that codes:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778`` ``````#include using namespace std; int menu() { int a; while(1) { cout<<"**********************\n"; cout<<"**Choose your operation**\n"; cout<<"**********************\n"; cout<<"1.Addition\n2.Subtraction\n3.Multiplaction\n4.Division\n5.Exit\n"; cin>>a; if(a==1||a==2||a==3||a==4||a==5){break;} cout<<"Bad ınput.\n";} return a; } void addition(int x, int y) { cout<<"Input the first number.\n"; cin>>x; cout<<"Input the second number.\n"; cin>>y; cout<<"\n**Addition of numbers:"<>x; cout<<"Input the second number.\n"; cin>>y; cout<<"\n**Subtraction of numbers:"<>x; cout<<"Input the second number.\n"; cin>>y; cout<<"\n**Multiplaction of the numbers:"<>x; cout<<"Input the second number.\n"; cin>>y; cout<<"\n**Division of the numbers:"<

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` `````` #include using namespace std; int main() { int first, second, choice; // integers for stroring the numbers and selected operation cout<<"Input the first number.\n"; cin>>first; cout<<"Input the second number.\n"; cin>>second; cout<<"--Choose your operation--\n"<>choice; switch(choice) case 1: cout<<"Addition of the numbers is:"<

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include using namespace std; int main() { int first, second, choice; // integers for stroring the numbers and selected operation cout<<"Input the first number.\n"; cin>>first; cout<<"Input the second number.\n"; cin>>second; cout<<"--Choose your operation--\n"<>choice; if(choice==1) {cout<<"Addition of the numbers is:"<
@senhor, is posting that large code block truly necessary? Posting your own solution to a coding question doesn't really help someone in this situation. He wasn't asking for someone else's solution, he was asking about solving an error and then wanted explanation for his code's behavior that he didn't understand.

@OP: Definitely check out R10111001's post. You get zero every time because that's what your function returns! Look at his function for specifics, it's got what you're looking for.
It is not necessary but maybe it can be helpfull to see alternative ways.
But i am sorry
Damn.. i always succeed to make the most stupid things ever. I cant belive that i missed to return something!

Thanks for all your help guys!
Topic archived. No new replies allowed.