使用循环将信息流传递给setter时出现运行时错误

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

所以,我一直在使用c ++在Visual Studio 2017中练习一些基本的文件操作,当我利用while或for循环将中间变量传递给setter函数时,我遇到了意外的运行时错误。首先我尝试了一个带有运行时错误的while循环,然后我尝试将它输入line for line,它运行得很好。好奇如果它只是while循环的问题,我在for循环中复制它并且具有相同的运行时错误。所以我想我的问题是,为什么我会收到这个错误?我知道乞丐不能选择,但我很想知道为什么如此详细的答案,好像向白痴解释一样,将非常感激。非常感谢您抽出时间提前回答我的问题。

以下是我的整个源文件。我觉得有必要详细说明。以下是在美国收集13个最受欢迎的全名并使用所述信息填充一系列类的文件的初步尝试。我没有包含类头和.cpp,因为它非常基本。只有三个私有变量“firstName”,“lastName”,“numPeople”。然后有各种预期的getter和setter用于所述变量。我不认为类本身是一个问题(由于无知而导致执行不佳),因为当我没有使用循环并调用类方法时,它表现得很好。此外,如果你注意到,有一个while循环和一个for循环,已被注释掉。我认为每个人都必须看到我如何尝试实现我的循环,以防出现明显的逻辑错误。再次感谢!

#include <fstream>
#include "listNames.h"

int main() 
{
    listNames fullNames[13];
    std::string first;
    std::string  last;
    int num = 0;
    int i = 0;

    std::ifstream nameFile("fullNames.txt");

    std::cout << "FullName\tNumber of people\n";

    nameFile >> first;
    nameFile >> last;
    nameFile >> num;
    fullNames[i].set_firstName(first);
    fullNames[i].set_lastName(last);
    fullNames[i].set_numPeople(num);
    std::cout << fullNames[i].get_firstName() << " " << 
fullNames[i].get_lastName() << "\t" << fullNames[i].get_numPeople() << 
std::endl;
    i++;
    nameFile >> first;
    nameFile >> last;
    nameFile >> num;
    fullNames[i].set_firstName(first);
    fullNames[i].set_lastName(last);
    fullNames[i].set_numPeople(num);
    std::cout << fullNames[i].get_firstName() << " " << 
fullNames[i].get_lastName() << "\t" << fullNames[i].get_numPeople() << 
std::endl;

    /*for (i = 0; i < 13; i++) 
    {
        nameFile >> first;

        nameFile >> last;

        nameFile >> num;

        fullNames[i].set_firstName(first);

        fullNames[i].set_lastName(last);

        fullNames[i].set_numPeople(num);

        i++;

        std::cout << fullNames[i].get_firstName() << " " << 
        fullNames[i].get_lastName() << "\t" << fullNames[i].get_numPeople() 
       << std::endl;
    }*/

    /*while (nameFile >> first >> last >> num) 
    {
        fullNames[i].set_firstName(first);

        fullNames[i].set_lastName(last);

        fullNames[i].set_numPeople(num);

        i++;

        std::cout << fullNames[i].get_firstName() << " " << 
        fullNames[i].get_lastName() << "\t" << fullNames[i].get_numPeople() 
        << std::endl;

        if (i == 13) 
        {
            break;
        }
    }*/

    return 0;
}
c++ arrays visual-studio oop runtime-error
1个回答
1
投票

The problem in the for loop:

你在循环的每次运行中递增i两次。删除循环内的i++线。

它应该是:

for (i = 0; i < 13; i++) 
{
    // Break of out the loop if there is an error in reading.
    if ( !(nameFile >> first >> last >> num) )
    {
       break;
    }

    fullNames[i].set_firstName(first);

    fullNames[i].set_lastName(last);

    fullNames[i].set_numPeople(num);

    // This is the problem.
    // i++;

    std::cout << fullNames[i].get_firstName() << " " << 
    fullNames[i].get_lastName() << "\t" << fullNames[i].get_numPeople() 
   << std::endl;
}

The problem in the while loop:

在打印内容时访问数组之前,首先递增i。当i等于12时,最终使用out of bounds索引访问数组,这会导致未定义的行为。

它应该是:

while ( nameFile >> first >> last >> num ) 
{
    fullNames[i].set_firstName(first);

    fullNames[i].set_lastName(last);

    fullNames[i].set_numPeople(num);

    std::cout << fullNames[i].get_firstName() << " " << 
    fullNames[i].get_lastName() << "\t" << fullNames[i].get_numPeople() 
    << std::endl;

    // Increment it after printing.
    i++;

    if (i == 13) 
    {
        break;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.