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; }
3 Answers
+ 3
you return result but is undefine
if(num1>num2){
result = num1 ;
}
else{
....
}
return result ;
+ 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)
- 1
i tried your code and now it says that ââresultâ was not defined in this scopeâ