Question

I have some functions that need to be called multiple times, as such

int i;
i = 10;
while (i > 0)
{
    selectletter(wordArray);
    computerTurn(wordArray);
    printGrid(grid);
    i--;
}

The function selectletter works fine, and near the end of that function, it calls another function, "claimword". Claimword runs entirely fine, but at the end of the function, the program crashes when it runs out of context, as opposed to it just moving on to computerTurn as it should as shown above. I looked up on SO how to "exit" a function, and everyone said that "return;" would work fine, even in a void function. However, when I try using return, nothing at all happens, except for anything after the return statement is ignored. Can anyone tell me why the return statement doesn't work?

void claimword(Tile grid[7][6], char letter, string wordArray[100])
{
    cout << "Would you like to claim a word? (Y/N)" << endl;
    char chooseinput;
    cin >> chooseinput;
    if ((chooseinput == 'y') || (chooseinput == 'Y'))
    {
    printGrid(grid);
    cout << "Please enter the word you would like to claim." << endl;
    string input;
    cin >> input;
    int inthegrid = 0;
    int errormessage = 0;
    compchecker(grid, input, inthegrid);
    int length;
    if (inthegrid = 1)
    {
        for(int i = 0; i < 100; ++i)
        {
            if (input == wordArray[i])
            {
                if (input.find(letter) != std::string::npos)
                {
                    string strl;
                    strl = wordArray[i];

                    length = strl.length();
                    cout << "You have claimed the word " << strl << endl;
                    wordArray[i] = "/";

                }
                else
                {
                    errormessage = 1;
                }

            }
            else
            {
                ///cout << "Sorry, that word is not in the dictionary." << endl;
                errormessage = 2;
            }
        }
        if (errormessage = 1)
        {
            cout << "Sorry you cannot claim that word." << endl;
        }
        if (errormessage = 2)
        {
            cout << "Sorry, that word is not in the dictionary." << endl;

        }
                    if (length == 3)
                    {
                        human.humanpoints = human.humanpoints + 1;
                        wordsthisturn = wordsthisturn + 1;
                        cout << "You have earned one point!" << endl;
                    }
                    if (length == 4)
                    {
                        human.humanpoints = human.humanpoints + 2;
                        wordsthisturn = wordsthisturn + 2;
                        cout << "You have earned two points!" << endl;
                    }
                    if (length == 5)
                    {
                        human.humanpoints = human.humanpoints + 4;
                        wordsthisturn = wordsthisturn + 4;
                        cout << "You have earned four points!" << endl;
                    }
                    if (length == 6)
                    {
                        human.humanpoints = human.humanpoints + 8;
                        wordsthisturn = wordsthisturn + 8;
                        cout << "You have earned eight points!" << endl;
                    }
                    if (length == 7)
                    {
                        human.humanpoints = human.humanpoints + 16;
                        wordsthisturn = wordsthisturn + 16;
                        cout << "You have earned sixteen points!" << endl;
                    }
                    else
                    {
                        cout << "Your word was too small to claim any points." << endl;
                    }

    }
    }
    else
    {
        cout << "End of Player Turn." << endl;
        //return;
    }
    cout <<"Test1";
    return;
    cout <<"Test2";

}

Regardless of the input I give it (y/n and such), "Test1" displays, but "Test2" doesn't. My theory is that the program doesn't return all the way, or I'm just simply not using it right.

EDIT:

With an edited statement in the main function,

        selectletter(wordArray);
        cout << "test11";
        computerTurn(wordArray);

What should happen is that the selectletter function should be called. The selectletter function, at the end of it, calls another function, claimWord. claimWord is posted above. At the end of the function, it should end. There should be nothing left for it to do, and after all those if/elses regarding points, and even if no points are scored, or anything in the function happens, the function should end. The program should then display "test11", but it does not.

EDIT2:

void selectletter(string wordArray[100])
{
    cout << endl;
    cout << "REMAINING LETTERS:" << endl;
    cout << human.humanletters << endl;
    cout << "Select a letter.";
    int length;
    length = human.humanletters.size();
    char input;
    cin >> input;
    int column;
    int row = 7;
    int cinput;
    //mght have to change since 0 is the first val
    cout << "What column would you like to drop that in? (1-7)";
    cin >> cinput;
    column = cinput - 1;
    //cout << "Test1";
    while (row > 0)
    {
    if (grid[row-1][column].active == true)
    {
        row--;
        //cout << "Test3";
    }
    else
        for(int i = 0; i < length; i++)
        {

        if(human.humanletters[i] == input)
        {
            //cout << "Test5";
            human.humanletters.erase(std::remove(human.humanletters.begin(), human.humanletters.end(), input), human.humanletters.end());
            grid[row-1][column].letter = input;
            grid[row-1][column].active = true;
            cout << endl;
            //cout << "Test6";
            claimword(grid, input, wordArray);
            //this removes ALL instances of the letter, however
        }
        break;
        //need to add something for if the letter is not in the string
    //}
        //row = 9999;
    }
}
}
Was it helpful?

Solution 2

Your while loop has the condition while (row > 0), and you only decrement row when (grid[row-1][column].active == true). If that ever evaluates to false, you won't decrement row and your program runs forever.

Perhaps your break; was meant to break out of the while loop, but all it will do is break out of the for loop. A break statement breaks out of the nearest enclosing loop/switch block.

OTHER TIPS

Regardless of the input I give it (y/n and such), "Test1" displays, but "Test2" doesn't.

That is what it is supposed to do. You called return after displaying Test1 and before displaying Test2, so the latter was skipped. return is an immediate return to the function that called the current function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top