关于结构和将它们与数组一起使用
Regarding Structs and Using Them With Array's
我正在尝试编写一个程序,该程序将从文本文件中提取每一行,并将值加载到数组中。然而,出于某种原因,当我尝试创建一个动态数组并尝试将信息放在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 boughtn";
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 boughtn";
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;
}
}
在此处赋值时,
while (inventory >> temp.PLU >> temp.name >> temp.salesType >> temp.unitPrice >> temp.inventory)
我假设输入文件的格式是正确的吗(因为你把每一行都分配给变量?
第1行:要分配给PLU的某个字符串
第2行:要分配给名称的某个字符串
第3行:您想要分配给salestype的某些Int
…………..
…………..
第n行:字符串PLU
相关文章:
- 为什么我不能将 rand() 与数组的大小一起使用?
- 如何将 std::find() 与 2d std: 数组一起使用?
- 程序在初始化期间未与数组一起运行
- 将 find_if() 与数组一起使用的代码段中出错
- 是否可以将isdigit,isalpha函数与输入的字符串数组一起使用
- 将MPI_Scatter与 char* 数组一起使用
- 在模板参数中使用 auto:一些使用示例和..如何使其与恒定大小的 C 数组一起使用
- 将模板气泡排序与结构数组一起使用时出现问题
- 将'sort'与字符数组一起使用 (C++)
- Strncpy只能与固定长度的数组一起使用
- 关于结构和将它们与数组一起使用
- 如何将while循环与数组一起使用
- 将"getline"与数组一起使用
- 将输入流迭代器与数组一起使用
- 将指针与动态数组一起使用时出现问题
- 如何将 printf 与 2D 数组一起使用
- 函数更改后指针的显示值与数组 C 一起改变
- 如何将 boost::crc_optimal 与二进制数组(0 和 1 的数组)一起使用
- 是否可以将两个多维数组一起添加到第三个多维数组中
- 如何声明一个将string和int数组一起排序的函数?