Thread: How to make this simpler

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    12

    How to make this simpler

    Code:
    #include <iostream>;
    #include <cstdlib>;
    
    using namespace std;
    
    int menu()
    {
        int choice;
    
        cout << " **** Menu ****" << endl << endl;  //States the options on the menu 
        cout << "(1) Play Game." << endl;
        cout << "(2) Options." << endl;
        cout << "(3) Multiplayer." << endl;
        cout << "(4) Quit." << endl << endl;
    
        cout << ": ";
        cin >> choice;  //enter there choice, 1,2,3 or 4
        return choice;
    } 
    
        int main()
    {
        int choice = menu(), i, second;  
        cout << "You chose option " << choice << endl;  //tells the user their choice
    	cout << "Is this correct? If yes click 1, if no click 2. " << endl;  //asks them if it is correct (1) or not (2) 
    		cout << " (1)"<<endl;
    		cout << " (2)"<<endl;
    		cin>>second;  //they enter their choice 1 or 2
    		if (second==1){ // if they enter 1, it says that they can continue
    			cout<< "Click to continue."<<endl;
    			cin>>i
    				;
    			return 0;
    		}
    		else if (second==2) // if they enter 2, it gives them the menu again
    		{
    			cout << "Try again then."<<endl;
    			{int choice = menu(), i, second;
        cout << "You chose option " << choice << endl;
    	cout << "Is this correct? If yes click 1, if no click 2. " << endl;
    		cout << " (1)"<<endl;
    		cout << " (2)"<<endl;
    			cin>> second;
    			}
    			return 0;
    		}
    		else (second>2);  // if they enter a number bigger than 2, it tells them that it is invalid
    		{
    			cout<<"You have not entered a valid choice."<<endl;
    			cin>>i;
    			return 0;
    		}
    		return second;
    	}
    I am sure that there is a way of making this simpler, but because i'm new i'm not sure how. I want it to loop to the menu everytime the option 2 is entered, and i want it to loop to the question about whether it is the right option if they enter a number bigger than 2. I would also like some help on this.

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    For starters, you can't have semicolons in your #include preprocessor directives. And you need to ditch the ones in your if-else statements. should be

    Code:
    if(condition){
    
    }
    else if(other_condition){
    
    }
    
    else{
    
    }
    You could do something like:

    Code:
    while((choice = menu()) == 2){
    
        choice = menu();
     
        if(choice == 1){
           //statements here
            break;
       }
        else if(choice == 2){
           //statements here
        }
        else{
           //statements here for case not 2 or 1
            break;
        }
    
    }
    
    cin.get();
    Not too sure what your "second" variable is for though. And you don't need that variable 'i' for the system to block. You could use cin.get()
    Last edited by Syscal; 09-19-2010 at 10:58 AM.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    In the program, it doesn't. It says:
    Code:
    #include "backtobasics.h"
    #include "baic.h"
    I just put the iostream bit in and the cstdlib to show what it actaully represents.

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Then it's:

    Code:
    #include <iostream>
    #include <cstdio>
    Also, I've edited the above post so you may want to re-read it.

    EDIT: ohh, well you need to get rid of those semicolons. Doesn't your compiler give you warnings about this?

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    Quote Originally Posted by Syscal View Post
    Doesn't your compiler give you warnings about this?
    Strangely enough no!! Thanks for the code before as well.

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    I would suggest using a do-while loop to call menu again.

    Code:
    #include <iostream>
    using namespace std;
    
    int menu() {
    	int choice;
    	cout << " **** Menu ****" << endl << endl;
    	cout << "(1) Play Game." << endl;
    	cout << "(2) Options." << endl;
    	cout << "(3) Multiplayer." << endl;
    	cout << "(4) Quit." << endl << endl;
    	cout << ": ";
    	cin >> choice;
       	return choice;
    }
    
    int main() {
    	int choice, second;
    	do {
    		choice = menu();
    		cout << "You chose option " << choice << endl;
    		cout << "Is this correct? (Yes 1/ No 2) " << endl;
    		cin >> second;
    	} while (second != 1);
    	return 0;
    }
    You don't need to include cstdlib.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    12
    i know it was on there from a code i wrote before. thanks for the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make a Packet sniffer/filter?
    By shown in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2009, 09:51 PM
  2. "Cannot make pipe"
    By crepincdotcom in forum C Programming
    Replies: 5
    Last Post: 08-16-2004, 12:43 PM
  3. HELP!wanting to make full screen game windowed
    By rented in forum Game Programming
    Replies: 3
    Last Post: 06-11-2004, 04:19 AM
  4. make all rule
    By duffy in forum C Programming
    Replies: 9
    Last Post: 09-11-2003, 01:05 PM
  5. Replies: 6
    Last Post: 04-20-2002, 06:35 PM