I have c++ code and i want to now which code i can use to gå back to menu , i mean if i wrot click any kee to gå back to menu (which is up on my code ), how can i do this
I have c++ code and i want to now which code i can use to gå back to menu , i mean if i wrot click any kee to gå back to menu (which is up on my code ), how can i do this
You really are not providing enough details for us to give any kind of a useful response. It sounds like this is a console program (no windows - just text). If that's the case, you probably want to clear the screen (see the FAQ for details on how to do this), and then reexecute the code you used to display the menu. From here on, I've had to take wild guesses at how your program might be set up.
If your menu is in a loop, just 'continue' to start another iteration of the loop. If your menu is in a separate function, you need to call that function or end the function you're in now. Or you could use a 'goto', but this is considered extremely bad style and if you feel the need to use it, there's a good chance your program as a whole needs redesigning.
Hi
Here is the code and i want from the last text to go back to the menu which is the first block starting with ENIGMA 2005.
Code:#include <iostream> using namespace std; // int main () // { cout << "\ENIGMA 2005 "; // cout << '\n'; int tal1; // int tal2; // int tal3; // cout << "\What are you doing? \n"; // meddelande cout << "(1) scapp a message \n"; // cout << "(2) decrypt message \n"; // Val av material Sten cout << "Choose: \n"; // Val av material Sten int material; cin >> material; if (material == 1) { cout << "Write a message \n"; cout << '\n'; cout << "Choose keycode (2-5): \n"; cin >> material; } if (material == 3) { cout << "Programm has genererat keycode: \n"; cout << "8, 1, 2 \n"; cout << "Write a message you want crypted: \n"; cin >> material; cout << "Your crypt message: \n"; cout << "MOITBPL: \n"; cout << " << Click any key to gå back to menu >> \n"; cin >> material; } return 0; // }
The only way to do it with that code setup is to use a "goto" - but you know what that means - it means you need to redesign your program. I would set up different functions for displaying each menu - like "DisplayMainMenu()", "ClearScreen()". And you probably want to set up a loop that ends when the user selects exit - so that you can keep slecting different options from the main menu, returning, doing aother task, etc...
Can you help me to redesign my program
hey dude there is a lot of mistakes in your codes!
consider revising and see what output u want and what u have actually written
To name some of the mistakes:
1) the variables tal1, tal2,tal3 are never being reference anywhere in your code.
2) bad indentation and there are too many unused // in ur codes
3) what does tis mean:
It shud rather be written like this:Code:cout << " << Click any key to gå back to menu >> \n";
4) your "if-statements" are not complete, that is you are asking the user to enter something, BUT what are u doing with that "something" ?? nothing is being done actually.Code:cout << "Click any key to gå back to menu \n";
And to reply to your question:
just put your whole code in infinite a loop, perhaps like :
But try to put a validation, so that the user can exit the program when he wishes to, perhaps u can use 2menus to do this....Code:int main () { do { cout << "\nENIGMA 2005 "; cout << '\n'; /* all your code body goes in here***************** *********************************************** if (material == 3) { cout << "Programm has genererat keycode: \n"; cout << "8, 1, 2 \n"; cout << "Write a message you want crypted: \n"; cin >> material; cout << "Your crypt message: \n"; cout << "MOITBPL: \n"; cout << "Click any key to gå back to menu\n"; cin.get(); } return 0; ***************************************************/ }while(1); //loop infinetly so that u get back to menu each time }
Hi
I tried but i can go back to the menu , please can you help me what is wrong.
Code:int main () { do { cout << "\nENIGMA 2005 "; cout << '\n'; cout << "\What are you doing? \n"; // meddelande cout << "(1) scapp a message \n"; // cout << "(2) decrypt message \n"; // Val av material Sten cout << "Choose: \n"; // Val av material Sten int material; cin >> material; if (material == 1) { cout << "Write a message \n"; cout << '\n'; cout << "Choose keycode (2-5): \n"; cin >> material; } if (material == 3) { cout << "Programm has genererat keycode: \n"; cout << "8, 1, 2 \n"; cout << "Write a message you want crypted: \n"; cin >> material; cout << "Your crypt message: \n"; cout << "MOITBPL: \n"; cout << "Click any key to gå back to menu\n"; cin.get(); } return 0; }while(1); //loop infinetly so that u get back to menu each time }
It will repeat infinitely with that code if you put the return 0 outside of the loop.
i have added something in it..here is the code...but the prob is that the program will lopp forever.....but just tell what u want ur program to be and i'll try to adjust it! till then here is the code:
Code:#include <iostream> using namespace std; int main () { do { cout << "\nENIGMA 2005 "; cout << '\n'; cout << "\nWhat are you doing? \n"; // meddelande cout << "(1) scapp a message \n"; // cout << "(2) decrypt message \n"; // Val av material Sten cout << "Choose: \n"; // Val av material Sten int material; cin >> material; if (material == 1) { cout << "Write a message \n"; cout << '\n'; cout << "Choose keycode (2-5): \n"; cin >> material; continue; } if (material == 3) { cout << "Programm has genererat keycode: \n"; cout << "8, 1, 2 \n"; cout << "Write a message you want crypted: \n"; cin >> material; cout << "Your crypt message: \n"; cout << "MOITBPL: \n"; cout << "Click any key to gå back to menu\n"; cin.get(); continue; } }while(1);//loop infinetly so that u get back to menu each time return 0; }
Theres the quit ability, and it should be 'else if', not 'if', because you can only choose one.. right? Making them all if statements will result in problems if you add 'else' to any of them. Added an else statement to it also, if the user enters nothing.Code:#include <iostream> using namespace std; int main () { do { cout << "\nENIGMA 2005" << endl; cout << "\nWhat are you doing?" << endl; // meddelande cout << "(1) scapp a message" << endl; // cout << "(2) decrypt message" << endl; // Val av material Sten cout << "To exit type: /quit" << endl; cout << "Choice: \n"; // Val av material Sten string material; cin >> material; if (material == "1") { cout << "Write a message \n"; cout << '\n'; cout << "Choose keycode (2-5): \n"; cin >> material; continue; } else if (material == "3") { cout << "Programm has genererat keycode: \n"; cout << "8, 1, 2 \n"; cout << "Write a message you want crypted: \n"; cin >> material; cout << "Your crypt message: \n"; cout << "MOITBPL: \n"; cout << "Click any key to gå back to menu\n"; cin.get(); continue; } else if(material == "/quit") { break; } else { cout << "Invalid entry." << endl; } } while(1); //loop infinetly so that u get back to menu each time return 0; }
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
NEVER EVER USE GOTO
use a loop
heres an example its a calculator i made
Code:#include<iostream> using namespace std; int main() { int x; int y; int input; do{ cout<<"1. multiply\n"; cout<<"2. devide\n"; cout<<"3. addition\n"; cout<<"4. subtraction\n"; cout<<"please provide the number listed\n"; cin>> input; switch ( input ) { case 1: cout<<"please provide 2 numbers to be multiplied\n"; cin>> x >> y ; cin.ignore(); cout<<( x*y ); cin.get(); break; case 2: cout<<"please provide 2 numbers to be devided\n"; cin>> x >> y ; cin.ignore(); cout<<( x/y ); cin.get(); break; case 3: cout<<"pls provide 2 numbers to be added\n"; cin>> x >> y ; cin.ignore(); cout<<( x+y ); cin.get(); break; case 4: cout<<"pls provide 2 numbers to be subtracted\n"; cin>> x >> y ; cin.ignore(); cout<<( x-y ); cin.get(); break; } }while(input!=4); }
and you have good english for someone forein another forein kid at my school has been here for 3 years and he still can barely put together the most basic sentence
Last edited by lilhawk2892; 09-26-2005 at 06:04 AM.
hahahahahahahahahahahahhhhhhhhhhhhhhhhhhhhhhhhhhhh hhhhhhhhhhhhhhhhhhhhhhhhhh i need to learn more and sydney won the grand finale.
Settle down, read the article on goto. Its obviously not appropriate for this situation, but it does have 1/10000 cases where it is appropriate, and no one said it was either.Originally Posted by lilhawk2892
Its foreign, by the way.
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101
I want to add to the end of my code this block:
choose siffra nr 1:
choose siffra nr 2:
choose siffra nr 3:
and it must give the users possibility to write up the nummbers they choose. Thanks alot
Code:include <iostream> using namespace std; int main () { do { cout << "\nENIGMA 2005" << endl; cout << "\nWhat are you doing?" << endl; // meddelande cout << "(1) scapp a message" << endl; // cout << "(2) decrypt message" << endl; // Val av material Sten cout << "To exit type: /quit" << endl; cout << "Choice: \n"; // Val av material Sten string material; cin >> material; if (material == "1") { cout << "Write a message \n"; cout << '\n'; cout << "Choose keycode (2-5): \n"; cin >> material; continue; } else if (material == "3") { cout << "Programm has genererat keycode: \n"; cout << "8, 1, 2 \n"; cout << "Write a message you want crypted: \n"; cin >> material; cout << "Your crypt message: \n"; cout << "MOITBPL: \n"; cout << "Click any key to gå back to menu\n"; cin.get(); continue; } else if(material == "/quit") { break; } else { cout << "Invalid entry." << endl; } } while(1); //loop infinetly so that u get back to menu each time