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循环后,其余的for循环会使用值"3435973836"初始化迭代器,而不考虑正确的声明("int i=0"、"int k=0"等)。我声明它们为unsigned,省略signed/signed仍然会错误地初始化值(-858993460)。
这做了两件事中的一件:
- unsigned循环永远不会启动,因为值太高,无法启动循环
- 省略会使循环运行很长很长时间
有什么想法吗?我已经在下面发布了代码。请忽略除此之外我所犯的任何其他错误,因为我无法通过此来调试其他任何错误。
编辑-->解决了:我按值传递向量的问题。但即使我把它改为通过引用传递,这个程序仍然不能工作。实际问题是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?它应该是该函数的本地函数,因为您不在外部使用它。它看起来不可靠,原因与传递的向量相同。
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 为什么兰德每次都给我几乎相同(但略有不同)的数字
- 当我输入字符类型的数字时,为什么我无法获得整数?
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 为什么使用数组元素查找最大数字的程序不起作用?
- C++ 为什么数字限制不适用于uint8_t和int8_t?
- 为什么strlen(s)与s的大小不同,为什么cout-char显示的是字符而不是数字
- 为什么我C++得到这个随机的奇怪数字
- 为什么当我输入一个被接受的数字时,我的 do-while 循环没有中断?
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 为什么我的位移给出不正确的数字
- 为什么 sf::Text 显示点而不是数字?
- 反转一个数字程序不起作用,为什么?
- 为什么这个程序接受整数和字母数字输入并打印它们? C++ 中的 std::string 也采用整数值吗?
- 为什么当我输入一个大数字时,输出会一遍又一遍地重复?
- 为什么断点显示数组的第二个值是一个大数字?额外学分工作
- 为什么我的 while 循环永远不会在 C++ 中的数字猜测游戏中结束?
- 为什么当我在 c++ 中运行函数时,我的代码显示数字 53