【问题标题】:Struggling to store data from file in an 2D int array努力将文件中的数据存储在 2D int 数组中
【发布时间】:2022-01-21 05:36:43
【问题描述】:

我正在做一个项目,我应该从 txt 文件中读取参数集,然后在命令提示符下打印多个圆圈。这是文件的格式:

10 10 14 15

50 20 5 15

这只是我为 Circle 类制作的构造函数,但是虽然它似乎可以正确读取文件,但我的嵌套 for 循环肯定有问题。我运行程序,打印它在循环期间从文件中读取的内容,然后是之后存储的内容,它似乎只保存了第一行的最后两个数字(数组似乎保持 14 14 14 14 15 15 15 15)。这只是我在 C++ 上工作的第二个学期,所以我不禁想知道我是否遗漏了一些明显的东西,但我可以在这里使用一些帮助来找出我的循环/数组有什么问题,以便我可以修复它然后继续打印我也在苦苦挣扎的圆圈代码,已经没有时间来解决我的问题了。

        Circle::Circle(string fileAdd)      //constructor function
    {
        int num;        //variable for holding the file data in loops
        cirCount = 0;       //making sure this variable doesn't have junk value
        cirStats = nullptr;     //making sure this variable doesn't have junk value
        string line;
        ifstream userFile;      //creating a filestream object
        userFile.open(fileAdd);    //opening the file
        if (userFile.is_open())     //confirming the file has properly been opened
        {
            while (getline(userFile, line))     //the number of circles is stored in a separate variable by this loop
            {
                if (line.length() != 0)     //this if statement checks whether the line is blank or not, and skips it if that's the case
                {
                    cirCount++;
                }
            }
            userFile.clear();
            userFile.seekg(0);
            cirStats = new int[STATS, cirCount];    //a dynamically allocated array is created in order to store the file data
            for (int i = 0; i < cirCount; i++)      //these nested loops store the data in a 2 dimensional array
            {
                for (int j = 0; j < STATS; j++)
                {
                    userFile >> num;
                    cirStats[j, i] = num;
                }
            }
            userFile.close();       //the file is closed to prevent issues down the line
            cout << cirCount << endl;
        }
        //if the file has not been properly loaded, then this code will run and exit the program
        else
        {
            cout << "User file failed to load. Please reset and try again.";
            exit(0);
        }
        for (int i = 0; i < cirCount; i++)      //these nested loops are just for testing what has been saved in the array
        {
            for (int j = 0; j < STATS; j++)
            {
                cout << cirStats[j, i] << endl;
            }
        }
    
    };

【问题讨论】:

  • 欢迎来到 Stack Overflow。请在minimal complete examples 上查看我们的页面。您发布的代码既不完整(因此我们无法在不填写大量空白的情况下运行它)也不是最小的(如果此代码可能与错误无关,并且可以被删除,则大多数情况下)。我猜我会说问题在于您正在使用 getline 和流输入运算符 &gt;&gt; 并且它们正在发生冲突。
  • 你可以使用std::vector吗?或者你还没有了解这一点?您在这里对二维数组有一个主要的误解。括号[]中必须只有一个值。逗号在这里不起作用。 . .

标签: c++ multidimensional-array constructor nested-loops


【解决方案1】:

解决方案在@Armin_Montigny 的评论中。类似的说明

cirStats[j, i] = num;

实际上应该这样写:

cirStats[j][i] = num;

也就是说,没有逗号运算符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-24
    • 1970-01-01
    • 2017-10-02
    • 2016-12-23
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多