2014-01-13 95 views
-3

嗨我目前正在做一個基本的程序與四個選項菜單。下面的代碼工作正常(我仍然有工作要做的前三個選項,但我會嘗試做我自己的)唯一的問題是選擇一個選項後,它打破了程序關閉。我想知道如何做到這一點,以便在選擇一個選項後程序返回菜單,並且程序只在用戶選擇「4」時關閉。謝謝!如何在開關語句後停止程序關閉

cout << "Correct login details entered!" << "" << endl; 
cout << "1. Transfer an amount" <<endl; 
cout << "2. List recent transactions"<<endl; 
cout << "3. Display account details and current balance"<<endl; 
cout << "4. Quit" << endl; 
cout << "Please enter menu number"<<endl; 
cin >> selection; 


switch(selection) 
{ 
case 1: 
cout << "You have choosen to transfer an amount" << endl; 
cout << "How much do you wish to transfer from the shop account?"<<endl; 
cin >> B; 
cout << B << endl; 
break; 


case 2: 
cout << "Here are you're recent transactions" <<endl; 
break; 

case 3: 
cout << "The account names is:" << name << endl; 
cout << "The account number is:" << number << endl; 
cout << "The current balance is" << endl; //Need to get cuurent balance still 
break; 

case 4: 
return 0; 
break; 

default: 
cout << "Ooops, invalid selection!" << endl; 
break; 
+0

添加一個循環在你的「菜單」代碼 – prajmus

+0

好了,你可以先試着寫一個小程序,讀取輸入,直到它得到一個1,2,或3 –

+0

我開始假設你正在Visual Studio中運行控制檯應用程序?在main()退出後,應用程序關閉窗口。編輯:哦,我看到你想要在你的交換機周圍循環。 – drescherjm

回答

3
do{ 
    cout << "Correct login details entered!" << "" << endl; 
    cout << "1. Transfer an amount" <<endl; 
    cout << "2. List recent transactions"<<endl; 
    cout << "3. Display account details and current balance"<<endl; 
    cout << "4. Quit" << endl; 
    cout << "Please enter menu number"<<endl; 
    cin >> selection; 

    switch(selection) 
    { 
    case 1: 
    //... 

    case 2: 
    //... 

    case 3: 
    //... 

    case 4: 
    //... 

    default: 
    //... 

    } 
while(selection != 4); 
+0

非常感謝! – user3057816

2
while (true) 
{ 
    cout << "1. Transfer an amount" <<endl; 
    // ... 

    switch(selection) 
    { 
    case 1: 
    // ... 
    } // end of switch 
}