向量中的向量总是0?

时间:2014-08-24 10:24:19

标签: c++ vector struct

以下代码应如下所示:

  • 获取某张桌子的订单
  • 检查该订单是否在文本文件中,因此可用
  • 如果可用,则按向量并向其添加订单

下次运行此函数时,它应该只添加到向量的末尾。但由于某种原因,每次进入此函数时,向量的大小都会重置为0。

示例:

  • 新订单是餐桌1的牛排
  • 所以allOrders [numberTable] .nameProduct [1]应该是牛排
  • 停止功能
  • 现在我们想要另一个订单
  • 此订单包含表1的可乐
  • 所以我会假设allOrders [numberTable] .nameProduct [2]将是可乐的
  • 但可乐覆盖牛排

那我哪里出错了?

向量是在学生头文件public

中创建的
struct order{
    std::vector <std::string> nameProduct;
};
order allOrders[tables];

编辑:即使我改变了我阅读信息的方式,我仍然会遇到同样的问题。矢量大小在开始时始终为0.

代码:

void student::newOrder(){
int numberTable;
std::cout << "Order for which table: ";
std::cin >> numberTable;
std::string line;
std::string itemOrdered;
std::size_t found;
int size;

//As long as true
while(1){
    //What item is ordered
    std::cout << "Product: ";
    std::cin >> itemOrdered;
    //A way to stop the loop
    if(itemOrdered == "stop"){
        std::cout << "No more orders.\n";
        break;
    }
    std::fstream menuFileRead ("C:\\Users\\kk\\Desktop\\menu.txt");
    if (!menuFileRead || !menuFileRead.good())
    {
        std::cout << "ERROR8: could not open file!\n";
    }
    //If the file is open...
    if(menuFileRead.is_open()){
        //While file is not at the end of the file
        while(menuFileRead >> line){
            //Find item that is ordered in line
            found = line.find(itemOrdered);
            //If item is found and not nothing
            if(found != std::string::npos){
                //Make room in vector
                allOrders[numberTable].nameProduct.push_back(itemOrdered);
                //Get the size of the vector
                size = allOrders[numberTable].nameProduct.size();
                break;
            }
        }
        menuFileRead.close();
    }
    else{
        std::cout << "ERROR7: menu file could not be opened.\n";
    }
}
std::cout << allOrders[numberTable].nameProduct.size() << "\n";
for(int j = 0; j < size; j++){
    std::cout << allOrders[numberTable].nameProduct[j] << "\n";
}

}

主要功能的一部分:

while(isLoggedIn == true){
        users genericUsersObj;
        //Depending of the function of the user, a different menu will be printed
        if(loginType == "baas"){
            baas genericBaasObj;
            userChoice = genericBaasObj.printMenuStudent();
            //Change progression depending on choice
            switch(userChoice){
                case(11):
                    //Add a user
                    genericBaasObj.userAdd();
                    break;
                case(12):
                    //Remove a user
                    genericBaasObj.userRemove();
                    break;
                case(13):
                    //Go back
                    break;
                case(2):
                    //Print menu with foods and drinks
                    genericBaasObj.printFoodMenu();
                    break;
                case(3):
                    //Make a new order
                    genericBaasObj.newOrder();
                    break;
                case(4):
                    //Change an order if something went wrong
                    genericBaasObj.orderChange();
                    break;
                case(5):
                    //TODO removeOrder Baas
                    break;
                case(6):
                    //TODO closeOrder Baas
                case(7):
                    loginObj.logout();
                    isLoggedIn

0 个答案:

没有答案