从文件填充结构

Filling structure from a file

本文关键字:结构 填充 文件      更新时间:2023-10-16

嘿,我在学校做一个项目,我需要从文本文档中填充一个指针数组:

4101 BRAEBURN_REG 1 0.99 101.5
4021 DELICIOUS_GDN_REG 1 0.89 94.2
4020 DELICIOUS_GLDN_LG 1 1.09 84.2
4015 DELICIOUS_RED_REG 1 1.19 75.3
4016 DELICIOUS_RED_LG 1 1.29 45.6
4167 DELICIOUS_RED_SM 1 0.89 35.4
4124 EMPIRE 1 1.14 145.2
4129 FUJI_REG 1 1.05 154.5
4131 FUJI_X-LGE 1 1.25 164.1
4135 GALA_LGE 1 1.35 187.7
4133 GALA_REG 1 1.45 145.2
4139 GRANNY_SMITH_REG 1 1.39 198.2
4017 GRANNY_SMITH_LGE 1 1.49 176.5
3115 PEACHES 1 2.09 145.5
4011 BANANAS 1 0.49 123.2
4383 MINNEOLAS 1 0.79 187.3
3144 TANGERINES 1 1.19 135.5
4028 STRAWBERRIES_PINT 0 0.99 104
4252 STRAWBERRIES_HALF_CASE 0 3.99 53
4249 STRAWBERRIES_FULL_CASE 0 7.49 67
94011 ORGANIC_BANANAS 1 0.99 56.3

所以我必须把它放入一个结构体中。下面是我的函数:

bool readInventory(string filename)
{
   inputFile.open(filename);   //opening products file
   bool errors = true;
   if(!inputFile.fail()) // validate that the file did open
   {
   while (!filename)      // Dynamically creates array and fills with info 
      { 
          product *inventory = new product();
          inputFile >> inventory->plu;
          inputFile >> inventory->itemName;
          inputFile >> inventory->saleType;
          inputFile >> inventory->price;
          inputFile >> inventory->currentInventory;
          itemArray[counter] = inventory;
          counter++;
        cout << itemArray[14]<< endl;
      }
   }
   else
   {
      cout << "nError, unable to open products.txt.n";
      errors = false;
      return errors;
   }
}// ends readInventory

它不会填充数组,但是如果我执行while (filename) // Dynamically creates array and fills with info,它将只将第一项记录到数组中,而将其他项留空。我还需要验证输入。(即PLU代码不是整数,但PLU类型是字符串)并跳过有问题的产品并将bool errors从真更改为假。下面是我的全部代码:

#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
using namespace std; 
bool readInventory(string filename);
double checkout();
bool updateInventory(string filename);
// prototypes
int counter = 0; //used to fill the array
ifstream inputFile;            //file to read from
const int SIZES = 100; //constant int used to tell sizes
product *itemArray[SIZES]; //array of pointers of size 100
struct product
      {
         string plu;             //price look up code for each product
         string itemName;        //name of item
         int saleType;           //item is per pound or per unit
         int price;              //price of item 
         int currentInventory;   //amount in stock
      };

int main ()
{     
   int choice;           // choice is used to find out what choice they want in the menu.
   bool menuOn = true;   //menuOn is used to turn on and off the menu.
   readInventory("Products.txt");
   while (menuOn == true)
   {
      cout << "1 - Check out.n";
      cout << "2 - Close the store and exit.n";
      cout << "Enter your choice and press return: ";
      //Displays choices for the menu
      cin >> choice;  //Chose what menu option you want
      cout << endl;
      switch (choice)
      {
         case 1:
         checkout();
         break;
         case 2:
         cout << "Thank you for using this item check out program.";
         menuOn = false;
         break;
         default:
         cout << "Not a Valid Choice. n";
         cout << "Choose again.nn";
         break;
         // Safty net if user doent enter choice 1-2
     }
  }
   inputFile.close();
   cout << endl; 
   system("pause"); 
   return 0; 
} // end function main () 
bool readInventory(string filename)
{
   inputFile.open(filename);   //opening products file
   bool errors = true;
   if(!inputFile.fail()) // validate that the file did open
   {
   while (counter < 22)   // Dynamically creates array and fills with info 
      { 
          product *inventory = new product();
          inputFile >> inventory->plu;
          inputFile >> inventory->itemName;
          inputFile >> inventory->saleType;
          inputFile >> inventory->price;
          inputFile >> inventory->currentInventory;
          itemArray[counter] = inventory;
          counter++;
      }
   }
   else
   {
      cout << "nError, unable to open products.txt.n";
      errors = false;
      return errors;
   }
}// ends readInventory
double checkout()
{
   double total = 0; //total cost
   string pluCode; // code to look for
   bool found = false; // notify if item is found
   double costs;
   double quantity;
   bool codeValid = true;
   do
   {    
       cout << "Please enter PLU code or 0 to exit: ";
       codeValid = true;
       cin >> pluCode;
    /*   for (int x = 0; x <= pluCode.length(); x++)
        {
           if ((pluCode[x] != '1') && (pluCode[x] != '2') && (pluCode[x] != '3') && (pluCode[x] != '4') && (pluCode[x] != '5') && (pluCode[x] != '6') && (pluCode[x] != '7') && (pluCode[x] != '8') && (pluCode[x] != '9') && (pluCode[x] != '0'))
           {
                codeValid = false;
           }
        }   */

      for (int itemFind = 0 ; itemFind < counter; itemFind++)
      {
          if (itemArray[itemFind]->plu == pluCode)
          {
              found = true;
              costs = itemArray[itemFind]->price;
              cout << "Please enter lbs or quantity: ";
              cin >> quantity;
              if (costs); // Data Validation
              costs = quantity*costs;
              total += costs;
          };
      };
      if(!found)
      {
          cout << "Please enter a valid PLU code.n";
      }
   } while (pluCode != "0");
   if (total > 50.00)
      total = (.95*total);
   return total;
   return 0.00;
}//ends Checkout
bool updateInventory(string filename)
{
   return true;
}//ends updateInventory

这里是一个链接到实际的分配,如果有人需要:https://www.dropbox.com/s/howsf5af2gsa1i8/CS1Asg4BStructures.docx

使用以下代码读取文件中的所有行:

std::string line;
while (std::getline(inputFile,line))
{   
    std::istringstream iss(line);
    product *inventory = new product();
    iss >> inventory->plu;
    iss >> inventory->itemName;
    iss >> inventory->saleType;
    iss >> inventory->price;
    iss >> inventory->currentInventory;
    itemArray[counter] = inventory;
    ++counter;
}

还尝试摆脱全局变量定义(例如counter, inputFile),并提供它们作为函数局部变量或参数(或者在counter的情况下,甚至可以合理地将其用作返回值:返回0-1错误情况,以及读取的记录数)。此外,在分配新记录并将其分配给itemArray[counter]之前,您需要检查counter是否小于SIZES

我建议至少使用std::vector来管理记录:

std::vector<product> itemArray;
// ... 
std::string line;
while (std::getline(inputFile,line))
{   
    std::istringstream iss(line);
    product newProd;
    iss >> newProd.plu;
    iss >> newProd.itemName;
    iss >> newProd.saleType;
    iss >> newProd.price;
    iss >> newProd.currentInventory;
    itemArray.push_back(newProd);
}

此代码不需要counter, itemArray.size()将告诉从文件中读取的记录数

我怀疑你是指

while (inputFile) 
不是

while (!filename) 

!filename没有意义,您正在否定字符串。如果你知道有多少物品,你可以设置一个条件,如while (counter < (howManyItems))。如果没有,可以先循环查找行结束符,然后将while改为for。