使用cstring数组有问题
Having problems with cstring arrays
我有麻烦与我的代码与数组的东西。我得到以下错误
In function ‘int main(int, const char**)’:
75: error: cannot convert ‘char*’ to ‘char (*)[81]’ for argument ‘1’ ion(char (*)[81], OneItem*, int&, int&)’
In function ‘void parseInformation(char (*)[81], OneItem*, int&, in
164: error: ISO C++ forbids comparison between pointer and integer
166: error: incompatible types in assignment of ‘const char [2]’ to
169: error: ISO C++ forbids comparison between pointer and integer
174: error: ISO C++ forbids comparison between pointer and integer
174: error: ISO C++ forbids comparison between pointer and integer
176: error: invalid conversion from ‘char*’ to ‘char’
代码与行号不对齐。我已经尝试了很多东西谷歌一些东西仍然没有找到解决方案。
const int MAX_CHARACTERS = 80;
const int MAX_INVENTORY = 12;
typedef char OneLine[MAX_CHARACTERS + 1];
struct OneItem
{
char product[MAX_CHARACTERS + 1];
int quantity;
float unitPrice;
float totalPrice;
};
int main( const int argc, const char* argv[] )
{
OneLine fileName;
ifstream inFile;
OneLine readLine;
OneItem inventory[MAX_INVENTORY];
int readLineIndex;
int structureCounter = 0;
int averageQuantity;
float averagePrice;
float averageTotalPrice;
displayIntroduction();
getFileName( argc, argv, fileName );
if (!inFile)
{
cout << "File not found: " << fileName << endl;
}
else
{
inFile.open(fileName);
while(!inFile.getline(readLine, MAX_CHARACTERS, 'n').eof())
{
if (structureCounter < MAX_INVENTORY)
{
parseInformation(readLine,inventory, readLineIndex, structureCounter);
}
}
void parseInformation(OneLine readLine[],OneItem inventory[], int & readLineIndex, int & structureCounter)
{
int tempIndex = 0;
int valueCounter = 0;
OneLine tempArray;
while(readLine[readLineIndex] != 'n')
{
tempArray = " ";
while(readLine[readLineIndex] == ' ')
{
readLineIndex += 1;
}
while(readLine[readLineIndex] != ' ' && readLine[readLineIndex] != 'n')
{
tempArray[tempIndex] = readLine[readLineIndex];
tempIndex += 1;
readLineIndex += 1;
}
if(valueCounter == 0)
{
for(int i = 0; i <= strlen(tempArray); i++)
{
inventory[structureCounter].product[i] = tempArray[i];
}
valueCounter += 1;
}
else if(valueCounter == 1)
{
inventory[structureCounter].quantity = atoi(tempArray);
valueCounter += 1;
}
else
{
inventory[structureCounter].unitPrice = atof(tempArray);
structureCounter += 1;
}
}
return;
你对parseInformation
的定义是错误的。它说readLine
应该是OneLine
的一个数组,但它只需要一个OneLine
。应该是:
void parseInformation(OneLine readLine,OneItem inventory[], int & readLineIndex, int & structureCounter)
这会导致你得到的所有错误,因为你用单个OneLine
调用函数,而不是一个数组。在函数内部,你将readLine[readLineIndex]
与一个字符进行比较,这要求readLine
是一个字符数组,而不是OneLine
的数组。
OneLine
类型定义已经使它成为一个数组,您不需要在参数声明中添加[]
。
正如Barmar所说,您需要更改第一个参数,但这里还有一个问题:
OneLine tempArray;
while(readLine[readLineIndex] != 'n')
{
tempArray = " ";
你不能做这样的分配。tempArray
需要是const char*
的类型,或者你需要这样做:
tempArray[some_index] = ' ';
相关文章:
- 将 std::unique_ptr 重置为指向数组的指针有什么问题?
- 将任意对象附加到数组有什么问题
- 我们在 c++17 中有自动数组吗?
- 我的C++程序有问题.涉及动态调整整数数组的大小
- C++有问题的函数,数组到列表
- 我的算法在 1D 数组中渲染矩形有什么问题
- 问题是关于C 的数组,以便在编程主题中有更多的了解
- 我的代码中的数组有问题.任何建议
- 我的代码正在制作 2D 数组元素的重复副本.代码中有什么问题?
- 在C++中使用有界数组的正确方法是什么?
- 我的动态数组有问题
- 如何将双精度数组从 VB6 传递到 VC++.这段代码有什么问题
- 我的字节数组到十六进制字符串转换器有什么问题,或者为什么它在写入文件时会在某个位置后剪切符号?
- 我在合并排序中传递此数组的方式有什么问题
- 声明类类型的数组(Borland C++有问题)
- 我的指向结构的指针数组有什么问题?
- 在 c++ 中,这段代码涉及对象数组的排序有什么问题?
- 这个字符数组代码有什么问题?
- 我的数组代码有什么问题
- C/ c++中有复制数组的函数吗?