return to "menu"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xiaolim
    New Member
    • Jul 2008
    • 18

    return to "menu"

    hi

    as shown in the source, i wanted to return to 'choice' in the 'switch' statement so that user can input their choices again after they chose the option.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <conio.h>
    #define MAX_STUDENT 100
    using namespace std;
    
    struct stud
    {
    	char id[8];
    	char name[20];
        char address[30];
    	int  telephone;
    };
    
    int main()
    
    {
    
        //diaplay welcome message
        cout << "Welcome to student information panel"<< endl;  
        //ask to login
        cout << "Please enter password to logon" << endl; 
        
    int security = 0;
        
    
        do {
        string username;
        cout << "\nUsername: ";
        cin >> username;
        
        string 
        password;
        cout << "Password: ";
        cin >> password;
        
        //if user enter correct username and password
        if (username == "1" && password == "1") 
        {            
                     //clear the screen   
                     system("cls");
                     
                                 {                                                  // CHOICE STATEMENT OPEN        
                                          char choice;
                                          cout << "Welcome Admin\n\n1. Add New Student\n2. Delete Student\n3. Update Student\n4. Display All\n5. Save As Text\n6. Save As Binary\n7. Quit\n\n";
                                          cin >> choice;
                                                        while ( choice <='0' || choice > '7')
                                                        {
                                                              system("cls");
                                                              cout << "Please Enter A Vaild Option\n";
                                                              cout << "Welcome Admin\n\n1. Add New Student\n2. Delete Student\n3. Update Student\n4. Display All\n5. Save As Text\n6. Save As Binary\n7. Quit\n\n";
                                                              cin >> choice;
                                                        }
                                                        
    	                                  switch(choice)
    	                                  
    	                                  {                                         // SWITCH STATEMENT OPEN
                                                        case'1':system("cls");
                                                                cout<<"You Have Entered Add New Student Page.\n";                                                                       
                                                                cout<<"ADD NEW STUDENT.\n\n\n\n";
        
           
                                                        break;
                                                        
                                                        case'2':system("cls");
                                                                cout<<"Your Have Entered Delete Student Page\n";               
                                                                cout<<"DELETE STUDENT"; 
                                                               
                                                                                                                                                       
                                                        break;
                                                        
                                                        case'3':system("cls");
                                                                cout<<"You Have Entered Update student Page.\n";                                         
                                                                cout<<"UPDATE";
                                                                           
                                                        break;
                                                        
                                                        case'4':system("cls");
                                                                cout<<"Your Have Entered Display All Student Page\n";                                                                             
                                                                cout<<"DISPLAY\n\n\n\n"; 
                                                              
           
                                                                                 
                                                              
                                                                                 
                                                        break;                        
                                                                                 
                                                        case'5':system("cls");
                                                                cout<<"You Have Entered Save As Text Page.\n";                                                                       
                                                                cout<<"SAVE TEXT";
                                                                           
                                                        break;
                                                        
                                                        case'6':system("cls");
                                                                cout<<"Your Have Entered Save As Binary Page\n";                                                                             
                                                                cout<<"SAVE BINARY\n\n\n\n";
    
                                                        break;
                                                        
                                                        case'7':
                                                                system("cls");
                                                                cout << "System Shutting Down\n";
                                                                system("pause");           
        	                                                    return 0;                                                                           
                                                                                                                                                     
                                                        break;                                                                             
                                                                                                                                                                                                                          
                                          }                                         // SWITCH STATEMENT CLOSE
                                                                      
                                 }                                                  // CHOICE STATEMENT CLOSE
                             
    
    
    
    
    
    
                          
        security = 5;
        }
        
        if (!security) //if user enter incorrect password or username
     
           cout << "\nYour login has failed. Please check your username and password and try again\n\n";
        } while (!security);
           
    
    getch(); 
    return 0;
    }
    so the question is, how can i loop back to the 'choice' for every case? thank you
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Use a loop maybe?
    [code=cpp]
    while (1)
    {
    cin >> choice;
    switch (choice)
    {
    case 1:
    Add();
    break;
    case 2:
    exit(); //this is the quit choice.

    }
    [/code]

    Comment

    Working...