下面的代码能够在调试器中正常运行,但是最终导致在while(invalid)循环之后无法打印任何内容,如底部所注释。完成代码功能或猜测所有内容后,代码将崩溃,并带您进入名为memcpy.asm的文件中的运行时错误。从我的研究中,人们经常会将此文件仅与丢失相关联。我确实记得我的代码在添加提示数组并进入Visual Studio的工具-> options-> Debugger-> Symbols并检查“ 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.
我做了一些快速研究,但是“访问冲突读取位置...”是错误的代码,但我认为逻辑上没有错误。
答案 0 :(得分:1)
word->size()
给出了字符串word[0]
的长度。不过,您需要的是数组word[]
的大小。使用std::size(word)
。