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
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
What is your question?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
A lot of back story to catch up on
Creating A File Of Names And Id - C And C++ | Dream.In.Code
Break Statement - C And C++ | Dream.In.Code
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.
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.
Perhaps you should spend some time to learn C++ instead of trying to tweak code that you found somewhere but don't understand.Originally Posted by david16
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Why not seek to understand the code instead?Originally Posted by david16
- 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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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.
In that case you should just write it with void return type:
As you can see, I changed the return statement to a statement without the return since it is no longer appropriate.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"; }
Next, you need to figure out how to modify this:
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.Code:while ((DisplayMenu() && !(cin >> A)) || (cin.good() && (A < 1 || A > 9))) { cout << "\nInvalid input!\n"<<endl; cin.clear(); cin.ignore(1000, '\n'); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
salem what r u posting Its a from another forum
thx for ur answer I will let u know if I got any issue
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 ??
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.Originally Posted by david16
When telling people about an error message, post the exact error message, along with the relevant code.Originally Posted by david16
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
> 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.