My code runs fine but breaks while in a game loop

-1

The code below is able to run fine in the debugger but ends up failing to print anything after the while(incorrect) loop as commented at the bottom. Upon finishing the function of the code or guessing everything the code crashes and takes you to the runtime error in a file named memcpy.asm. From my research people often get this file to only be associated with being missing. I do remember that my code was working all the way through on an earlier version before adding the hint array and going into visual studio's tools->options->Debugger->Symbols and checking "Microsoft Symbol Servers", since my initial run after checking it I've unchecked it.

#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;
}

The consoles error message for the code is to cryptic for me to understand what the problem is:

'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.

I did some quick research but the "Access violation reading location..." is bad code but I see nothing wrong logic wise.

c++
visual-studio
unhandled-exception
asked on Stack Overflow Oct 19, 2018 by Juhpan • edited Jan 5, 2019 by anothermh

1 Answer

1

word->size() gives you the length of the string word[0]. What you want, though, is the size of the array word[]. Use std::size(word).

answered on Stack Overflow Oct 19, 2018 by Swordfish

User contributions licensed under CC BY-SA 3.0