Thread: Problems with a menu

  1. #1
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106

    Exclamation Problems with a menu

    I am new to C++ and would like to make a menu in which the user will input a number and then depending on the number they type a second program will open.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Post the code you have and state the problems you are having achieving your task.

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    The best way to achieve this would to use a case select statement, or an if statement, but case select is much neater Assign a variable of int data type (Ex. int selection) and in the case select put your code that assigns to what number. Hope this helps!

  4. #4
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Code:
    #include <iostream>
    #include <stdlib.h> 
    #include <stdio.h> 
    #include <string.h> 
    
    int v;
    int w;
    int x;
    int y;
    int z;
    
    int main()
    {
        cout<<"1. Addition";
        cout<<"2. Subtraction";
        cout<<"3. Multiplication";
        cout<<"4. Division";
        cout<<"5. Exit";
        cin>> v >> w >> x >> y >> z;
        cin.ignore();
        
        switch (menu)
        
        case 1:
             char a[];
             char b[];
             if (v ==1) char a[] = "Add.exe";
             char b [BFRSIZ];
             system ("Add.exe");
             system (a);
             strcpy (b, "Add.exe");
             strcat (b, " -aparm -b");
             printf ("Executing %s\n", b);
             system (b);
             return 0;
             break;
        case 2:
                 if (v==2)
                 return 0;
                 break;
        case 3:
                 if (v==3)
                 return 0;
                 break;
        case 4:
                 if (v==4)
                 return 0;
                 break;
        case 5:
                 if (v==5)
                 return 0;
                 break;
    }
    DONT LAUGH AT ME I SAID I WAS NEW TO THIS (thats if its drastically wrong)!

  5. #5
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    You need to declare a variable of int. Such as, int selection, and for your cin statement, just put cin >> variablename. Variable name should be replaced with the name you chose of the int data type. In the case statements, call your functions. I think you might need to use file i/o to open other programs. Why would you want to do that anyway? A function will do just fine, for whatever you want to achieve, unless opening a program is your goal.

    EDIT: Your headers should be changed to string, cstdlib, and cstdio

  6. #6
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    THANX 4 your help i'll do that now!

  7. #7
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    I need help again (I dont believe this). Could you please have a look at my code and tell me whats the matter with it as i cant compile:

    Code:
    #include <iostream>
    #include <cstdio> 
    #include <cstdlib> 
    #include <string> 
    
    using namespace std;
    
    int selection;
    int mult (int x,int y);
    int divi (int x,int y);
    int add (int x,int y);
    int subt (int x,int y);
    
    int main()
    {
        cout<<"1. Addition";
        cout<<"2. Subtraction";
        cout<<"3. Multiplication";
        cout<<"4. Division";
        cout<<"5. Exit";
        cin>> selection;
        cin.ignore();
        
        switch (selection)
        
    case 1:
             int mult (int x,int y);
             
                 int x;
                 int y;
                 
                 cout<<"Please input two numbers to be multiplied: ";
                 cin>> x >> y;
                 cin.ignore();
                 cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
                 cin.get();
                 }
    
                 int mult ( int x, int y )
                 {
                        return x * y;
                 }
                 break;       
    case 2:
         int divi (int x,int y);
         
             int x;
             int y;
             
             cout<<"Please input two numbers to be divided:";
             cin>> x >> y;
             cin.ignore();
             cout<<"The product of your two numbers is "<< divi (x,y) <<".\n";
             cin.get();
             }
             int divi (int x,int y)
             {
                 return x/y;
             }
             break;
    }

  8. #8
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Forgot to mention it gets as far as
    Code:
     int mult ( int x, int y )
                 {
                        return x * y;
                 }
    it gets to here and i get an error------>BREAK;

  9. #9
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    That is because you declare two functions of the same name. Also, you forgot a "{" after switch. You also have alot of other errors. Sometimes it's better just to start over, then going through the code rooting out errors.

  10. #10
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Thanx for the advise i might try that. But first i'll try solve it!

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Why have you used global variables?

  12. #12
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    could you show me where i have to add the { after switch please?

  13. #13
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I think it is liek this:

    Code:
    switch (selection)
    {    
    case 1:
             int mult (int x,int y);
             
                 int x;
                 int y;
                 
                 cout<<"Please input two numbers to be multiplied: ";
                 cin>> x >> y;
                 cin.ignore();
                 cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
                 cin.get();
                 }
    
                 int mult ( int x, int y )
                 {
                        return x * y;
                 }
                 break;       
    case 2:
         int divi (int x,int y);
         
             int x;
             int y;
             
             cout<<"Please input two numbers to be divided:";
             cin>> x >> y;
             cin.ignore();
             cout<<"The product of your two numbers is "<< divi (x,y) <<".\n";
             cin.get();
             }
             int divi (int x,int y)
             {
                 return x/y;
             }
             break;
    }
    Edit: Would you not also be best defining your functions outside of main to make your program clearer.

  14. #14
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Yes, it would be better to declare them from outside of main. Or you could just not do anything in main. But remember it returns 0; even if there is nothing in it. All you would have to do is make a menu() function, and call that in main. Or you could just do int main(void). There are many possibilities in programming, so don't get scared of making a mistake, or not doing something right. If it runs and does what you want it to do then you are fine, although it may not be the best technique, it doesnt really matter.

  15. #15
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Well it probably would if he wants to get into big time programming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ncurses menu question
    By mak2k in forum C Programming
    Replies: 2
    Last Post: 11-07-2006, 03:04 PM
  2. Creating a menu that reads input via an array?
    By Nalif in forum C Programming
    Replies: 6
    Last Post: 09-29-2006, 09:21 PM
  3. menu - options Problems
    By wakish in forum C Programming
    Replies: 3
    Last Post: 07-05-2006, 03:23 AM
  4. Problems with my menu
    By Olidivera in forum Windows Programming
    Replies: 2
    Last Post: 07-07-2005, 12:44 PM
  5. Menu stuff
    By Shadow in forum C Programming
    Replies: 10
    Last Post: 04-28-2002, 09:05 AM