将文本文件中的整数读取到二维数组中并评估长度

问题描述 投票:0回答:1

我需要将一个文本文件中的整数读取到二维数组中。

-1表示数组的结尾。每个数组都假定有6个正整数(-1不算作数组的一部分)。

例如,如果我的文本文件包含以下整数:

1 3 4 6 1 7 -1 1 3 5 7 2 3 -1 2 5 7 2 6 3 -1

这意味着当将这些整数读入程序时,将有3个数组:

第一数组:1 3 4 6 1 7

第二数组:1 3 5 7 2 3

第三个数组:2 5 7 2 6 3

我编写了一个程序来评估每个数组是否具有正确的长度(6个整数)和正确的整数(所有整数都应为正),如果不满足这些条件,则输出相应的错误消息。

#include <iostream>
#include  <fstream>
#include <string>
#define MAX_ROWS  3
#define MAX_COLUMNS 2

using namespace std;

int main()
{
    string fileName = "testdata.txt";                                                           //declare a string to store the Input File's name 
    ifstream inFile;                                                                             //name the input file connection 
    inFile.open(fileName);                                                                       //open the Input File
    string errorMessage = "Input file cannot be found";                                           //Declare a string to store an error message, just in case the Input file doesn't exist or cannot be found

    if (!inFile) {                                                                               //If the Input File doesn't not exist, then display the error message
        cout << errorMessage << '\n';                                                           //also store the error message to the Output file
        system("pause");
        return  0;                                                                               //end the program if the Input File does not exist
    }


    int checkNbr;
    int ArrB[MAX_ROWS][MAX_COLUMNS];
    int size = MAX_ROWS * MAX_COLUMNS;
    bool bad = false;
    bool invalidnum = false;

while (!inFile.eof())      
    {
        for (int i = 0; i < MAX_ROWS;i++) {
            for (int j = 0; j < MAX_COLUMNS; j++) {
                inFile >> ArrB[i][j];
                if (ArrB[i][j] == -1) {
                    bad = true;
                    cout << "\nThe array does not have enough integers" << endl;
                    break;
                    //return 1;
                }
                else {
                    if (ArrB[i][j] < 1) {
                        invalidnum = true;
                    }
                }
                cout << *(*(ArrB + i) + j) << " ";

            }
        }

        if (invalidnum == true) {
            invalidnum = false;
            cout << "\nThere is/are negative number(s) or zero(s) in the array imported from your text file.\n";
        }

        if (bad == false) {
            inFile >> checkNbr;
            if (checkNbr == -1) {
                cout << "\nThe size of the array is correct." << endl;
            }
            else {

                while (checkNbr != -1)
                {
                    cout << checkNbr;
                    cout << " ";
                    inFile >> checkNbr;

                }

                cout << "\nYou have too many numbers in this array\n";
            }
        }
    }

    return 0;
}

如果我运行程序:

案例1-通过

1 2 3 4 5 6 -1 3 5 2 1 6 8 3 2 5 -1 3 3 5 6 7 5 -1

enter image description here

案例2 –通过

1 2 3 4 5 -6 -1 3 -5 2 1 6 8 -1 3 5 6 7 5 -1

enter image description here

案例3 –失败(!)

1 2 3 4 -1 1 2 3 4 5 6 7 8 -1 1 2 3 4 5 -1

enter image description here

如您所见,案例3失败了。第二个数组(1 2 3 4 5 6 7 8)实际上比声明的数组大小长,但是它会打印出“该数组没有足够的整数”错误消息…..

此程序唯一无法使用的时间是在第一个数组没有足够的整数时。

任何评论或提示将不胜感激!

c++ if-statement while-loop
1个回答
1
投票

问题

[bad处理后未重置。

解决方案

bool bad = false;移动到while (!inFile.eof())循环主体中,以便在每次迭代时将其重置。如果您在尽可能小的范围内定义变量,通常可以避免此问题,因此您应该强烈考虑对其余变量执行相同的操作。

这将解决所询问的错误以及至少一个尚未发现的错误。这留下了至少两个其他尚待解决的错误,问题的注释中都包含了这些错误。

TL; DR版本

快速浏览输入1 2 3 4 -1 1 2 3 4 5 6 7 8 -1 1 2 3 4 5 -1

1 2 3 4 -1被解析并发现为bad,因此bad设置为true,并且跳过了对太长的值和清理的测试。该文件尚未结束,因此程序循环并开始读取下一个数组。

剩余输入:

1 2 3 4 5 6 7 8 -1 1 2 3 4 5 -1

到目前为止的输出:

1 2 3 4数组没有足够的整数

1 2 3 4 5 6被解析。这将填充数组,并且for循环退出。 bad仍为true,因此跳过了溢出检查,并且未打印任何消息

剩余输入:

7 8 -1 1 2 3 4 5 -1

到目前为止的输出:

1 2 3 4数组没有足够的整数1 2 3 4 5 6

现在7 8 -1已解析。这太短了,无法作为数组进行报告。请注意这对输出的作用。

剩余输入:

1 2 3 4 5 -1

到目前为止的输出:

1 2 3 4数组没有足够的整数1 2 3 4 5 6 7 8数组没有足够的整数

哇。看起来1 2 3 4 5 6 7 8不仅仅是7 8太短。

程序然后解析1 2 3 4 5,发现它太短。

剩余输入:

到目前为止的输出:

1 2 3 4数组没有足够的整数1 2 3 4 5 6 7 8数组没有足够的整数1 2 3 4 5数组没有足够的整数
© www.soinside.com 2019 - 2024. All rights reserved.