为什么我在破坏程序时会遇到这个错误?C++; 我有一个C++类的程序,由于某些原因,当我到达程序的某个点时,它总是会中断。p>
这是我的文件的头文件为什么我在破坏程序时会遇到这个错误?C++; 我有一个C++类的程序,由于某些原因,当我到达程序的某个点时,它总是会中断。p>,c++,arrays,object,invalid-argument,C++,Arrays,Object,Invalid Argument,这是我的文件的头文件 // This class has overloaded constructors. #ifndef INVENTORYITEM_H #define INVENTORYITEM_H #include <string> using namespace std; class InventoryItem { private: string description; // The item description double cost; /
// This class has overloaded constructors.
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include <string>
using namespace std;
class InventoryItem
{
private:
string description; // The item description
double cost; // The item cost
int units; // Number of units on hand
int inventoryItemNumber; //Used to sort items from first entered to last
public:
// Constructor #1
InventoryItem()
{ // Initialize description, cost, and units.
description = "";
cost = 0.0;
units = 0; }
// Constructor #2
InventoryItem(string desc)
{ // Assign the value to description.
description = desc;
// Initialize cost and units.
cost = 0.0;
units = 0; }
// Constructor #3
InventoryItem(string desc, double c, int u)
{ // Assign values to description, cost, and units.
description = desc;
cost = c;
units = u; }
// Mutator functions
void setDescription(string d)
{ description = d; }
void setCost(double c)
{ cost = c; }
void setUnits(int u)
{ units = u; }
// Accessor functions
string getDescription() const
{ return description; }
double getCost() const
{ return cost; }
int getUnits() const
{ return units; }
};
#endif
一旦我在输入“pipeoping.txt”后按enter键,程序就会运行
看起来是这样的:
“命令:我
为输入输入输入文件名:Pimping.txt“
然后程序就中断了
我将代码部分的范围缩小到实际导致程序崩溃的原因,以及这部分代码中的某些内容:
for(int y = 0; y < count; y++)
{
int itemNumber = stoi(parsingArray2[0+(4*y)]);
string description = parsingArray2[1+(4*y)];
double costs = stof(parsingArray2[2+(4*y)]);
int unit = stoi(parsingArray2[3+(4*y)]);
item[itemNumber].setDescription(description);
item[itemNumber].setCost(costs);
item[itemNumber].setUnits(unit);
}
for(int y=0;y
请帮我弄清楚为什么我的程序老是出错。我想不出来。我试图从文件中读取字符串形式的输入,将字符串解析为单独的数据段。使用stoi或stof将字符串转换为整数或双精度,并将信息输入到对象数组中。感谢您的阅读和提供的任何帮助。您的代码中有两个问题涉及使用
for
循环,而最好使用while
。第一个问题涉及代码块:
for (int i = 0; inFile; i++) {
getline(inFile, parsingArray[i], '\n');
count++;//count will be needed for counting iterations of for loop for InventoryItem object data field completion
}
for (int j = jTracker; num < variable; j++) {//jTracker continues to grow through multiple outer "k" loops.
newDelimiter = parsingArray[k].find("|", oldDelimiter); //newDelimiter becomes the further pipe delimiter in the line.
parsingArray2[j] = parsingArray[k].substr(oldDelimiter, ((newDelimiter)-oldDelimiter));//the pipe delimited strings are isolated in input2[]
oldDelimiter = newDelimiter + 1;//oldDelimiter is set to the next pipe delimiter.
variable = parsingArray[k].length();
//The following alters variables as needed for the next loop
num = newDelimiter;
jTracker = (j + 1);
newLine = j;
}
在这里,count
即使在到达文件末尾后也会增加。因此,count
将比文件中的行数多一行。我们可以使用while循环:
while (getline(inFile, parsingArray[count], '\n'))
++count;
在这里,getline
将在读取最后一行时为infle
while
循环。当count
在此循环之前初始化为零时,
得到的count
将正确反映文件中的行数
第二个问题涉及代码块:
for (int i = 0; inFile; i++) {
getline(inFile, parsingArray[i], '\n');
count++;//count will be needed for counting iterations of for loop for InventoryItem object data field completion
}
for (int j = jTracker; num < variable; j++) {//jTracker continues to grow through multiple outer "k" loops.
newDelimiter = parsingArray[k].find("|", oldDelimiter); //newDelimiter becomes the further pipe delimiter in the line.
parsingArray2[j] = parsingArray[k].substr(oldDelimiter, ((newDelimiter)-oldDelimiter));//the pipe delimited strings are isolated in input2[]
oldDelimiter = newDelimiter + 1;//oldDelimiter is set to the next pipe delimiter.
variable = parsingArray[k].length();
//The following alters variables as needed for the next loop
num = newDelimiter;
jTracker = (j + 1);
newLine = j;
}
在这里,find
将返回std::string::npos
,如果没有更多的分隔符(即“|”
)。当循环的外部k
之前的
j初始化为零时,此而
循环将解析除最后一个标记外由分隔的所有标记。因此,在while
循环之后,我们使用std::string::npos
提取最后一个子字符串,以表示我们希望从oldDelimiter
直到字符串结束的所有字符。请注意,变量newLine
、num
、variable
和jTracker
都不是必需的
事实上,我们可以将这两个while
循环组合起来,得到:
std::string line; // don't need parsingArray any more
int j = 0; // initialize j before looping over file
while (std::getline(inFile, line, '\n')) {
int oldDelimiter = 0, newDelimiter = 0;
while ((newDelimiter = line.find("|", oldDelimiter)) != std::string::npos) {
parsingArray2[j] = line.substr(oldDelimiter, ((newDelimiter)-oldDelimiter)); //the pipe delimited strings are isolated in input2[]
oldDelimiter = newDelimiter + 1; //oldDelimiter is set to the next pipe delimiter.
++j; // increment j
}
// get the last token and increment j
parsingArray2[j] = line.substr(oldDelimiter, std::string::npos); //the pipe delimited strings are isolated in input2[]
++j;
// increment count
++count;
}
这样就不需要循环的外部k
。还请注意,不再需要数组parsingArray
,因为不再需要存储从一个循环中累积的中间结果,以便迭代并在后续循环中使用
需要考虑的补充说明:
对于std::string
和InventoryItem
使用std::vector
而不是固定大小的数组
“本地”声明变量
在随后设置项目的for
循环中,假设文件每行正好包含四个标记(并且这些标记正是该顺序中的项目编号(整数)、描述(字符串)、成本(实数)和单位(整数)。如果文件不满足这个要求(即,输入数据错误),以及程序应该如何处理所有错误情况,则应仔细考虑会发生什么。也许该循环中的代码可以合并到嵌套的while
循环中,以便在解析文件时检测并正确处理错误
那么,它到底是如何“断裂”的呢?您得到的确切错误是什么?您是否在调试器中单步执行了代码?您希望使用的变量中包含哪些值?您实际找到了什么?for(int j=jTracker;num
什么时候0会小于0?