For 循环 - i 的值不是 0,即使代码说"int i = 0"。i = 大数字,为什么?如何解决?

For-Loop - value of i is not 0 even if code says 'int i = 0'. i = BIG number, why? How to fix?

本文关键字:数字 为什么 解决 何解决 循环 For 代码 int      更新时间:2023-10-16

我正在设计一个程序来清理包含代码的文本文件。它删除注释、多余的空格/行,并为文件中带有多个分号的行创建新行。

实际上我让这个程序工作了,但它使用了数组。由于我正在开发另一个基于此的程序,除了数据输入的大小更加多样化之外,我正在将其转换为使用向量而不是标准数组,这样我就可以重新调整程序的用途。。。这就是重点。

我的问题是,在程序迭代完第一个for循环后,其余的for循环会使用值"3435973836"初始化迭代器,而不考虑正确的声明("int i=0"、"int k=0"等)。我声明它们为unsigned,省略signed/signed仍然会错误地初始化值(-858993460)。

这做了两件事中的一件:

  1. unsigned循环永远不会启动,因为值太高,无法启动循环
  2. 省略会使循环运行很长很长时间

有什么想法吗?我已经在下面发布了代码。请忽略除此之外我所犯的任何其他错误,因为我无法通过此来调试其他任何错误。

编辑-->解决了:我按值传递向量的问题。但即使我把它改为通过引用传递,这个程序仍然不能工作。实际问题是Microsoft Visual Studio 2012。我有一次PBV,它破坏了我的项目。我不得不开始一个新的VS项目并插入代码。如果你这样做,小心不要在PBV仍然运行程序,否则你将不得不再次运行。我不知道为什么会发生这种事。也许了解微软Visual Studio的人可以回答这个问题。再次感谢社区!

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void removeComments(vector<string> row, ifstream & myfile);
void sortLines (vector<string> row);
//void cleanCode (vector<string> row);
int main()
{
    vector<string> row;
    ifstream myfile;
    //Open txt file
    myfile.open("newdata.txt");
    if(myfile.is_open()) 
    {
        //Remove comments, create proper lines, and remove/add spaces.
        removeComments(row, myfile);
        sortLines(row);
        //cleanCode(row);
    }
    else 
    {
        cout<< "ERROR: No file was able to open.  Check the file name or location and try again."<< endl << endl;
    }

    for (unsigned int i = 0; i < row.size(); i++)
    {
        cout<< row[i] << endl;
    }
    cout<< endl;
    myfile.close();
    system("PAUSE");
    return 0;
}
//FUNCTIONS

//Removes all comments.
void removeComments(vector<string> row, ifstream & myfile)
{
    string line;
    while(getline(myfile, line))
    {
        string tempString;
        for(unsigned int i = 0; i < line.length(); i++)
        {
            //Copy characters to row string array until "//".
            //All character following and including "//" will be ignored.
            if(line.at(i) == '/' && line.at(i+1) == '/')
            {
                break;
            }
            else
            {
                tempString += line.at(i);
            }
        }
        row.push_back(tempString);
    }

}
//Creates a new line after every semi-colon.
void sortLines (vector<string> row)
{
    vector<string> tempRow;
    string tempLine;
    string tempString;
    for (unsigned int i = 0; i < row.size(); i++)
    {
        tempLine = row [i];
        for (unsigned int j = 0; j < tempLine.length(); j++)
        {
            tempString += tempLine[j];
            if (tempLine[j] == ';')
            {
                tempRow.push_back(tempString);
            }
        }
    }
    //Revalue row array elements.
    //DEBUGGING OUTPUT
    for (unsigned int i = 0; i < tempRow.size(); i++)
    {
        cout<< tempRow[i] << endl;
    }
    row.clear();
    row = tempRow;
}

好的,这是我的参考编辑:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void removeComments(vector<string> &row, ifstream & myfile);
void sortLines (vector<string> &row);
//void cleanCode (vector<string> &row);
int main()
{
    vector<string> row;
    ifstream myfile;
    //Open txt file
    myfile.open("newdata.txt");
    if(myfile.is_open()) 
    {
        //Remove comments, create proper lines, and remove/add spaces.
        removeComments(row, myfile);
        sortLines(row);
        //cleanCode(row);
    }
    else 
    {
        cout<< "ERROR: No file was able to open.  Check the file name or location and try again."<< endl << endl;
    }

    for (unsigned int i = 0; i < row.size(); i++)
    {
        cout<< row[i] << endl;
    }
    cout<< endl;
    myfile.close();
    system("PAUSE");
    return 0;
}
//FUNCTIONS

//Removes all comments.
void removeComments(vector<string> &row, ifstream & myfile)
{
    string line;
    while(getline(myfile, line))
    {
        string tempString;
        for(unsigned int i = 0; i < line.length(); i++)
        {
            //Copy characters to row string array until "//".
            //All character following and including "//" will be ignored.
            if(line.at(i) == '/' && line.at(i+1) == '/')
            {
                break;
            }
            else
            {
                tempString += line.at(i);
            }
        }
        row.push_back(tempString);
    }

}
//Creates a new line after every semi-colon.
void sortLines (vector<string> &row)
{
    vector<string> tempRow;
    string tempLine;
    string tempString;
    for (unsigned int i = 0; i < row.size(); i++)
    {
        tempLine = row [i];
        for (unsigned int j = 0; j < tempLine.length(); j++)
        {
            tempString += tempLine[j];
            if (tempLine[j] == ';')
            {
                tempRow.push_back(tempString);
            }
        }
    }
    //Revalue row array elements.
    //DEBUGGING OUTPUT
    for (unsigned int i = 0; i < tempRow.size(); i++)
    {
        cout<< tempRow[i] << endl;
    }
    row.clear();
    row = tempRow;
}

正如其他人所指出的,您是:

  • 按值传递矢量

  • 使用我范围之外可能未初始化的东西(在您的问题中没有声明/定义)作为增量变量

    //Creates a new line after every semi-colon.
    void sortLines (vector<string> row)
    {
      vector<string> tempRow;
      string tempLine;
      string tempString;
      for (unsigned int i = 0; i < row.size(); k++) // <-- what is k??
      {
    

为什么要将"字符串行"传递给removeComments?它应该是该函数的本地函数,因为您不在外部使用它。它看起来不可靠,原因与传递的向量相同。

相关文章: