C++ error C2181: illegal else without matching if

I’m new to C++. I have been following an online course that teaches you how to make a Hangman game in C++. For the most part the code was working, every time I debugged it ran fine. I completed the entire code and double checked it, but I’m getting an error on one of the ‘else’ the errors I’m getting is “error C2181: illegal else without matching if.

Here’s my code.

// Hangman game
// CN

// Header file
#include "stdafx.h"
// Input/Output
#include <iostream>
// Text
#include <string>
// Information storage
#include <vector>
// Figuring out the information storage
#include <algorithm>
// Converts lower cased letters to higher case letters
#include <cctype>
// Reads computers time and distributes a word from a list accordingly
#include <ctime>
// Library for system
#include <cstdlib>

using namespace std;


int main()
{
    // Constant data type which is a fixed value, MaxWrong in Pascal Text and the max number of guesses.
    const int MaxWrong = 6;


    vector<string> words;
    words.push_back("SANTA CLAUSE");
    words.push_back("REINDEER");
    words.push_back("PRESENT");
    words.push_back("TREE");
    words.push_back("MILK");
    words.push_back("COOKIE");

    // Parenthesis must always match
    srand(static_cast<unsigned int>(time(0)));
    random_shuffle(words.begin(), words.end());
    const string theWord = words[0];
    int wrong = 0;
    string soFar(theWord.size(), '_');
    string used = "";

    cout << "\n******** Welcome to Chaye's Hangman ********\n\n";
    cout << "      __  \n";
    cout << "     |  | \n";
    cout << "     |    \n";
    cout << "     |    \n";
    cout << "     |    \n";
    cout << "     |    \n";
    cout << "  ___|____  \n\n";
    cout << " Do Your Best Human  \n\n";

    // Loop
    while ((wrong < MaxWrong) && (soFar != theWord))
    {
        cout << "\n You have " << (MaxWrong - wrong);
        cout << " incorrect tries left.\n";
        cout << "\n You've used these letters:\n" << used << endl;

        char guess;
        cout << "\n\n Enter your guess: ";
        cin >> guess;
        guess = toupper(guess);
        while (used.find(guess) != string::npos)
        {
            cout << "\n You've already tried " << guess << endl;
            cout << " Enter your guess: ";
            cin >> guess;
            guess = toupper(guess);
        }

        used += guess;

        if (theWord.find(guess) != string::npos)
        {
            cout << " That's right " << guess << " is in the word,\n";

            for (int i = 0; i < theWord.length(); ++i)
            {
                if (theWord[i] == guess)
                {
                    soFar[i] = guess;
                }
            }
        }

        else
        {
            cout << " sorry. " << guess << " isn't in the word.\n";
            ++wrong;
        }


        // If one is wrong
        if (wrong == 1)
        {
            cout << "      __  \n";
            cout << "     |  | \n";
            cout << "     |  O \n";
            cout << "     |    \n";
            cout << "     |    \n";
            cout << "     |    \n";
            cout << "  ___|____  \n\n";
            cout << " Do Your Best Human  \n\n";
        }
        else
        {
            cout << endl;
        }

        // If one is wrong
        if (wrong == 2)
        {
            cout << "      ___  \n";
            cout << "     |   | \n";
            cout << "     |   O \n";
            cout << "     |   @ \n";
            cout << "     |    \n";
            cout << "     |    \n";
            cout << "  ___|____  \n\n";
            cout << " Do Your Best Human  \n\n";
        }
        else
        {
            cout << endl;
        }


        // If one is wrong
        if (wrong == 3)
        {
            cout << "      ___  \n";
            cout << "     |   | \n";
            cout << "     |   O \n";
            cout << "     |  /@ \n";
            cout << "     |     \n";
            cout << "     |     \n";
            cout << "  ___|____  \n\n";
            cout << " Do Your Best Human  \n\n";
        }
        else
        {
            cout << endl;
        }

        // If one is wrong
        if (wrong == 4)
        {
            cout << "      ___   \n";
            cout << "     |   |  \n";
            cout << "     |   O  \n";
            cout << "     |  /@\ \n";
            cout << "     |      \n";
            cout << "     |      \n";
            cout << "  ___|____  \n\n";
            cout << " Do Your Best Human  \n\n";
        }
        else
        {
            cout << endl;
        }

        // If one is wrong
        if (wrong == 5)
        {
            cout << "\n You've been hung  \n";
            cout << "      ___   \n";
            cout << "     |   |  \n";
            cout << "     |   O  \n";
            cout << "     |  /@\ \n";
            cout << "     |  /   \n";
            cout << "     |      \n";
            cout << "  ___|____  \n\n";
            cout << " Do Your Best Human  \n\n";
        }
        else
        {
            cout << "\n I'm suprised you've got this far\n";
        }
        cout << "\n The word was " << theWord << endl;


    }

    // If one is wrong
    if (wrong == MaxWrong)
    {
        cout << "\n You've been hung  \n";
        cout << "      ___   \n";
        cout << "     |   |  \n";
        cout << "     |   O  \n";
        cout << "     |  /@\ \n";
        cout << "     |  / \  \n";
        cout << "     |      \n";
        cout << "  ___|____  \n\n";
        cout << " Do Your Best Human  \n\n";
    }
    else
    {
        cout << "\n I'm suprised you've got this far\n";
    }
    cout << "\n The word was " << theWord << endl;


    // Pause Console
    system("pause");

    // Kills the application once done
    return 0;
}

c++

Leave a Comment