Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Error upon calling a function

    General and Desktop
    3
    5
    580
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      Sylas last edited by

      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

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        @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 ) ...

        S 1 Reply Last reply Reply Quote 1
        • S
          Sylas @mrjj last edited by

          @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

          kshegunov 1 Reply Last reply Reply Quote 1
          • mrjj
            mrjj Lifetime Qt Champion last edited by

            @Sylas said:

            Stroustrup

            oh yes. that book is more a reference than "learn c++."
            But later you will be happy for it :)

            1 Reply Last reply Reply Quote 0
            • kshegunov
              kshegunov Moderators @Sylas last edited by

              @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.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply Reply Quote 0
              • First post
                Last post