【问题标题】:while loop wont end, despite false condition尽管条件错误,但循环不会结束
【发布时间】: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


【解决方案1】:

根据我的经验,

如果我在 while 循环中使用 switch,cpu 被锁定并且没有响应任何用户或系统输入。

这里有一个例子是锁定我的 cpu 而不是打印到屏幕“打破 while 循环”,为什么会发生这种情况我不知道但是当我输入等待函数到案例 1 循环真的被打破了。

int condition = 1;
bool loop = true;
while(loop ){

    switch(condition){

       case 1:
         wait(1); // if you put this with any msecond, loop can be break
         cout << "Enter 2 for exit" << endl;
         cin >> condition;

       break;

       case 2:

         cout << "Exiting";
         loop = false;

       break;

       default:

       break;

    }
}

cout << "breaking the while loop" << endl;

【讨论】:

    【解决方案2】:

    clientMenue()prodMenue() 函数再次调用start(); 是导致您的问题的原因。由于这些函数已经从 start() 函数中调用,因此无需从这些函数内部调用 start() 函数,因为完成后您将返回到 start()

    作为一个例子,你正在做的是:

        (1st)Start()---calls--->clientMenue()---calls---->(2nd)Start()
    So  (2nd)start() --return to-->clientMenu()---return to--->(1st)start()
    

    假设您显示一次 clientMenue() 并决定从 Start() 退出,这就是为什么您需要为 start 提供两次退出条件,每个 start() 函数一个。如果您通过调用 clientMenue() 或 prodMenue() 函数 N 次而不选择从 start() 退出来显示 Client 或 Product 菜单,那么当您决定退出 start() 时,您必须输入您的退出条件N+1次。

    所以从clientMenue()prodMenue() 函数中删除对start() 函数的调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2020-05-18
      • 2012-02-21
      相关资源
      最近更新 更多