C++ 调试代码中的溢出问题
我目前正在写一个刽子手程序,我遇到了一个溢出问题。有人知道是什么导致了这一切吗?我试过到处换号码,但似乎找不到问题C++ 调试代码中的溢出问题,c++,C++,我目前正在写一个刽子手程序,我遇到了一个溢出问题。有人知道是什么导致了这一切吗?我试过到处换号码,但似乎找不到问题 #include <iostream> #include <fstream> #include <varargs.h> #include <string> #include <cstdlib> #include <time.h> using namespace std; int main(int argc,
#include <iostream>
#include <fstream>
#include <varargs.h>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
int main(int argc, char* argv[])
{
char ch;
fstream file;
if (argc != 3)
{
cout << "game file guesses";
return 1;
}
file.open(argv[1],
ios::in);
if (!file)
{
cerr << endl
<< "game: error opening file '"
<< argv[1] << "'"
<< endl << endl;
return 2;
}
int word_count = 0;
char word[11];
bool in_word = false;
int element = 0;
string str[100];
while ((ch = file.get()) != EOF)
{
if (ch != ' ' && ch != '\n')
{
word[element] = ch;
element++;
if (!in_word)
{
in_word = true;
}
}
else
{
in_word = false;
word[element] = '\0';
str[word_count] = string(word);
word_count++;
element = 0;
}
}
int x = atoi(argv[2]);
cout << x << endl;
file.close();
srand((unsigned int)time(NULL));
int count = (rand() % word_count + 1);
string random_word = str[count];
cout << random_word << endl;
string mask = "";
for (int i = 0; i < random_word.length(); i++)
{
mask = mask + "*";
}
cout << mask << endl;
for (int guesses = 0; guesses < x; )
{
cout << "Guess a letter: ";
string guess;
cin >> guess;
int k;
for (k = 0; k <= random_word.length(); k++)
{
if (guess == random_word.substr(k))
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
char ch;
流文件;
如果(argc!=3)
{
请将您看到的实际错误添加到您的帖子中,并尝试将代码减少到仍能再现错误的最小数量。错误代码是包含掩码的行上的c2664。替换(k,1,1,猜测);哦,这是一个编译器错误,所以它不是一个溢出错误。看看这个答案:我已经阅读了线程,但我不明白如何实现INT[],特别是我认为您试图使用一个不存在的std::string::replace版本。mask.replace(k,1,1,guess[0])
可能会编译。没有版本的replace
接受您试图传递的参数。请参阅
{
mask.replace(k, 1, 1, guess);
cout << mask << endl;
cout << "Correct" << endl;
break;
}
else
{
guesses++;
}
}
}
return 0;
};