Thread: loop

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    8

    loop

    hello . I have a question on 2d arrays and loop Is there anyone to help ?? I wish u can help me progress my prog . thx

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your question?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2017
    Posts
    8
    Code:
    ostream& DisplayMenu()
    {
        return (cout <<"Please choose from the following options :\n\n"
                     <<"1- A\n"
                     <<"2- B.\n"
                     <<"3- c.\n\n"
                     <<"or :\n"
                     <<"------------------------\n\n"
              <<"4-l.\n"
                     <<"5-y.\n"
                     <<"6-k.\n"
             <<"7-v.\n"
             <<"8-n.\n\n"
                     <<"Or :\n"
              <<"----\n\n"
              <<"9-I changed my mind and would like to exit.\n\n");
        
            
     }
    
    
    int ChooseFromMenu()
    {
        int A = 0;
       while ((DisplayMenu() && !(cin >> A)) || (cin.good() && (A < 1 || A > 9))) {
           cout << "\nInvalid input!\n"<<endl;
            cin.clear();
            cin.ignore(1000, '\n');
           
    }
    return A;
    }

    First , i would like to make this menu work with void instead of ostream& how cn we change it??. thx
    Last edited by david16; 05-16-2017 at 02:04 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by david16
    First , i would like to make this menu work with void instead of ostream& how cn we change it?
    Perhaps you should spend some time to learn C++ instead of trying to tweak code that you found somewhere but don't understand.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    8
    This is my code dear . I wrote the menu and then someone helped me to call it each time input is wrong . But I didn't get it so I prefer to use void but now i'm stuck on how to change it cn u help ?? thx
    Last edited by david16; 05-16-2017 at 02:41 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by david16
    I wrote the menu and then someone helped me to call it each time input is wrong . But I didn't get it so I prefer to use void but now i'm stuck on how to change it cn u help ?
    Why not seek to understand the code instead?
    • DisplayMenu must return a bool or something convertible to bool because it is called as a subexpression in the boolean expression (DisplayMenu() && !(cin >> A)).
    • ostream is convertible to bool, hence a reference to ostream is convertible bool, thus ostream& is a suitable return type for DisplayMenu
    • cout is an ostream object
    • The return type of the various overloads of operator<< for ostream is typically ostream&, hence allowing these calls to be chained. But this also means that you can return the result of the big expression in DisplayMenu, since the type of the expression matches the return type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2017
    Posts
    8
    I see . But if you show me how to write it with void , that will be much easier . I want to try both and see which 1 is suitable for my task . thx

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In that case you should just write it with void return type:
    Code:
    void DisplayMenu()
    {
        cout << "Please choose from the following options :\n\n"
             << "1- A\n"
             << "2- B.\n"
             << "3- c.\n\n"
             << "or :\n"
             << "------------------------\n\n"
             << "4-l.\n"
             << "5-y.\n"
             << "6-k.\n"
             << "7-v.\n"
             << "8-n.\n\n"
             << "Or :\n"
             << "----\n\n"
             << "9-I changed my mind and would like to exit.\n\n";
    }
    As you can see, I changed the return statement to a statement without the return since it is no longer appropriate.

    Next, you need to figure out how to modify this:
    Code:
    while ((DisplayMenu() && !(cin >> A)) || (cin.good() && (A < 1 || A > 9))) {
        cout << "\nInvalid input!\n"<<endl;
        cin.clear();
        cin.ignore(1000, '\n');
    }
    such that you can still display the menu at the right time, and yet do without DisplayMenu() being in the boolean expression. There are quite a few ways of doing this, so think about it and try out your ideas.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2017
    Posts
    8
    salem what r u posting Its a from another forum

  12. #12
    Registered User
    Join Date
    May 2017
    Posts
    8
    thx for ur answer I will let u know if I got any issue

  13. #13
    Registered User
    Join Date
    May 2017
    Posts
    8
    actually it says undeclared A if I declared it before while I'm getting an error on displayMenu because its void were should I declare it ??

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by david16
    salem what r u posting Its a from another forum
    For each question that you have, you should pick one relevant channel of help and stick to it until you have your answer, or you find that the channel simply isn't providing the help you need (typically after quite some time trying, and then you should link the new help channel to the old one). You should not pose the same question to multiple channels of help at the same time.

    Quote Originally Posted by david16
    actually it says undeclared A should if I declared it before while I'm getting an error on dislpayMenu because its void were should I declare it ??
    When telling people about an error message, post the exact error message, along with the relevant code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > salem what r u posting Its a from another forum
    The link was meant for laserlight, not you.

    To show that you're asking the same questions, getting the same answers, and thus being a complete and utter waste of time. You're neither listening or learning anything by forum hopping, and you're wasting the time of people who would otherwise have something else to do.


    This one however, is for you.
    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-22-2016, 09:08 AM
  2. Replies: 1
    Last Post: 03-28-2015, 08:59 PM
  3. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM

Tags for this Thread