2016-09-03 6 views
-4

私はC++プログラムを作成しました。これで私はユーザーのための多くのオプションを追加しました。 しかし、すべての選択肢では、ユーザーがプログラムを終了するか、または彼の選択に応じてメインメニューに戻ることができる機能を追加する必要があります。私のC++コードの「メインメニューに戻る」機能が必要

#include<iostream.h> 
#include<conio.h> 
void main() 
clrscr(); 
int choice,p_card; 
char text0,text1; 
cout<<"\n\n\t\t\t \"Welcome to Zinc hospital\"\n\n\n"; 
cout<<"\tMENU\n"; 
cout<<"\n\n1. Emergency treatment\n\n"; 
cout<<"2. Common treatment\n\n"; 
cout<<"3. Regular checkups\n\n"; 
cout<<"4. Get appointment\n\n"; 
cout<<"5. Consult specialist\n\n"; 
cout<<"6. Pay due amount\n\n"; 
cout<<"7. Log in for new patient card\n\n"; 
cout<<"8. For suggestions, feedbacks and register complains\n\n"; 
cout<<"\n\t\t\t\t\t\t\t\tChoice______"; 
cin>>choice; 
    clrscr(); 
     if(choice==1) 
     { 
     int e_choice; 
     cout<<"\n\n\n Enter the type of emergency"; 
     cout<<"\n\n1. Accidental case"; 
     cout<<"\n\n2. Heavy injury case"; 
     cout<<"\n\n3. Delicate organ injury\n\n"; 
     cout<<"4. Any other\n\n"; 
     cout<<"\n\n\t\t\t\t\t\t\tEmergency choice____"; 
     cin>>e_choice; 
     if(e_choice==4) 
     { 
     cout<<"Please specify the type of emergency "; 
     cin>>text0; 
     } 
     cout<<"\n\n\n\n\t\t\t  \"EMERGENCY DECLARED\"\n\n\t \'Please quickly proceed to the operation theatre with patient\'"; 
     } 
     if(choice==2) 
     { 
     cout<<"\n\nEnter the patient card number\n\n\t"; 
     cout<<"\t\t\tCard No.______"; 
     cin>>p_card; 
     cout<<"\n\n\t\t\tYour card has been recognized succesfully."; 
     cout<<"\n\n\nNow enter the specific treatment to be provided___"; 
     cin>>text0; 
     cout<<"\n\n\n\n  \t\t\tDATA recorded succesfully"; 
     cout<<"\n\nYour card has been charged $10.\n\n\n\n  Please proceed to counter to get the room no. and wait list serial."; 
     } 
     else if(choice==3) 
     { 
     cout<<"";//Under construction 
     } 
     else if(choice==4) 
     { 
     cout<<"";//Under construction 
     } 
     else if(choice==5) 
     { 
     cout<<"";//Under construction 
     } 
     else if(choice==6) 
     { 
     cout<<"";//Under construction 
     } 
     else if(choice==7) 
     { 
     cout<<"";//Under construction 
     } 
     else if(choice==8) 
     { 
     cout<<"";//Under construction 
     } 
     else if(choice!=1&&choice!=2&&choice!=3&&choice!=4&&choice!=5&&choice!=6&&choice!=7&&choice!=8) 
     { 
     cout<<"Invalid choice inputed"; 
     cout<<"\n\n\n\n\n\n\t\[email protected] ERROR"; 
     } 
getch(); 
} 
+2

スタックオーバーフローは、人々があなたのために無料で機能を書く場所ではありません。 –

+1

意味は....私は助けてくれない! –

+1

いいえ、それはそれが意味するものではありません。私が望むなら、あなたのためにこれを解決することができます。ニースを試してみてください。 –

答えて

1

ルックアップテーブル(多くのソリューションの1つ): - だから、私はcoding.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx に助けを持つことができます。

typedef (void) (P_Function)(void); 

struct Menu_Entry 
{ 
    unsigned int selection_number; 
    const char * text; 
    P_Function p_processing_function; 
}; 

void Process_Emergency_Treatment(void); 
void Process_Common_Treatment(void); 

Menu_Entry main_menu[] = 
{ 
    {1, "Emergency Treatment", Process_Emergency_Treatment}, 
    {2, "Common Treatment", Process_Common_Treatment}, 
}; 
static const unsigned int quantity_menu_items = 
    sizeof(main_menu)/sizeof(main_menu[0]); 

// ... 
unsigned int selection; 
std::cout << "Enter selection: "; 
std::cin >> selection; 
unsigned int index = 0U; 
for (index = 0U; index < quantity_menu_items; ++index) 
{ 
    if (main_menu[index].selection_number == selection) 
    { 
    main_menu[index].p_processing_function(); // Execute the command processor. 
    break; 
    } 
} 
if (index >= quantity_menu_items) 
{ 
    std::cout << "\nInvalid selection, try again.\n"; 
} 

ルックアップテーブルの便利な点は、メニューに項目を追加する場合は、テーブルに項目を追加することです。また、検索ループを一度テストするだけで済みます。テーブルにエントリを追加しても、検索ループは実行されません。

編集1:基本メニューのアルゴリズム
基本的なアルゴリズムは次のようになります。

bool selection_is_valid = false; 
while (!selection_is_valid) 
{ 
    Print_Menu(); 
    unsigned int selection = 0U; 
    std::cout << "Enter selection: "; 
    std::cin >> selection; 
    if (select >= MAXIMUM_CHOICES) 
    { 
    selection_is_valid = false; 
    } 
    else 
    { 
    Process_Menu_Item(selection); 
    selection_is_valid = true; 
    } 
} 

少しスキルであなたが押されると、「選択を存在」まで、上記のアルゴリズムを表示するように変更することができます。

関連する問題