【问题标题】:c++ for loop with decrement update causing infinite loop?c ++ for循环减量更新导致无限循环?
【发布时间】:2014-07-29 06:45:44
【问题描述】:

这是一个更大的代码的一部分,用于逐字读取输入文件,然后以相反的顺序打印单词。它使用一个名为 words[] 的字符串数组来逐字存储程序前面输入文件中的 char 字符串:

//print to screen
for (int i = MAXSIZE; i >= 0; i--)
    {
        cout << words[i] << " ";
    }

测试输入文件内容:

This is my test file. I hope this works. 

输出只是“工作”。不断重复。 为什么 i-- 显然从未发生过?

编辑:我的代码中的所有内容。至少可以说,我在这里有点时间紧张。 MAXSIZE=1024 部分实验室提示。不能使用向量或反向;看到这一切,但它是这个实验室的禁区。编程新手,所以如果你能避免居高临下,那就太好了。只是想让这个工作。读取 input.txt 和打印到屏幕位工作正常。输出部分完全失败,我不知道为什么。谁能告诉我为什么而不是侮辱我,谢谢?

//Kristen Korz
//CIS 22A
//This program reads an input file and writes the words in reverse order to an output file.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    //create and link input...
    ifstream inputFile;
    inputFile.open("input.txt");
    //...and output files
    ofstream outputFile;
    outputFile.open("output.txt");

    //error message for file open fail
    if (inputFile.fail())
        cout << "Error opening the file.\n";

    //constant for max size
    const int MAXSIZE = 1024;
    //string array and temporary-use string
    string words[MAXSIZE];
    string str;                 //note: variables will be used for output loops too

    //read words from input file
    for (int i = 0; (inputFile >> str) && (i < MAXSIZE); ++i)
    {
        words[i] = str;
        //for showing in terminal if read correctly
        cout << words[i] << " ";
    }
    inputFile.close();
    cout << endl;

    //something wrong with for loop resulting in i apparently not updating
    for (int i = MAXSIZE; (outputFile << str) && (i >= 0); --i)
    {
        words[i] = str;
        //for showing in terminal if written correctly
        cout << words[i] << " ";
    }
    outputFile.close();
    cout << endl;

    system("pause");
    return 0;
}

对于带有 i 的输出,我在 for 循环中的 cout 语句说:

cout << words[i] << " " << i << " ";

给终端输出: 这 0 是 1 我的 2 测试 3 文件。 4 我 5 希望 6 这 7 有效。 8 作品。 1023 件作品。 1022 件作品。 1021(大量重复的作品。后面跟着递减的数字)作品。 3作品。 2作品。 1作品。 0

【问题讨论】:

  • 什么是wordswords 是如何声明的?以及如何将“输入文件”放入words
  • 根据@WhozCraig 链接的线程,您似乎正在用输入的最后一个单词“works”填充整个words 数组。
  • @user657267 正是该循环的作用。

标签: c++ for-loop infinite-loop decrement


【解决方案1】:

您的输出循环会:

words[i] = str;

每次迭代。 str 仍然保存您输入的最后一个字符串的值,因此这会将 words 的每个成员设置为相同的字符串。由于您最后输入的字符串是"works",这就解释了为什么您每次都输出"works"

如果您只是删除该行,它应该会更好。另外,从MAXSIZE - 1 开始。数组的有效索引是0MAXSIZE-1。您的越界访问会导致未定义的行为,尽管在这种情况下显然没有任何效果。

但是,如果您的输入只有您建议的 8 个单词,那么输出 1024 个单词会给您带来很多空白。考虑从i 的位置开始输出,而不是MAXSIZE - 1

【讨论】:

  • 而且一直在我的眼球面前有一个明显的答案!我知道一旦我看到问题所在,我会因为没有得到它而觉得自己像个白痴。感谢您将我的困惑和沮丧带到了我本应该能够达到的终点。
  • 公平地说,如果您仔细阅读,David Young 的回答实际上与我的回答完全相同:)(我一开始也没有仔细阅读,否则我不会发布)
【解决方案2】:

在标记为不工作的部分(第二个for 循环),正在读取str,但它从未更改为该循环中的任何其他内容,因此它重复最后一个单词。 i正在更新,问题是str没有更新。

另一个问题是您试图访问数组末尾之后的元素,正如 WhozCraig 和 Velthune 在他们的回答中所讨论的那样。您需要在第二个 for 循环中正确弄清楚您想用 words 做什么。这是关键。此外,您需要存储您读取的数组的结束位置。

【讨论】:

  • 好的,我可以看到这一点,我觉得我现在应该有足够的能力将它们放在一起,但我仍然不明白我需要做些什么来修复它。
  • @KristenKorz 考虑一下:当第二个for 循环运行时,words 数组中包含什么?应该如何处理该数组中的信息?
  • @KristenKorz 我添加了一些提示。
  • 第一个 for 循环执行的最后一次迭代是分配 `words[1023]' 内存位置 str... 这是第二个 for 循环获取数据以写入输出文件的位置从。我仍然不知道我该怎么做才能让这该死的东西发挥作用。我明白了为什么我所拥有的东西不起作用,但不是解决方案应该是什么。
  • 所以我解决了数组问题。
【解决方案3】:

查看 WhozCraig 的链接(如果有):

const int MAXSIZE = 1024;
string words[MAXSIZE];

for (int i = MAXSIZE; i >= 0; i--) {
    cout << words[i] << " ";
}

你有一个从 0..1023 开始的字符串。

访问单词[1024] 具有潜在危险。

为了正确地迭代你的字符串:

for (int i = MAXSIZE - 1; i >= 0; --i) {
    cout << words[i] << " ";
}

顺便说一句,填词的时候,加个控件:

for (int i = 0; (inputFile >> str) && (i < MAXSIZE); ++i)) {
    if(str.size() <= MAXSIZE) {
       words[i] = str;
    }
}

更新

确保文件中的字符串:

"This is my test file. I hope this works. "

不以空格结尾。可以肯定的是,测试将“EOF”添加到您的字符串中:

"This is my test file. I hope this works.EOF"

其他,这样循环:

int i = 0;
while(inputFile.good() && i < MAXSIZE) {
  std::string word << inputFile;
  if(!word.empty())
    words[i] = str;
    //for showing in terminal if read correctly
    cout << words[i] << " ";
}

【讨论】:

  • 没有任何潜力。其纯粹的未定义行为
  • 也许你不走运,而 words[1024] 是 'i' 存储的位置。出于好奇也打印“i”
  • 在每个单词后打印 i [i] 给出 This 0 is 1 my 2 test 3 文件。 4 我 5 希望 6 这 7 有效。 8部作品。 1023 件作品。 1022 件作品。 (直到工作。0)
  • ??我不明白..用“cout
  • 非常感谢您的帮助。我连接点的速度很慢,我知道这有多令人沮丧。
【解决方案4】:

你在这里得到很多“作品”的问题是:

这段代码之后:

//read words from input file
for (int i = 0; (inputFile >> str) && (i < MAXSIZE); ++i)
{
    words[i] = str;
    //for showing in terminal if read correctly
    cout << words[i] << " ";
}
inputFile.close();
cout << endl;

//str = "works";

变量str 的值为works

之后,您将words 中的每个元素设置为str。所以words 中的每个元素现在都是相同的值works

 for (int i = MAXSIZE; (outputFile << str) && (i >= 0); --i)
{
    words[i] = str;//=="works"
    //for showing in terminal if written correctly
    cout << words[i] << " ";
}
outputFile.close();
cout << endl;

【讨论】:

    猜你喜欢
    • 2017-05-02
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 2012-11-20
    相关资源
    最近更新 更多