Solved Error upon calling a function
-
I am a beginner in C++. The function I call is:
bool oui_ou_non()
{
cout << "Oui ou non ? o/n ? ";
char rep=0;
cin >> rep;
switch (rep){
case 'o':
return true;
case 'n':
return false;
default:
cout << "Je prends celà pour un non.\n";
return false;
}
}
I call that with:
int main()
{cout << "Voulez-vous lancer le programme ?\n";
oui_ou_non();//let us call the sub-progra
cout << oui_ou_non <<endl;
cout << "nous sommes revenus au programme principal\n";
if (oui_ou_non == false)//apparement ok,mais d'aprés warningligne précédente:"bool oui_ou_non wiil always evaluate as'true'
{
cout << "retourné false . Le programme va s'arrêter.\n";
return 0;
}
cout << "Programme principal\n";
}
When i return in int main, the value of oui_ou_non, is lost. It becomes always 1. Ithink that this is due because I use a bad way for calling the bool function. I spent two weeks in many trials without any success. Is there somebody to help me. Thanks -
@Sylas said:
hi you seem not to store the return from the function ?
in main
bool return_from_function = oui_ou_non();
if ( return_from_function == false ) ... -
@mrjj Wonderfulll. Now it works very well. I ask myself : where should I find the solution. Indeed the Stroustrup pdf is very hard for beginners. Thanks again
-
@Sylas said:
Stroustrup
oh yes. that book is more a reference than "learn c++."
But later you will be happy for it :) -
@Sylas
A function named "yes or no" is not very descriptive. I recon you're using this for testing purposes, but still I do advise to name your functions in a manner that speaks to you, even if their names are getting long-ish. That said, bool values can be checked without actually invoking the equality operator and I do think it's preferable this way, meaning your code will look something like:if (oui_ou_non()) { // The function returned true }
Kind regards.