C ++迭代菜单直到选择了所有选项

时间:2015-06-30 23:28:54

标签: c++

我正在研究我的c ++课程的最终项目。我的程序是一个盘点控制台应用程序。到目前为止,我已经能够通过项目下的每个项目和每个大小使程序完全按预期运行。我在程序中添加了一个菜单,供员工选择要为其进行库存的项目。我希望这个菜单能够一直显示,直到所有选项(项目)都被记录为#34;。我做了一个可以工作的开关盒,但它不会遍历所有项目,而只是通过选择的单个项目。如何在所有项目都被计算之前进行菜单循环? (因为这是在零售店做库存的重点) 这是我的代码:

    //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 for\n 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 the\n"
                 << "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:没有开关的情况,我得到程序成功迭代每个单项并显示所有结果,没有任何&#34;垃圾&#34;结果。垃圾结果仅在我执行切换案例后才出现,因为输入仅占一个项目。

PSS:我还尝试了一个包含开关盒的do while循环,但它没有按预期工作。我对不同类型的解决方案持开放态度,它不必最终成为交换机案例,尽管如果有一种简单的方法来合并交换机案例,那么它是可取的。 谢谢!

编辑:这里是没有开关盒或菜单的清洁代码

    //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;
}

我在底部发了一个自己问题的答案。我取消了菜单的想法,并与其他东西一起去了。如何将此问题标记为已解决? 感谢所有帮助!

2 个答案:

答案 0 :(得分:0)

  1. 您的代码中存在复制粘贴错误
  2. 在问题描述中,您说“我在程序中添加了一个菜单,以便员工能够选择要为其进行库存的项目。我希望此菜单一直显示,直到所有选项(项目)都有已经计入了“。
    ”那么,如果他们必须在显示结果之前选择所有项目,那么他们可以选择选择他们想要进行库存的项目吗?
    我建议你取下开关盒,然后逐一放上“衬衫”,“裤子”和“鞋子”。不需要开关盒。

答案 1 :(得分:0)

我已经废弃了整个菜单的想法,并添加了不同的循环和方法来使我的代码更长。我添加了一个类,可以为丢失的库存项目生成订单。 这是我的最终项目完成。感谢所有想要帮助的人!

        //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;
    }