help with undefined

for some reason my printResult(result) at the end is undefined i need some advice as to what i did wrong

code:

#include <iostream>


int getNumber()
{
int number1;
std::cin >> number1;
std::cout << "choose your first number" << std::endl;
return number1;
}

int operation()
{
int operation;
std::cin >> operation;
std::cout << "pick your operator 1 = + 2 = - 3 = * 4 = /" << std::endl;
return operation;
}

int calculation(int x, int y, int operation)
{
if (operation == 1)
return x + y;
if (operation == 2)
return x - y;
if (operation == 3)
return x * y;
if (operation == 4)
return x / y;
return -1;
}

int printResult(int result)
{
std::cout << "your answer is " << result << std::endl;
}

int main()
{
int input1 = getNumber();
int op = operation();
int input2 = getNumber();
int calc = calculation(input1, op, input2);

printResult(result);
}
closed account (E0p9LyTq)
Where is the result variable being declared and initialized in main()?

PLEASE use code tags, it makes reading your code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

Recommend changing the return type of printResult to void. You are not returning a value from the function.

If I understand your code correctly I rewrote it as follows:

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
#include <iostream>

int getNumber()
{
   int number1;
   std::cout << "choose a number ";
   std::cin >> number1;
   return number1;
}

int operation()
{
   int operation;
   std::cout << "pick your operator \n1 = + \n2 = - \n3 = * \n4 = /" << std::endl;
   std::cin >> operation;
   return operation;
}

int calculation(int x, int y, int operation)
{
   if (operation == 1)
   {
      return x + y;
   }
   else if (operation == 2)
   {
      return x - y;
   }
   else if (operation == 3)
   {
      return x * y;
   }
   else if (operation == 4)
   {
      return x / y;
   }

   return -1;
}

void printResult(int result)
{
   std::cout << "\nyour answer is " << result << std::endl;
}

int main()
{
   int input1 = getNumber();
   int op = operation();
   int input2 = getNumber();
   int calc = calculation(input1, input2, op);

   printResult(calc);
}

choose a number 12
pick your operator
1 = +
2 = -
3 = *
4 = /
1
choose a number 5

your answer is 17
Last edited on
This function must return a value, because the function is a int.
1
2
3
4
int printResult(int result)
{
std::cout << "your answer is " << result << std::endl;
}



1
2
3
4
5
6
7
8
9
int main()
{
int input1 = getNumber();
int op = operation();
int input2 = getNumber();
int calc = calculation(input1, op, input2);

printResult(result); // The variable result is undeclared on main function.
}


Since you want to print the result of the calculation, you can call the function print result with the value calc. For instance,

printResult(calc);

Furthermore, make sure you return 0 at the end of the main function. I hope it helps
Last edited on
closed account (E0p9LyTq)
chicofeo wrote:
Furthermore, make sure you return 0 at the end of the main function.

http://en.cppreference.com/w/cpp/language/main_function

4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;.

5) Execution of the return (or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration) and then calling std::exit with the same argument as the argument of the return. (std::exit then destroys static objects and terminates the program)
Thanks for bringing it up. I will keep using return 0, but I wont tell anyone else to do so.
closed account (E0p9LyTq)
@chicofeo,

I agree with you, I will keep using return 0; as well myself. What if the C++ ISO Committee decides that return 0; be mandatory in some future standard?

Oh, all the borked code! ;)
Topic archived. No new replies allowed.