虽然错误的情况下循环不会结束

时间:2016-04-10 08:38:24

标签: c++ while-loop switch-statement conditional

我正在尝试在循环中使用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;
}

2 个答案:

答案 0 :(得分:0)

再次从start();clientMenue()函数调用prodMenue()是导致问题的原因。由于已经从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()函数来显示客户端或产品菜单,而不选择退出start(),那么当您决定退出start()时,您必须输入退出条件N + 1次。

因此,请从start()clientMenue()函数中删除对prodMenue()函数的调用。

答案 1 :(得分:0)

根据我的经验,

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

这里有一个示例是锁定我的cpu而不是打印到屏幕&#34;打破while循环&#34;,为什么会发生这种情况我不知道但是当我输入等待函数到案例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;
相关问题