Roll dice C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Roll dice C++

Hey folks, My code outputs the correct answer (the highest value of two inputs) but adds an extra zero, can someone explain why please? #include <iostream> using namespace std; int max(int num1, int num2) { int result; if(num1 > num2) { cout << num1; } else{ cout << num2; } return result; } int main() { //getting inputs int first; cin >> first; int second; cin >> second; cout << max(first, second); return 0; }

2nd Mar 2021, 11:21 PM
Millü
Millü - avatar
3 Answers
+ 3
you return result but is undefine if(num1>num2){ result = num1 ; } else{ .... } return result ;
2nd Mar 2021, 11:32 PM
Mohammad
Mohammad - avatar
+ 1
What max is doing is printing the max value and returning result whitch it's never changed from its initial value(that you actually didn't initialize, but this is your lucky day) 0, whitch is than printed So now the choice is yours: You either call max without the "cout<<" Or you change the "cout<<num1" (and cout<<num2) in "result = num1" ( and result = num2)
3rd Mar 2021, 2:48 AM
Angelo
Angelo - avatar
- 1
i tried your code and now it says that “‘result’ was not defined in this scope”
2nd Mar 2021, 11:43 PM
Millü
Millü - avatar