Subscript out of range

string subscript out of range

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<string>
using namespace std;

string enter(string theword)
{
    cout<<"welcome to hangman please enter the word to be guessed";
    cin>>theword;

    return(theword);//returns the word to main
}

void guessaletter(string &answer)
{
    int count = 0, guesscount=0;
    char inchar; 
    string dash;
    bool bob=false;
    for(int t=0; t>answer.size(); t++)//sets up dash to have the number of dashes that answer has of letters
    {
        dash+="-";
    }
    cout<<"word is"<<dash;//outputs the initial word in dashes
    while(dash!=answer)
    {
        cout << "Your letter ($ to guess the word)? ";// user guesses a letter
        cin >> inchar;
        guesscount++;// In making a guess, add one to the guess count.
        if(inchar=='$')
        {
            cout<<"your guess? ";
            string guess;
            for(unsigned int i = 0; i < answer.size(); i++)
            {
                if (guess[i]!=answer[i])//checks answer at i is equal to the letter.
                {
                    bob=true;
                }
            }
            if (bob==true)
            {
                cout<<"InCorrect"; //tells you you're wrong
                bob=false;
            }
            else
                cout<<"Correct! You took "<<guesscount<<" tries";
        }
        else
        {
            for(unsigned int i = 0; i < answer.size(); i++)
            {
                if (dash[i]==answer[i])
                {
                    cout<<"Sorry, you guessed "<<inchar<<" already. Try again";
                }
                else if (inchar==answer[i])//checks answer at i is equal to the letter.
                {
                    dash[i]=inchar;//if so it will input the letter at the correct location in dash.
                    bob=true;
                }
            }
            if (bob==true)
            {
                cout<<"the word is "<<dash; //shows dash with the new letter(s).
                bob=false;
            }
            else
                cout<<"sorry, there are no "<<inchar<<"'s";//is no letter then it says so.
        }
    }
}


int main()
{
    string word;
    word=enter(word);
    guessaletter(word);

   return (0);
   } 
Last edited on
I believe the error here is on line 76. You are trying to pass word to enter() on the same line you are declaring it. I don't believe this works since the function should happen first, then the return from the function, then the assignment to word. Try splitting that up to this:

1
2
string word;
word = enter(word);
that did help thank you, i'm now running into an error
string subscript is out of range
when i try to guess letters
Hi GIjessiebear,

When you have an error please post the compiler output in full, because it gives the line number.

Line 19: did you mean < rather than > ? But this is not your main problem.

Another different thing - LInes 35 - 46:

With stings you can compare them directly with the == operator. Although I would recommend converting them both to upper case first. With your code bob will only be true if the last letter is equal. But then you say it is incorrect when bob is true, and set it to false.

HTH
Topic archived. No new replies allowed.