C++ 关于结构并将其用于数组';s

C++ 关于结构并将其用于数组';s,c++,arrays,struct,C++,Arrays,Struct,我正在尝试编写一个程序,它将从文本文件中获取每一行并将值加载到数组中。然而,由于某种原因,当我尝试创建一个动态数组并尝试将信息放在0以外的任何位置时,从0位置传来的信息被复制过来,我似乎不明白为什么。特别是在这个程序中,它的readInventory函数是我写的。基本上,为什么我不能将一个结构复制到另一个 文件样本 A009 Strawberries_Case 0 12.50 8 4028 STRAWBERRIES_PINT 0 0.99 104 4383 MINNEOLAS 1 0.79 18

我正在尝试编写一个程序,它将从文本文件中获取每一行并将值加载到数组中。然而,由于某种原因,当我尝试创建一个动态数组并尝试将信息放在0以外的任何位置时,从0位置传来的信息被复制过来,我似乎不明白为什么。特别是在这个程序中,它的readInventory函数是我写的。基本上,为什么我不能将一个结构复制到另一个

文件样本

A009 Strawberries_Case 0 12.50 8
4028 STRAWBERRIES_PINT 0 0.99 104
4383 MINNEOLAS 1 0.79 187.3
4261 Rice_1_LB_Bag 0 0.49 107
来自程序的代码

#include <iostream>
#include <string>
#include <cstring>
#include <iomanip>
#include <fstream>
using namespace std;

struct Product
{
   string PLU;
   string name;
   int salesType;
   double unitPrice/*rice per pound*/;
   double inventory;
};

struct ItemSold
{
   string PLU;
   string name;
   double cost;

};

Product *inventoryLevels = new Product[100];
ItemSold *itemsSold = new ItemSold[100];

bool readInventory(string filename, int &numberOfItems);
double checkout(int inventoryLength);
double price(string PLU, double units);
int typeCheck(string PLU, int inventoryLength);
string nameCheck(string PLU, int inventoryLength);



int main()
{

   int numberOfItems = 0;
   string filename = "products.txt";
   int total;

   if (readInventory(filename, numberOfItems))
   {
      cout << "Inventory file has errors, please make changes before continuing" << endl << endl;
   }


   total = checkout(numberOfItems);
   cout << total;


   system("pause");
}



double checkout(int inventoryLength)
{  // Function that will be used to perform the checkout by the user


   string PLU = "1";

   double units/*pounds*/;
   int salesType;
   int counter = 0;
   int temp;
   double total = 0;

   while (PLU != "0")
   {
      cout << "Enter a PLU: ";
      cin >> PLU;
      itemsSold[counter].PLU = PLU;

      if (PLU == "0")
      {
         // do nothing
      }

      else
      {
         itemsSold[counter].name = nameCheck(PLU, inventoryLength);
         if (typeCheck(PLU, inventoryLength) == 0)
         {
            cout << " Enter the number of units being bought: ";
            cin >> units;
            while (units > inventoryLevels[counter].inventory)
            {
               cout << "You have entered in more units than we have on hand \n Please reduce the number of units being bought\n";
               cout << " Enter the number of units being bought: ";
               cin >> units;
            }

            itemsSold[counter].cost = price(PLU, units);
            inventoryLevels[counter].inventory -= units;


         }
         else
         {
            cout << "Enter the number of pounds of the item being bought: ";
            cin >> units;
            itemsSold[counter].cost = price(PLU, units);

            while (units > inventoryLevels[counter].inventory)
            {
               cout << "You have entered in more pounds than we have on hand \n Please reduce the number of pounds being bought\n";
               cout << "Enter the number of pounds of the item being bought: ";
               cin >> units;
            }

            inventoryLevels[counter].inventory -= units;

         }

         counter++;
      }


   }

   temp = counter;
   while (temp >= 0)
   {
      total += itemsSold[temp].cost;
      temp--;
   }



   return total;

}


string nameCheck(string PLU, int inventoryLength)
{
   for (int k = 0; k < inventoryLength; k++)
   {
      if (inventoryLevels[k].PLU == PLU)
      {
         return inventoryLevels[k].name;
      }
   }

   return "We are currently out of stock of this item.";
}

int typeCheck(string PLU, int inventoryLength)
{
   for (int k = 0; k < inventoryLength ; k++)
   {
      if (inventoryLevels[k].PLU == PLU)
      {
         return inventoryLevels[k].salesType;
      }
   }
}


double price(string PLU, double units)
{ // 
   double price;

   for (int k = 0; k < 100; k++)
   {
      if (inventoryLevels[k].PLU == PLU)
      {
        price = units * (inventoryLevels[k].unitPrice);
        return price;
      }
   }

}


bool readInventory(string filename, int &numberOfItems)
{
   // File object
   fstream inventory;

   // Some temp variable used to validate information is still in file while it is being transfered to array
   //string temp;

   // Open the inventory file
   inventory.open(filename);



   // Will temporarily hold the properties of an item until loaded onto the array
   Product temp;


   // Counter will allow for a new item to be stored onto the next available location in the array
   int counter = 0;


   // Will demonstrate whether or not there is an error
   int error = 0;

   // Store items and their properties in the global array
   while (inventory >> temp.PLU >> temp.name >> temp.salesType >> temp.unitPrice >> temp.inventory)
   {
      // Checks to see if they 


      if ((temp.PLU.at(0) > 57) || (temp.PLU.at(1) > 57) || (temp.PLU.at(2) > 57) || (temp.PLU.at(3) > 57))
      {
         error++;
      }

      else
      { 

         inventoryLevels[numberOfItems].PLU = temp.PLU;
         inventoryLevels[numberOfItems].name = temp.name;
         inventoryLevels[numberOfItems].salesType = temp.salesType;
         inventoryLevels[numberOfItems].unitPrice = temp.unitPrice;
         inventoryLevels[numberOfItems].inventory = temp.inventory;

       numberOfItems++;
      counter++;
      }


   }

   // If there is no error return true
   if (error == 0)
   {
      return false;
   }
   // If there is an error return false
   else if (error > 0)
   {
      return true;
   }

}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构产品
{
字符串PLU;
字符串名;
int-salesType;
双倍单价/*每磅大米*/;
双重库存;
};
结构项已售出
{
字符串PLU;
字符串名;
双重成本;
};
产品*库存水平=新产品[100];
ItemSelled*itemsSold=新售出的ItemSelled[100];
bool readInventory(字符串文件名、int和numberOfItems);
双重签出(int inventoryLength);
双倍价格(字符串PLU,双单位);
int类型检查(字符串PLU,int inventory长度);
字符串名称检查(字符串PLU,int inventoryLength);
int main()
{
int numberOfItems=0;
字符串filename=“products.txt”;
整数合计;
if(readInventory(文件名,numberOfItems))
{
cout单位;
itemsSold[计数器]。成本=价格(PLU,单位);
while(单位>库存水平[计数器]。库存)
{
cout单位;
}
库存水平[计数器]。库存-=单位;
}
计数器++;
}
}
温度=计数器;
而(温度>=0)
{
总计+=项目库存[temp]。成本;
温度--;
}
返回总数;
}
字符串名称检查(字符串PLU,int inventoryLength)
{
for(int k=0;k>临时PLU>>临时名称>>临时销售类型>>临时单价>>临时库存)
{
//检查是否有问题
if((0)时的温度加成>57)| |(1)时的温度加成>57)| |(2)时的温度加成>57)| |(3)时的温度加成>57))
{
错误++;
}
其他的
{ 
库存水平[numberOfItems].PLU=temp.PLU;
inventoryLevels[numberOfItems].name=temp.name;
库存水平[numberOfItems].salesType=temp.salesType;
库存水平[numberOfItems]。单价=临时单价;
库存水平[numberOfItems]。库存=临时库存;
numberOfItems++;
计数器++;
}
}
//如果没有错误,则返回true
如果(错误==0)
{
返回false;
}
//如果有错误,返回false
否则如果(错误>0)
{
返回true;
}
}

当您在此处赋值时

while (inventory >> temp.PLU >> temp.name >> temp.salesType >> temp.unitPrice >> temp.inventory)
我假设输入文件的格式正确吗(因为您将每一行分配给变量)

第1行:要分配给PLU的某些字符串
第2行:要分配给name的字符串
第3行:要分配给salestype的某些Int
…….
…….
第n行:字符串PLU


FWIW
typeCheck
price
缺少返回语句。我理解这一点,但在运行这些语句之前我就遇到了问题。我的意思是,当您查看readInventory并了解我如何将值从临时结构加载到全局数组中时。临时结构读取文件中的每一行,但它不会加载到数组中。显然,您在读取该文件时遇到了一些问题。为什么不利用调试器并在那里放置一些断点,以查看您实际读取到的临时结构中的内容?是的,这就是我第一次发现问题的原因。我想我应该更清楚。如果您看一看在readInventory函数中的else语句中,我对它为什么不起作用感到困惑。但是,当我尝试将temp中的值分配给InventoryLevel数组时,却没有发生任何变化。读取不是问题。为什么不使用向量而不是对任意数量的对象进行硬编码?将原始帖子编辑为dem对这一点进行处理,以便在结构的每个部分中,程序读入temp的空间会有所不同,这正好是我尝试将temp分配给InventoryLevel的时候[1]它不复制。等等。计数器移动得很好,但是数组中第一个位置的每个后续位置都被分配了我想要分配的第一行代码。是的,似乎问题出在“分配”中部分。起初我怀疑可能是索引计数器或读取输入给你带来了问题。看起来不是这样。是的,这就是我不明白的。索引是好的,但是当试图分配它时,它并没有做我想做的