C++循环一个菜单,直到选择了所有选项

C++ Iterating a menu until all choices have been selected

本文关键字:选择 选项 菜单 循环 一个 C++      更新时间:2023-10-16

我正在为一个c++类做最后一个项目。我的程序是一个盘点控制台应用程序。到目前为止,我已经能够让程序完全按照预期运行,通过项目下的每个项目和每个尺寸。我在程序中添加了一个菜单,让员工可以选择他们想要盘点的项目。我希望这个菜单一直显示,直到所有选项(项目)都被"考虑在内"。我做了一个切换案例,它有效,但它不会遍历所有项目,而是只遍历选定的单个项目。如何使菜单循环,直到所有项目都已入账?(因为这是在零售店盘点的要点)这是我的代码:

    //Name: Abdul Tabel
//Inventory program
#include <iostream> 
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std; 

// clothing item class declaration
class Item
{
    private: 
        double small;
        double medium;
        double large;
    public: 
        void setSmall(double);
        void setMedium(double);
        void setLarge(double);
        double getRem() const;
        double getSmall() const;
        double getMedium() const;
        double getLarge() const;
        double allRem() const;
};
//menu function
void showMenu()
{   
    cout << "Please choose an item from the menu to do inventory forn Only choose each item one time!";
    cout << "nType 'A' for Shirts";
    cout << "nType 'B' for Pants";
    cout << "nType 'C' Shoes";
    cout << "nType 'D' to quit the program" << endl;
}
//assign value to small item
void Item::setSmall(double null)
{
    small = null;   
}
//assign value to medium item
void Item::setMedium(double null)
{
    medium = null;  
}
//assign value to large item
void Item::setLarge(double null)
{
    large = null;   
}

//gets value from small variable
double Item::getSmall() const
{
    return small;
}
//gets value from medium variable
double Item::getMedium() const
{
    return medium;
}
//gets value from large variable
double Item::getLarge() const
{
    return large;
}
//gets total of reamining items
double Item::allRem() const
{
    return small + medium + large;
}


int main() 
{ 
    Item shirt;
    Item pants;
    Item shoes;
    double number;
    double totalRemaining;
    char selection;

    // constants for menu choice
    const char choice_a = 'A',
               choice_b = 'B',
               choice_c = 'C',
               quit_choice = 'D';
    // set output format
    cout << fixed << showpoint << setprecision(2);
    //show menu
    showMenu();
    cin >> selection;
    switch (selection)  // respond to the user's menu selection
    {
        case choice_a: // get shirt item inventory
                    cout << "Enter how many small shirts are left? ";
                    cin >> number;
                shirt.setSmall(number);
                    cout << "Enter how many medium shirts are left? ";
                    cin >> number;
                shirt.setMedium(number);
                    cout << "Enter how many large shirts are left? ";
                    cin >> number;
                shirt.setLarge(number);
            break;
        case choice_b: // get pants item inventory
                    cout << endl << "Enter how many small pants are left? ";
                    cin >> number;
                pants.setSmall(number);
                    cout << "Enter how many medium pants are left? ";
                    cin >> number;
                pants.setMedium(number);
                    cout << "Enter how many large pants are left? ";
                    cin >> number;
                pants.setLarge(number);
            break;
        case choice_c: // get shoes item inventory
                    cout << endl << "Enter how many small shoes are left? ";
                    cin >> number;
                shoes.setSmall(number);
                    cout << "Enter how many medium shoes are left? ";
                    cin >> number;
                shoes.setMedium(number);
                    cout << "Enter how many large shoes are left? ";
                    cin >> number;
                shoes.setLarge(number);
            break;
        case quit_choice:
            cout << "Program ending.n";
            cin.get(); cin.get(); 
            return 0;
            break;
        default:
            cout << "The valid choices are A through D. Run then"
                 << "program again and select one of those.n";
            cin.get(); cin.get();
            return 0;
    }

    //being displaying inventory results
    cout << endl << "n*******************";
    cout << endl << "*Inventory results*";
    cout << endl << "*******************" << endl;

    //displays shirt results
    if (shirt.getSmall() < 5)
        cout << endl << "There are " << shirt.getSmall() << " small shirts left. ORDER MORE!" << endl;
    else
        cout << endl << "There are " << shirt.getSmall() << " small shirts left." << endl;  
    if (shirt.getMedium() < 5)
        cout << "There are " << shirt.getMedium() << " medium shirts left. ORDER MORE!" << endl;
    else
        cout << "There are " << shirt.getMedium() << " medium shirts left." << endl;
    if (shirt.getLarge() < 5)
        cout << "There are " << shirt.getLarge() << " large shirts left. ORDER MORE!" << endl;
    else
        cout << "There are " << shirt.getLarge() << " large shirts left." << endl;
    cout << "There are a total of " << shirt.allRem() << " shirts left." << endl;

    // displays pant results
    if (pants.getSmall() < 5)
        cout << endl << "There are " << pants.getSmall() << " small pants left. ORDER MORE!" << endl;
    else
        cout << endl << "There are " << pants.getSmall() << " small pants left." << endl;
    if (pants.getMedium() < 5)
        cout << "There are " << pants.getMedium() << " medium pants left. ORDER MORE!" << endl;
    else
        cout << "There are " << pants.getMedium() << " medium pants left." << endl;
    if (pants.getLarge() < 5)
        cout << "There are " << pants.getLarge() << " large pants left. ORDER MORE!" << endl;
    else
        cout << "There are " << pants.getLarge() << " large pants left." << endl;
    cout << "There are a total of " << pants.allRem() << " pants left." << endl;

    // displays shoe results
    if (shoes.getSmall() < 5)
        cout << endl << "There are " << shoes.getSmall() << " small shoes left. ORDER MORE!" << endl;
    else
        cout << endl << "There are " << shoes.getSmall() << " small shoes left." << endl;
    if (shoes.getMedium() < 5)
        cout << "There are " << shoes.getMedium() << " medium shoes left. ORDER MORE!" << endl;
    else
        cout << "There are " << shoes.getMedium() << " medium shoes left." << endl;
    if (shoes.getLarge() < 5)
        cout << "There are " << shoes.getLarge() << " large shoes left. ORDER MORE!" << endl;
    else
        cout << "There are " << shoes.getLarge() << " large shoes left." << endl;
    cout << "There are a total of " << shoes.allRem() << " shoes left." << endl;

    cin.get(); cin.get();
    return 0;
}

cin.get(); cin.get();
return 0;

}

PS:在没有切换的情况下,我让程序成功地遍历了每个项目,并显示了所有结果,没有任何"垃圾"结果。垃圾结果只在我完成切换后出现,因为输入只占一个项目。

PSS:我还尝试了一个do-white循环来封装交换机外壳,但它没有按预期工作。我对不同类型的解决方案持开放态度,它不必最终成为切换案例,尽管如果有一种简单的方法来合并切换案例,那就更好了。谢谢!

编辑:这是没有切换案例或菜单的干净代码

    //Name: Abdul Tabel
//Inventory program
#include <iostream> 
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std; 

// clothing item class declaration
class Item
{
    private: 
        double small;
        double medium;
        double large;
    public: 
        void setSmall(double);
        void setMedium(double);
        void setLarge(double);
        double getRem() const;
        double getSmall() const;
        double getMedium() const;
        double getLarge() const;
        double allRem() const;
};
//menu function
void showMenu()
{   
    cout << "Please enter the items remaining for the following items" << endl;
}
//assign value to small item
void Item::setSmall(double null)
{
    small = null;   
}
//assign value to medium item
void Item::setMedium(double null)
{
    medium = null;  
}
//assign value to large item
void Item::setLarge(double null)
{
    large = null;   
}

//gets value from small variable
double Item::getSmall() const
{
    return small;
}
//gets value from medium variable
double Item::getMedium() const
{
    return medium;
}
//gets value from large variable
double Item::getLarge() const
{
    return large;
}
//gets total of reamining items
double Item::allRem() const
{
    return small + medium + large;
}


int main() 
{ 
    Item shirt;
    Item pants;
    Item shoes;
    double number;
    double totalRemaining;
    char selection;

    // set output format
    cout << fixed << showpoint << setprecision(2);
    //show menu
    showMenu();
                    cout << "Enter how many small shirts are left? ";
                    cin >> number;
                shirt.setSmall(number);
                    cout << "Enter how many medium shirts are left? ";
                    cin >> number;
                shirt.setMedium(number);
                    cout << "Enter how many large shirts are left? ";
                    cin >> number;
                shirt.setLarge(number);
                    cout << endl << "Enter how many small pants are left? ";
                    cin >> number;
                pants.setSmall(number);
                    cout << "Enter how many medium pants are left? ";
                    cin >> number;
                pants.setMedium(number);
                    cout << "Enter how many large pants are left? ";
                    cin >> number;
                pants.setLarge(number);
                    cout << endl << "Enter how many small shoes are left? ";
                    cin >> number;
                shoes.setSmall(number);
                    cout << "Enter how many medium shoes are left? ";
                    cin >> number;
                shoes.setMedium(number);
                    cout << "Enter how many large shoes are left? ";
                    cin >> number;
                shoes.setLarge(number);

    //being displaying inventory results
    cout << endl << "n*******************";
    cout << endl << "*Inventory results*";
    cout << endl << "*******************" << endl;

    //displays shirt results
    if (shirt.getSmall() < 5)
        cout << endl << "There are " << shirt.getSmall() << " small shirts left. ORDER MORE!" << endl;
    else
        cout << endl << "There are " << shirt.getSmall() << " small shirts left." << endl;  
    if (shirt.getMedium() < 5)
        cout << "There are " << shirt.getMedium() << " medium shirts left. ORDER MORE!" << endl;
    else
        cout << "There are " << shirt.getMedium() << " medium shirts left." << endl;
    if (shirt.getLarge() < 5)
        cout << "There are " << shirt.getLarge() << " large shirts left. ORDER MORE!" << endl;
    else
        cout << "There are " << shirt.getLarge() << " large shirts left." << endl;
    cout << "There are a total of " << shirt.allRem() << " shirts left." << endl;

    // displays pant results
    if (pants.getSmall() < 5)
        cout << endl << "There are " << pants.getSmall() << " small pants left. ORDER MORE!" << endl;
    else
        cout << endl << "There are " << pants.getSmall() << " small pants left." << endl;
    if (pants.getMedium() < 5)
        cout << "There are " << pants.getMedium() << " medium pants left. ORDER MORE!" << endl;
    else
        cout << "There are " << pants.getMedium() << " medium pants left." << endl;
    if (pants.getLarge() < 5)
        cout << "There are " << pants.getLarge() << " large pants left. ORDER MORE!" << endl;
    else
        cout << "There are " << pants.getLarge() << " large pants left." << endl;
    cout << "There are a total of " << pants.allRem() << " pants left." << endl;

    // displays shoe results
    if (shoes.getSmall() < 5)
        cout << endl << "There are " << shoes.getSmall() << " small shoes left. ORDER MORE!" << endl;
    else
        cout << endl << "There are " << shoes.getSmall() << " small shoes left." << endl;
    if (shoes.getMedium() < 5)
        cout << "There are " << shoes.getMedium() << " medium shoes left. ORDER MORE!" << endl;
    else
        cout << "There are " << shoes.getMedium() << " medium shoes left." << endl;
    if (shoes.getLarge() < 5)
        cout << "There are " << shoes.getLarge() << " large shoes left. ORDER MORE!" << endl;
    else
        cout << "There are " << shoes.getLarge() << " large shoes left." << endl;
    cout << "There are a total of " << shoes.allRem() << " shoes left." << endl;

    cin.get(); cin.get();
    return 0;
}

我在底部贴出了自己问题的答案。我放弃了菜单的想法,选择了其他东西。如何将此问题标记为已解决?感谢所有的帮助!

  1. 您的代码中存在复制粘贴错误
  2. 在问题描述中,你说"我为程序添加了一个菜单,让员工能够选择他们想要盘点的项目。我希望这个菜单一直显示,直到所有的选择(项目)都被考虑在内"

    那么,如果他们必须在显示结果之前选择所有项目,那么让他们选择他们想要进行库存的项目有什么意义呢

    我建议你取下开关盒,把"衬衫"、"裤子"answers"鞋子"一一放好。不需要开关箱

我放弃了整个菜单的想法,添加了不同的循环和方法来延长代码。我添加了一个类,它为丢失的库存项目生成要下的订单。这是我完成的最后一个项目。谢谢你的帮助!

        //Name: Abdul Tabel
    //Inventory program
    #include <iostream> 
    #include <iomanip>
    #include <string>
    #include <cstring>
    #include <fstream>
    #include <cstdlib>
    #include <array>
    #include <list>
    using namespace std; 

    // clothing item class declaration
    class Item
    {
        private: 
            double small;
            double medium;
            double large;
            string description;
        public: 
            void setDescription(string);
            void setSmall(double);
            void setMedium(double);
            void setLarge(double);
            string getDescription() const;
            double getRem() const;
            double getSmall() const;
            double getMedium() const;
            double getLarge() const;
            double allRem() const;
            bool orderMore(int);
            /*Item();*/
    };

    class Order
    {
        private:
            string description;
            string size;
            double qty;
            int OrderNumber;
        public:
            void setDescription(string);
            void setSize(string);
            void setQty(double);
            void setOrderNumber();
            string getDescription() const;
            string getSize() const;
            double getQty() const;
            int getOrderNumber() const;
    };
    //methods for Order class
        string Order::getDescription() const
        {
            return description;
        }
        string Order::getSize() const
        {
            return size;
        }
        double Order::getQty() const
        {
            return qty;
        }
        int Order::getOrderNumber() const
        {
            return OrderNumber;
        }
        void Order::setDescription(string x)
        {
            description = x;
        }
        void Order::setSize(string x)
        {
            size = x;
        }
        void Order::setQty(double x)
        {
            qty = x;  
        }
        void Order::setOrderNumber()
        {
            OrderNumber = (rand() % 900) + 100;
        }
    bool Item::orderMore(int Count)
    {
        bool returnValue = false;
        if (Count < 5)
        {
            returnValue = true;
        }
        return returnValue;
    }
    //menu function
    void showMenu()
    {   
        cout << "Please enter the items remaining for the following items" << endl;
    }
        //assign value to small item
    void Item::setDescription(string x)
    {
        description = x;    
    }
    //assign value to small item
    void Item::setSmall(double number)
    {
        small = number; 
    }
    //assign value to medium item
    void Item::setMedium(double number)
    {
        medium = number;    
    }
    //assign value to large item
    void Item::setLarge(double number)
    {
        large = number; 
    }
    string Item::getDescription() const
    {
        return description;
    }

    //gets value from small variable
    double Item::getSmall() const
    {
        return small;
    }
    //gets value from medium variable
    double Item::getMedium() const
    {
        return medium;
    }
    //gets value from large variable
    double Item::getLarge() const
    {
        return large;
    }
    //gets total of reamining items
    double Item::allRem() const
    {
        return small + medium + large;
    }



    int main() 
    { 
        double number;
        std::array <string,4> itemType = {"shirts", "pants", "shoes", "coats"};
        //dynamic allocation of array size
        Item *items;
        items = new Item[itemType.size()];

        //for loop will iterate through all items in array
        showMenu();
        for(int i=0; i < itemType.size(); i++)
        {
            items[i].setDescription(itemType[i]);
                        cout << "Enter how many small " + itemType[i] + " are left? ";
                        cin >> number;
                        items[i].setSmall(number);
                        cout << "Enter how many medium " + itemType[i] + " are left? ";
                        cin >> number;
                    items[i].setMedium(number);
                        cout << "Enter how many large " + itemType[i] + " are left? ";
                        cin >> number;
                    items[i].setLarge(number);
        }

        //being displaying inventory results
        cout << endl << "n*******************";
        cout << endl << "*Inventory results*";
        cout << endl << "*******************" << endl;
        // dynamically creates a list for the unknown orders
        list<Order> orders;
        // Output the quantitis entered back to the user and create orders for the ones that need orders.
        for(int i=0; i < itemType.size(); i++)
        {
            cout << endl << "There are " << items[i].getSmall() << " small " << items[i].getDescription() << " left. ";
            if (items[i].orderMore(items[i].getSmall()))
            {
                cout << " Automatically creating order for this product";
                Order m;
                m.setDescription(items[i].getDescription());
                m.setQty(5-items[i].getSmall());
                m.setSize("small");
                m.setOrderNumber();   
                orders.push_front(m);
            }
            cout << endl << "There are " << items[i].getMedium() << " medium " << items[i].getDescription() << " left.";
            if (items[i].orderMore(items[i].getMedium()))
            {
                cout << " Automatically creating order for this product";
                Order m;
                m.setDescription(items[i].getDescription());
                m.setQty(5-items[i].getMedium());
                m.setSize("medium");
                m.setOrderNumber();   
                orders.push_front(m);
            }
            cout << endl << "There are " << items[i].getLarge() << " large " << items[i].getDescription() << " left.";
            if (items[i].orderMore(items[i].getLarge()))
            {
                cout << " Automatically creating order for this product";
                Order m;
                m.setDescription(items[i].getDescription());
                m.setQty(5-items[i].getLarge());
                m.setSize("large");
                m.setOrderNumber();   
                orders.push_front(m);
            }
            cout << endl << "There are " << items[i].allRem() << " total " << items[i].getDescription() << " left." << endl;
        }
        cout << endl << "n*******************";
        cout << endl << "*  Order results  *";
        cout << endl << "*******************" << endl;
        list<Order>::iterator iter; //iterator is used to iterate through all objects in the list
        // loop for orders
        for (iter = orders.begin(); iter != orders.end(); iter++)
        {           
            cout << endl << "Order placed for " << iter->getQty() << " " << iter->getSize() << " " << iter->getDescription() << " on order number: " << iter->getOrderNumber();
        }
        cin.get(); cin.get();
        return 0;
    }