Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error upon calling a function
Forum Updated to NodeBB v4.3 + New Features

Error upon calling a function

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 867 Views 2 Watching
  • 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 Offline
    S Offline
    Sylas
    wrote on last edited by
    #1

    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
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      @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
      1
      • mrjjM 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 Offline
        S Offline
        Sylas
        wrote on last edited by
        #3

        @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

        kshegunovK 1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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
          0
          • S Sylas

            @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

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

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

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved