【发布时间】:2016-07-31 08:55:16
【问题描述】:
我正在尝试在循环中使用 switch,并带有exit (3) 的选项。
如果我立即按 3 则一切正常,while 循环结束。
如果我先输入任何其他选项,然后第二次按“3”,循环会再继续一次,我需要再按一次“3”退出。
我尝试进行一些调试,它显示'exit' 等于 true,但循环仍然执行。
enum mainIndex { products = 1, clients, mainExit };
void Menue::start() {
bool exit = false;
int option;
while (!exit) {
cin >> option;
// swich menue
switch (option)
{
case products:
prodMenue();
break;
case clients:
clientMenue();
break;
case mainExit:
exit = true;
break;
default:
cout << "Error" << endl;
break;
}; // end switch
}
}
编辑:
enum prodIndex { prodAdd = 1, prodEditName, prodEditPrice, prodPrint, prodExit };
enum clientIndex { clientAddClient = 1, clientEdit, clientAddToCart, clientPurchase, clientExit };
void Menue::prodMenue() {
bool exit = false;
int optionProd;
while (!exit)
{
cin >> optionProd;
switch (optionProd)
{
case prodAdd:
addProduct();
break;
case prodEditName:
editProductPrice();
break;
case prodEditPrice:
editProductPrice();
break;
case prodPrint:
cout << market.getProducts();
break;
case prodExit:
exit = true;
break;
default:
cout << "Error" << endl;
break;
}// end switch
}// end while
start();
}
void Menue::clientMenue() {
bool exit = false;
int optionClient;
while (!exit)
{
cin >> optionClient;
switch (optionClient)
{
case clientAddClient:
addClient();
break;
case clientEdit:
editClient();
break;
case clientAddToCart:
addProdToCart();
break;
case clientPurchase:
buyCart();
break;
case clientExit:
exit = true;
break;
default:
cout << "Error" << endl;
break;
}// end switch
}// end while
start();
}
/***************************************************************************
*Menue class: *
*contains a user interface to control the store *
***************************************************************************/
class Menue {
Store market;
/***********************************************************************
*function name: prodMenue *
*The Input: input from user - number 1-5 (loop) *
***********************************************************************/
void prodMenue();
/***********************************************************************
*function name: clientMenue *
*The Input: input from user - number 1-5 (loop) *
***********************************************************************/
void clientMenue();
/***********************************************************************
*function name: addProduct *
*The Input: input from user - product serial name and price *
*The Function operation: creating new product if not in market *
***********************************************************************/
void addProduct();
/***********************************************************************
*function name: editProductName *
*The Input: input from user - product serial and new name *
*The Function operation: search for a product and edit its name if exis*
***********************************************************************/
void editProductName();
/***********************************************************************
*function name: editProductPrice *
*The Input: input from user - product serial and new price *
*The Function operation: search for a product and edit its price if exi*
***********************************************************************/
void editProductPrice();
/***********************************************************************
*function name: addClient *
*The Input: input from user - client id, name, adress, phone and credit*
*The Function operation: creating new costumer if not in market *
***********************************************************************/
void addClient();
/***********************************************************************
*function name: editProduct *
*The Input: input from user - client id, name, adress, phone and credit*
*The Function operation: editing existing product if in market *
***********************************************************************/
void editClient();
/***********************************************************************
*function name: editClientFull *
*The Input: ptr to costumer, id, name, adress, phone and credit *
*The Function operation: creating new product if not in market *
***********************************************************************/
void editClientFull(Costumer* client, const string &name,
const string &adress, const string &phone, const string &credit);
/***********************************************************************
*function name: addProdToCart *
*The Input: input from user - costumer id and product serial *
*The Function operation: creating new product if not in market *
***********************************************************************/
void addProdToCart();
/***********************************************************************
*function name: buyCart *
*The Input: input from user - client id *
*The Function operation: cleaning cart print it to screan + total price*
***********************************************************************/
void buyCart();
public:
/***********************************************************************
*function name: default Ctor *
***********************************************************************/
Menue() {};
/***********************************************************************
*function name: default Dtor *
***********************************************************************/
~Menue() {};
/***********************************************************************
*function name: start *
*The Input: input from user - number 1-3 (loop) *
***********************************************************************/
void start();
};
main.cpp
#include "menue.h"
int main() {
Menue navigat;
navigat.start();
return 0;
}
【问题讨论】:
-
I can not replicate the problem。问题很可能出在您没有向我们展示的代码中。
-
问题出在
prodMenue和/或clientMenue。 -
我添加了 prodMenue 和 clientMenue - 它们的工作原理相同。
-
我仍然无法复制问题。你能告诉我们你的 main() 和 start() 函数吗?您给定代码中的退出条件是 5 而不是 3。
-
问题仅在退出条件为 3 的开始菜单中。不适用于类菜单和主菜单
标签: c++ while-loop switch-statement conditional