【问题标题】:code only reads first line of text from source file代码只从源文件中读取第一行文本
【发布时间】:2017-07-17 05:05:46
【问题描述】:

我很确定我的代码是可靠的,应该从我保存的记事本中获取所有内容,但似乎并非如此。我认为也可能是它可能会将所有内容都放入我的数组中但无法正确打印。谁能指出我的错误在哪里?我不知所措。

这是我的 .txt 文件中的文本:

杰森 10 15 20 25 18 20 26
萨曼莎 15 18 29 16 26 20 23
拉维 20 26 18 29 10 12 20
希拉 17 20 15 26 18 25 12
Ankit 16 8 28 20 11 25 21

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void getData(ifstream& inf, string n[], double runData[][8], int count);
void calculateAverage(double runData[][8], int count);
void print(string n[], double runData[][8], int count);

int main()
{
    string names[5];
    double runData[6][8];

    ifstream inFile;

    inFile.open("Lab15Runner.txt");

    if (!inFile)
    {
        cout << "Cannot open the input file: Lab15Runner.txt." << endl;
        cout << "Program terminates!" << endl;
        return 1;
    }

    cout << fixed << showpoint << setprecision(2);

    getData(inFile, names, runData, 5);
    calculateAverage(runData, 5);
    print(names, runData, 5);

    inFile.close();

    return 0;
}

void getData(ifstream& inf, string n[], double runData[][8], int count)
{
    for (int i = 0; i < count; i++)
    {
        inf >> n[i];

        for (int j = 0; j < 8; j++)
            inf >> runData[i][j];

        runData[i][8] = 0.0;

    }
}

void calculateAverage(double runData[][8], int count)
{
    double sum;

    for (int i = 0; i < count; i++)
    {
        sum = 0.0;
        for (int j = 0; j < 8; j++)
            sum = sum + runData[i][j];
        runData[i][8] = sum / 7;
    }
}

void print(string n[], double runData[][8], int count)
{
    double sum = 0.0;

    cout << left << setw(10) << "Name"
        << right << setw(8) << "Day 1"
        << setw(8) << "Day 2"
        << setw(8) << "Day 3"
        << setw(8) << "Day 4"
        << setw(8) << "Day 5"
        << setw(8) << "Day 6"
        << setw(8) << "Day 7"
        << setw(10) << "Average" << endl;

    for (int i = 0; i < count; i++)
    {
        cout << left << setw(10) << n[i];
        cout << right;

        for (int j = 0; j < 9; j++)
            cout << setw(8) << runData[i][j];
        cout << setw(8) << runData[i][9];

    }

}

【问题讨论】:

  • runData[i][8] = 0.0; 写入越界,读取第 8 个数字(当 j == 7 时)将失败,因为 Samantha 不是数字,它设置 failbit 并导致所有未来读取也失败了。
  • 看起来您的索引超出范围,我很惊讶您没有收到运行时错误。标注为[6][8],runData中的范围不应该是[0-5][0-7]吗?
  • @Fhaab 你为什么感到惊讶?未定义的行为是未定义的。无法保证它会崩溃。
  • @BenVoigt 它是如何越界的?我以为我的矩阵有 8 列?

标签: c++ arrays multidimensional-array


【解决方案1】:

您有 7 个 int 值,但您尝试读取 8 [0-7]。当数组中的最后一个位置是 [7] 时,您还尝试在位置 [8] 中写入。

n[i]         runData[i][j] --> [0]  [1]  [2]  [3]  [4]  [5]  [6] {Length = 7}  
  0   Jason                     10   15   20   25  18   20   26 
  1   Samantha                  15   18   29   16  26   20   23 
  2   Ravi                      20   26   18   29  10   12   20
  3   Sheila                    17   20   15   26  18   25   12 
  4   Ankit                     16    8   28   20  11   25   21

我更改了您的代码并在更改前注释了 //CHANGED

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void getData(ifstream& inf, string n[], double runData[][8], int count);
void calculateAverage(double runData[][8], int count);
void print(string n[], double runData[][8], int count);

int main()
{
    string names[5];
    double runData[6][8];

    ifstream inFile;

    inFile.open("Lab15Runner.txt");

    if (!inFile)
    {
        cout << "Cannot open the input file: Lab15Runner.txt." << endl;
        cout << "Program terminates!" << endl;
        return 1;
    }

    cout << fixed << showpoint << setprecision(2);

    getData(inFile, names, runData, 5);
    calculateAverage(runData, 5);
    print(names, runData, 5);

    inFile.close();

    return 0;
}

void getData(ifstream& inf, string n[], double runData[][8], int count)
{
    for (int i = 0; i < count; i++)
    {
        inf >> n[i];

        //CHANGED - reads all 7 values after the name
        for (int j = 0; j < 7; j++) // Old --> for (int j = 0; j < 8; j++)
            inf >> runData[i][j];

        //CHANGED - set the last value (8th position) to 0.0
        runData[i][7] = 0.0; //Old --> runData[i][8] = 0.0;

    }
}

void calculateAverage(double runData[][8], int count)
{
    double sum;

    for (int i = 0; i < count; i++)
    {
        sum = 0.0;
        //CHANGED - sum all 7 values
        for (int j = 0; j < 7; j++) //Old --> for (int j = 0; j < 7; j++)
            sum = sum + runData[i][j];

        //CHANGED - update the 8th position
        runData[i][7] = sum / 7; // Old --> runData[i][8] = sum / 7;
    }
}

void print(string n[], double runData[][8], int count)
{
    double sum = 0.0;

    cout << left << setw(10) << "Name"
        << right << setw(8) << "Day 1"
        << setw(8) << "Day 2"
        << setw(8) << "Day 3"
        << setw(8) << "Day 4"
        << setw(8) << "Day 5"
        << setw(8) << "Day 6"
        << setw(8) << "Day 7"
        << setw(10) << "Average" << endl;

    for (int i = 0; i < count; i++)
    {
        cout << left << setw(10) << n[i];
        cout << right;

        //CHANGED - Print all 7 values
        for (int j = 0; j < 7; j++)// Old --> for (int j = 0; j < 9; j++)
            cout << setw(8) << runData[i][j];

        //CHANGED - print the average (average is at position [i][7], position [i][9] doesn't exist) and added endl at the end
        cout << setw(8) << runData[i][7] << endl;// Old --> cout << setw(8) << runData[i][9];

    }

}

它应该像这样工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多