Menu in C++

This is a discussion on Menu in C++ within the C++ Programming forums, part of the General Programming Boards category; Hi I'm making a menu in C++ for a sudoku puzzle solver program I've already wrote using switch setence. I've ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    52

    Menu in C++

    Hi I'm making a menu in C++ for a sudoku puzzle solver program I've already wrote using switch setence.
    I've wrote that code:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<iostream.h>
    
    int opcio1();
    int opcio2();
    int opcio3();
    
    
    int main(){
    
        char sel;
    
     
    
        cout <<"Sudoku\n\n\n"<<endl;
        cout <<"1. How to\n\n"<<endl;
        cout <<"2. Sudoku generator\n\n"<<endl;
        cout <<"3. Play!\n\n"<<endl;
        cout <<"4. Exit\n\n"<<endl;
       
        cout <<"Select an option, please..."<<endl;
    
     
    
        do{
    
            sel=getch();
    
        }while((sel<'1' || sel>'4')&&sel!=27); //esc=ASCII 27 
    
     
    
        switch(sel){
    
            case '1':
    
                opcio1();
    
                break;
    
     
    
            case '2':
    
                opcio2();
    
                break;
    
     
    
            case '3':
    
                opcio3();
    
                break;
    
     
    
    
            case 27:
    
                return;
    
                    
        }
    
    }
    
     
    
     
    
    int opcio1(){
    
        cout <<"How to\n\n"<<endl;
    
    }
    
     
    
    int opcio2(){
    
       cout <<"Sudoku generator\n\n"<<endl; 
    
    }
    
     
    
    int opcio3(){
    
        cout <<"Play\n\n"<<endl;
    
    }
    This code works.
    My question is how to do a return to the menu option inside every option in the menu?
    I've tried to use goto but it only works for inside the same function.
    Thank you

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    6
    Move the select statement inside the do while loop

    Code:
    do
    {
        sel=getch();
        switch(sel)
        {
            case '1': opcio1() ; break ;
            case '2': opcio2(); break ;
            case '3': opcio3(); break;
            default: break ;
         }
    }while(sel != 27) ;
    Hope this helps

  3. #3
    Registered User
    Join Date
    Dec 2007
    Location
    North Georgia Mountains
    Posts
    11
    ahh Grasshopper, you have not yet learned of loops and return values. To make your menu always reappear (by magic of course) place it inside of a for(; (this is supposed to be another semicolon and a right paren not a smiley face) loop, include your menu and the do while statement - add an appropriate break after the while line.
    Next visit each of your opcio functions and either give them an int return value or declare them as void, someone on this board has a signature that refers to compiler warnings and errors - pay attention to them, they are not there for decoration.

    tom

  4. #4
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<iostream.h>
    What's with all of these headers? At least change it to <iostream>. I can't believe this compiles, because you don't put std::cout or std::endl, and you don't use "namespace std" or anything like that.

    >>sel=getch();
    I think it's Elysia's sig that says not to use this (I don't know why myself). But why not just use "std::cin >> sel;"?

    I don't see why you don't just use ints for this, although I guess you're wanting to see if they pressed escape.

    case 27:

    return;
    If you have "int main()" (like you should), you can't just put "return". It must be "return 0" or something like that.

    Anyway, now that I'm complaining about your code , to your original question. Since your functions (opcio1, opcio2, etc.) are not void, they're supposed to return a value.
    Since they return a value you could do something like this:
    Code:
    int opcio1(){
    
        cout <<"How to\n\n"<<endl;
        return 0; //Or whatever value you want to return at the end
    }
    Code:
            case '1':
    
                int ret = opcio1(); //If you want to know the return value, otherwise you don't need this
                //Now the function has returned, and you can keep doing stuff under "case '1'" until you want to break
                
                break;
    Hope this helps.

    Ahhh, too slow.

  5. #5
    Registered User bobknows's Avatar
    Join Date
    Jan 2011
    Location
    SD
    Posts
    87
    just make the menu a function, then set a value for each choice, cin the choice, implement the menu option based on the choice with a switch, then return

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,231
    O_o

    This marks the forth useless old bump I'm aware of; are you doing this on purpose?

    Soma

  7. #7
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    probably he just clicked on the tag with anything regarding sudoku without looking at the date.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21