如何在switch语句后停止程序关闭

时间:2014-01-13 22:33:14

标签: c++ switch-statement

您好我正在制作一个带有四个选项菜单的基本程序。以下代码工作正常(我仍然有前三个选项的工作,但我会尝试自己这样做)唯一的问题是选择一个选项后它会中断程序关闭。我想知道我是如何做到的,以便程序在选择一个选项后返回菜单,程序仅在用户选择“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;

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:2)

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

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