我的代码运行良好但在游戏循环中中断
My code runs fine but breaks while in a game loop
下面的代码能够 运行 在调试器中运行良好,但最终无法在 while(incorrect) 循环之后打印任何内容,如底部所评论的。在完成代码的功能或猜测所有代码后,代码会崩溃,并将您带到名为 memcpy.asm 的文件中的 运行time 错误。根据我的研究,人们通常认为此文件仅与失踪有关。我确实记得我的代码在添加提示数组并进入 visual studio 的工具->选项->调试器->符号并检查 "Microsoft Symbol Servers" 之前一直在早期版本上运行,因为我最初的 运行 检查后我取消了检查。
#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!\n\nYour 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 state\n";
}
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:\Windows\SysWOW64\ucrtbased.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.
我做了一些快速研究,但 "Access violation reading location..." 是错误的代码,但我没有发现任何逻辑错误。
word->size()
给出字符串的长度 word[0]
。不过,您想要的是数组 word[]
的大小。使用 std::size(word)
.
下面的代码能够 运行 在调试器中运行良好,但最终无法在 while(incorrect) 循环之后打印任何内容,如底部所评论的。在完成代码的功能或猜测所有代码后,代码会崩溃,并将您带到名为 memcpy.asm 的文件中的 运行time 错误。根据我的研究,人们通常认为此文件仅与失踪有关。我确实记得我的代码在添加提示数组并进入 visual studio 的工具->选项->调试器->符号并检查 "Microsoft Symbol Servers" 之前一直在早期版本上运行,因为我最初的 运行 检查后我取消了检查。
#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!\n\nYour 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 state\n";
}
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:\Windows\SysWOW64\ucrtbased.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.
我做了一些快速研究,但 "Access violation reading location..." 是错误的代码,但我没有发现任何逻辑错误。
word->size()
给出字符串的长度 word[0]
。不过,您想要的是数组 word[]
的大小。使用 std::size(word)
.