使用cstring数组有问题

Having problems with cstring arrays

本文关键字:有问题 数组 cstring 使用      更新时间:2023-10-16

我有麻烦与我的代码与数组的东西。我得到以下错误

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] = '';