gpt4 book ai didi

c++ - cstring 数组有问题

转载 作者:行者123 更新时间:2023-11-30 03:49:19 28 4
gpt4 key购买 nike

我在使用数组时遇到问题。我收到以下错误

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 = "\0";


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 typedef 已经使它成为一个数组,你不需要添加 []到参数声明。

关于c++ - cstring 数组有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32622940/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com