我的代码运行良好,但在游戏循环中中断



下面的代码能够在调试器中正常运行,但最终在底部注释的while(不正确(循环之后无法打印任何内容。完成代码的功能或猜测所有内容后,代码崩溃并将您带到名为memcpy.asm的文件中的运行时错误。根据我的研究,人们经常认为这个文件只与丢失有关。我确实记得我的代码在添加提示数组并进入 Visual Studio 的工具->选项->调试器->符号并选中"Microsoft符号服务器"之前一直在早期版本上运行,因为我在检查它后首次运行,我已经取消选中它。

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
int main()
{
std::cout << "Welcome to my Word Scramble!nYou can quit at anytime by typing 'quit' and get a hint by typing 'hint' though this halfs your points!n";
srand(static_cast<unsigned int>(time(0)));
bool gameOn = true;
int score = 0;
while (gameOn)
{
std::string word[] = { "programming","saturn","helpful","terrible","college" };
std::string hint[] = { "Another word is coding","A planet in our solar system","to be of use","Just the worst","School for grown ups" };
for (int i = 0;i < word->size();i++) 
{
std::string jumble = word[i];
int length = jumble.size();
for (int j = 0; j < length; j++)
{
int index1 = rand() % length;
int index2 = rand() % length;
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}
std::cout << "Here is the word you'll be unscrambling!t--" << jumble << "--n";
int guesses = 1;
int pointReward = 100*length;
std::string guess;
bool incorrect = true;
while (incorrect)
{
std::cin >> guess;
if (guess == "quit") 
{
return 0;
}
else if (guess == "hint")
{
pointReward /= 2;
std::cout << "Your score for this round has been reduced by half, Here is your hint:";
std::cout << hint[i] << std::endl;
}
else if (guess == word[i])
{
incorrect = false;
int roundPoints = pointReward / guesses;
score += roundPoints;
std::cout << "Correct!!! You get " << roundPoints << " Points!nnYour total score is " << score << std::endl;
}
else if (guess!="hint"&&guess!="")
{
guesses++;
std::cout << "That wasn't quite it, I believe in you!n";
}
std::cout << "right after else if staten";
}
std::cout << "right after incorrect loop";
}
std::cout << "Anything after this point won't print";
gameOn = false;
}
std::cout << "Your final score was " << score << "!";
std::cout << "tThanks For Playing My First C++ Game!";
return 0;
}

代码的控制台错误消息是让我隐晦地理解问题所在:

'Word Jumble.exe' (Win32): Unloaded 'C:WindowsSysWOW64ucrtbased.dll'
Exception thrown at 0x509146FE (vcruntime140d.dll) in Word Jumble.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.
Unhandled exception at 0x509146FE (vcruntime140d.dll) in Word Jumble.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.

我做了一些快速的研究,但"访问违规阅读位置..."是糟糕的代码,但我在逻辑上没有看到任何错误。

word->size()给你字符串word[0]的长度。但是,您想要的是数组word[]的大小。使用std::size(word).

最新更新