[Solved] Why is my C++ code returning some weird numbers even though everything is correct? [closed]


There are some problems in your code:

#include <iostream>
using namespace std;

int guessIt(int num) { //when you make the function guessIt(int num) you say it returns an int
    if(num == 27) {
        cout << "You Won";
    }
    else {
        cout << "You Lost";
    }
} //code finished without returning an int

int main() {
    cout << guessIt(27); //What happens here? guessIt didn't return anything, so cout prints something it got that was in the memory where guessIt was supposed to put it's return value
}

What you have to do is either add returns, like return 0; if you win and return 1; if you lost instead of cout<<"You won"; or cout<<"You Lost"; Then in main() add

if (guessIt(27)==0) {
//TODO: Win message here
}
else {
//TODO: Lose message here
}

You can also change guessIt’s return value to void, and remove the cout<<guessIt(27) and just have guessIt(27). (void means it will return nothing.)

solved Why is my C++ code returning some weird numbers even though everything is correct? [closed]