从文件填充结构

时间:2014-03-19 21:28:01

标签: c++ file-io structure

嘿伙计们,我正在学校做一个项目,我需要用文本文档中的信息填充一系列指针:

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从true更改为false。这是我的全部代码:

#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.\n\n";
         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

3 个答案:

答案 0 :(得分:1)

要从文件中读取所有行,请使用以下代码:

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

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

我建议至少使用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()将告知从文件中读取的记录数。

答案 1 :(得分:0)

我怀疑你的意思

while (inputFile) 

而不是

while (!filename) 

答案 2 :(得分:-1)

!filename毫无意义,你在否定一个字符串。如果你知道有多少项,你可以创造while (counter < (howManyItems))之类的条件。如果你没有,你可以先通过循环查找行尾字符,然后将while更改为for。