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
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?
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:
#include <iostream>
usingnamespace 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+y<<endl;
}
void subtraction(int x, int y)
{
cout<<"Input the first number.\n";
cin>>x;
cout<<"Input the second number.\n";
cin>>y;
cout<<"\n**Subtraction of numbers:"<<x-y<<endl;
}
void multiplaction(int x, int y)
{
cout<<"Input the first number.\n";
cin>>x;
cout<<"Input the second number.\n";
cin>>y;
cout<<"\n**Multiplaction of the numbers:"<<x*y<<endl;
}
void division(double x, double y){
cout<<"Input the first number.\n";
cin>>x;
cout<<"Input the second number.\n";
cin>>y;
cout<<"\n**Division of the numbers:"<<x/y<<endl;
}
int main()
{
int x,y,b;
b=menu();
if(1==b){addition(x,y);}
if(2==b){subtraction(x,y);}
if(3==b){multiplaction(x,y);}
if(4==b){division(x,y);}
if(5==b) {return 0;}
}
#include <iostream>
usingnamespace 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"<<endl;
cout<<"1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n";
cin>>choice;
switch(choice)
case 1:
cout<<"Addition of the numbers is:"<<first+second<<endl;
break;
case 2:
cout<<"Subtraction of the numbers is:"<<first-second<<endl;
break;
case 3:
cout<<"Multiplication of the numbers is:"<<first*second<<endl;
break;
case 4:
cout<<"Division of the numbers is:"<<first/second<<endl;
break;
system("pause");
}
#include <iostream>
usingnamespace 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"<<endl;
cout<<"1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n";
cin>>choice;
if(choice==1)
{cout<<"Addition of the numbers is:"<<first+second<<endl; }
elseif(choice==2)
{cout<<"Subtraction of the numbers is:"<<first-second<<endl; }
elseif(choice==3)
{cout<<"Multiplication of the numbers is:"<<first*second<<endl; }
elseif(choice==4)
{cout<<"Division of the numbers is:"<<first/second<<endl; }
system("pause");
}
@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.