关于结构和使用它们与数组

时间:2015-03-18 01:05:50

标签: c++ arrays struct

我正在尝试编写一个程序,它将从文本文件中获取每一行并将值加载到数组中。然而,出于某种原因,当我尝试创建一个动态数组并尝试将信息放在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;
   }

}

1 个答案:

答案 0 :(得分:0)

在此处指定值时,

while (inventory >> temp.PLU >> temp.name >> temp.salesType >> temp.unitPrice >> temp.inventory)

我是否正确地假设输入文件采用的格式(因为您要将每一行分配给变量?

  

第1行:您想要分配给PLU的一些字符串
  第2行:您想要分配给名称的一些字符串   第3行:您想要分配给salestype的一些Int   ..........
  ..........
  line n:string PLU