C ++代码帮助(While循环)

时间:2016-02-18 16:14:51

标签: c++

#include <iostream>
using namespace std;

int main()

{
int choice;


cout << "                         Serendipity Booksellers\n";
cout << "                                Main Menu\n";
cout << "\n                      1.  Cashier Module";
cout << "\n                      2.  Inventory Database Module";
cout << "\n                      3.  Report Module";
cout << "\n                      4.  Exit\n";
cout << "\n                      Enter Your Choice: ";
cin >> choice;

while (choice != 4)
{


}


return 0;
  

主菜单上的第四项是“退出”,允许用户结束   程序。添加一个循环到mainmenu.cpp程序,导致它   重复显示菜单,直到用户选择第四项。

这是我的任务,任何人都可以帮助我在while语句中加入什么来完成它吗?

4 个答案:

答案 0 :(得分:0)

你想做什么&#34;而选择不是4&#34;?

可能你想显示菜单并要求另一个选择,对吧? 那应该已经回答了你的问题;)

答案 1 :(得分:0)

例如,循环可能看起来像

int choice = 0;

do
{
    cout << "                         Serendipity Booksellers\n";
    cout << "                                Main Menu\n";
    cout << "\n                      1.  Cashier Module";
    cout << "\n                      2.  Inventory Database Module";
    cout << "\n                      3.  Report Module";
    cout << "\n                      4.  Exit\n";
    cout << "\n                      Enter Your Choice: ";

} while ( cin >> choice && choice != 4 );

或者

do
{
    cout << "                         Serendipity Booksellers\n";
    cout << "                                Main Menu\n";
    cout << "\n                      1.  Cashier Module";
    cout << "\n                      2.  Inventory Database Module";
    cout << "\n                      3.  Report Module";
    cout << "\n                      4.  Exit\n";
    cout << "\n                      Enter Your Choice: ";

    if ( !( cin >> choice ) ) break;

    switch ( choice )
    {
    case 1:
    case 2;
    case 3:
    default:
        cout << "Let's repeat one more!" << endl;
        break;
    case 4:
        cout << "Bye!" << endl;
        break;
    }
} while ( choice != 4 );

答案 2 :(得分:0)

将所有的couts和cin放在那里

#include <iostream>
using namespace std;

int main()
{
  int choice = 0;
  while (choice != 4)
  {
    cout << "                         Serendipity Booksellers\n";
    cout << "                                Main Menu\n";
    cout << "\n                      1.  Cashier Module";
    cout << "\n                      2.  Inventory Database Module";
    cout << "\n                      3.  Report Module";
    cout << "\n                      4.  Exit\n";
    cout << "\n                      Enter Your Choice: ";
    cin >> choice;
  }

  return 0;
}

答案 3 :(得分:0)

您正在选择用户输入并关注它,以便在每次选择不是“4”时重复菜单。

#include <iostream>
using namespace std;

int main()

{
int choice;

do {

cout << "                         Serendipity Booksellers\n";
cout << "                                Main Menu\n";
cout << "\n                      1.  Cashier Module";
cout << "\n                      2.  Inventory Database Module";
cout << "\n                      3.  Report Module";
cout << "\n                      4.  Exit\n";
cout << "\n                      Enter Your Choice: ";
cin >> choice;
}while (choice != 4);
return 0;

P.S。:优良作法是使用'endl'来结束一行而不是“\ n” - 例如:

    #include <iostream>
    using namespace std;

    int main()

    {
    int choice;

    do {

    cout << "Serendipity Booksellers" << endl;
    cout << "Main Menu" << endl;
    cout << "1.  Cashier Module" << endl;
    cout << "2.  Inventory Database Module" << endl;
    cout << "3.  Report Module" << endl;
    cout << "4.  Exit" << endl;
    cout << "Enter Your Choice: " << endl;
    cin >> choice;

    } while (choice != 4);
    return 0;