【发布时间】:2014-03-19 21:28:00
【问题描述】:
大家好,我正在学校做一个项目,我需要用来自文本文档的信息填充一组指针:
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
【问题讨论】:
-
while (!filename)的目的应该是什么?这种情况永远不会成立。 -
我不知道该放什么我想通读整个文件。
-
使用
inputFile流从文件中读取(例如使用getline()方法,将字符串放入std::istringstream并解析记录的每个字段(使用>>运算符) .