因此,我是C ++的新手。我有此代码,需要输入要查看的内容。代码中发生的事情是,当我选择一个输入(例如1)时,它将显示Peripherals列表。当我在“外围设备列表”中输入5时,它将返回主菜单。在主菜单中,如果输入了4,则主菜单正在循环播放或一次又一次地重复。如果我输入4,则需要退出它或结束。
#include <iostream>
using namespace std;
int main()
{
int choice;
int pick;
char view;
cout << "\n" << endl;
cout << "\t \t -------------------------------------------------------------------" << endl;
cout << "\t \t \t \t \t WELCOME TO SHOPPING SPREE" << endl;
cout << "\t \t -------------------------------------------------------------------" << endl;
//MENU
do {
cout << "What type of items would you like to view?" << endl;
cout << " [1] Peripherals" << endl;
cout << " [2] Mobile Phones" << endl;
cout << " [3] Consoles" << endl;
cout << " [4] Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 1) {
cout << "\n";
cout << "What peripherals would you like to purchase?" << endl;
cout << "[1] HyperX Alloy FPS PRO" << endl;
cout << "[2] SteelSeries APEX PRO" << endl;
cout << "[3] Razer Kraken X" << endl;
cout << "[4] AORUS K7" << endl;
cout << "[5] BACK TO MENU" << endl;
cout << "Enter your choice: ";
cin >> pick;
}
else if (choice == 2) {
cout << "\n";
cout << "What mobile phones would you like to purchase?" << endl;
cout << "[1] Xiaomi Mi Mix 3" << endl;
cout << "[2] Oppo Reno" << endl;
cout << "[3] Realme 5" << endl;
cout << "[4] Samsung Galaxy 10" << endl;
cout << "[5] BACK TO MENU" << endl;
cout << "Enter your choice: ";
cin >> pick;
}
else if (choice == 3) {
cout << "\n";
cout << "What consoles would you like to purchase?" << endl;
cout << "[1] PlayStation 5" << endl;
cout << "[2] Nintendo Switch" << endl;
cout << "[3] PlayStation 4" << endl;
cout << "[4] XBOX S" << endl;
cout << "[5] BACK TO MENU" << endl;
cout << "Enter your choice: ";
cin >> pick;
}
}
while (pick == 5);
}
您需要在4时做一件事情choice
(当前您什么都不做)。
那件事是break
:结束循环。
else if (choice == 4) {
break;
}
或者,将其构建到循环条件中:
while (choice != 4 && pick == 5);
…尽管就我个人而言,我认为这很难遵循。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句