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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "stdafx.h"
#include <iostream>

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);
}

Please help me with this!
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.
closed account (1v5E3TCk)
You can try that codes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
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+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;}
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <iostream>

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"<<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");
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

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"<<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;  }

        else if(choice==2)
                   {cout<<"Subtraction of the numbers is:"<<first-second<<endl;    }

        else if(choice==3)
                  {cout<<"Multiplication of the numbers is:"<<first*second<<endl;     }

        else if(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.
closed account (1v5E3TCk)
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.