Infinite loop problem

Pages: 12
Ok now i got the code working to get the list and randomly select a word but when i enter the right word it displays the word thousands of times in a neverending loop what am i doing wrong?

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
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

string word_list(string randomword)
{
    string store_list = " ";
    string store_word = " ";
    srand(time(0));

    ifstream fileIn;
    fileIn.open("word list.txt");
    vector<string> words;

    while(!fileIn.eof())
    {
        fileIn >> store_word;
        words.push_back(store_word);
    }

    randomword = words[rand() % words.size()];

    //cout << randomword << endl; //Debug line

    return randomword;
}

int main()
{
    srand(time(0));

    string original_word = " ";
    string shuffled_word = " ";
    string random_word = " ";
    string HWLF = word_list(random_word);
    string guess = " ";
    int select = 0;

    bool game_loop = true;

    cout << "CRYPTO GUESS\n" << endl;

    cout << "Do you want to enter words yourself or have the computer give you words?" << endl;
    cout << "1) Enter words myself - Not working" << endl;
    cout << "2) Have the computer give me words" << endl;
    cin >> select;

    //Main Game loop

    cin.clear();

    if(select == 2)
    {
        while(game_loop != false)
        {
            shuffled_word = HWLF;
            random_shuffle(shuffled_word.begin(), shuffled_word.end()); //#2

            cout << shuffled_word << endl;

            cout << "\n";

            while(guess != HWLF)
            {
                cout << "Your guess: ";
                cin >> guess;

                if(guess == HWLF)
                {
                    cout << "Is correct!\n" << endl;
                    break;
                }
                else if(guess == "Quit" || guess == "quit")
                {
                    cout << "Goodbye thanks for playing" << endl;
                    return 0;
                }
                else
                {
                    cout << "Is wrong! Try again\n" << endl;
                }
            }
        }
    }
}
Where do you re-evaluate game_loop? It stays true the entire duration of the program.
ok well i put it before the break; in the is correct if function and that stopped it but the program ends which is not what i want to happen.
Can you explain further what you're trying to do for me? Maybe i'm not getting the right picture.

EDIT:
Oh. I see it now. you need to reset guess. Otherwise it will always equal HWLF on subsequent loops. while(guess != HWLF) is never executed because guess == HWLF.
Or move cin >> guess outside the second while loop.
Last edited on
ok so the program is supposed to get a list of words from a file, then randomly select one and jumble it up, from there the player is supposed to guess what the word is, if they get it right, they get to do another word, if they are wrong they get to try to answer that word again. the game should never quit unless the user exits out manually.
Refer to my edit above. Sorry about that :p
oh ok so how do i reset guess? do cin.clear() or something?
Last edited on
ok so i reset guess by doing guess = " "; in the correct if statement but it keeps giving me the same word.
any way to reset the random word
Last edited on
Ok now i have 2 problems the same problenm as before where the same word shows up and i want to let the user use their own lists but it just stops working what am i doing wrong?

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
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <vector>

using namespace std;

string word_list(string RW, string GWL)
{
    string store_word = " ";
    srand(time(0));

    ifstream fileIn;
    fileIn.open(GWL.c_str());
    vector<string> words;

    while(!fileIn.eof())
    {
        fileIn >> store_word;
        words.push_back(store_word);
    }

    RW = words[rand() % words.size()];

    return RW;
}

int main()
{
    srand(time(0));

    string original_word = " ";
    string shuffled_word = " ";
    string random_word = " ";
    string open_txt_document = " ";
    string HWLF = word_list(random_word, open_txt_document);
    string guess = " ";
    int select = 0;

    bool game_loop = true;

    cout << "CRYPTO GUESS\n" << endl;

    cout << "Do you want to enter words yourself or have the computer give you words?" << endl;
    cout << "1) Enter words myself - Not working" << endl;
    cout << "2) Have the computer give me words" << endl;
    cin >> select;

    //Main Game loop
    cout << "open a file" << endl;
    getline(cin, open_txt_document);

    if(select == 2)
    {

        while(game_loop != false)
        {
            shuffled_word = HWLF;
            random_shuffle(shuffled_word.begin(), shuffled_word.end()); //#2

            cout << shuffled_word << endl;

            cout << "\n";

            while(guess != HWLF)
            {
                cout << "Your guess: ";
                cin >> guess;

                if(guess == HWLF)
                {
                    cout << "Is correct!\n" << endl;
                    guess = " ";
                    break;
                }
                else if(guess == "Quit" || guess == "quit")
                {
                    cout << "Goodbye thanks for playing" << endl;
                    return 0;
                }
                else
                {
                    cout << "Is wrong! Try again\n" << endl;
                }
            }
        }
    }
}
bump
Hi there,
Have you tried sprinkling in print statements and stepping through your program by setting a breakpoint? Often times that will show you where your program stops working if it's a runtime error, or at least give you a better guess.

Putting things like:
cout << "I have reached the while-loop\n"; at even intervals but clear steps in your program describing where you are is a good debugging technique in programs that are involved or have multiple places that could cause an issue. This helps with loops especially because it can even help you find out how many times it's executing, or other useful things like that.

Try that and let us know where it stops and what error is given when it stops (if any at all). I'd love to help but I'm tired and don't have time to analyze every part of your code :)
ok well it seems to work fine until i do this

string word_list(string RW, string GWL)//<- Function

string HWLF = word_list(random_word, choose_list);//<- string in main

im trying to have the user input the name of a word list in main and reference the string to the word_list function but it says the application stopped working.
"Application stopped working" is a sign of a runtime error that usually implies there is a bad pointer or something to do with pointers. I see that you're using arrays in word_list() as well, which can cause those issues too.

After a look the first thing that jumped out as a possibility is this line of code:
RW = words[rand() % words.size()];

Are you positive that rand() % words.size() isn't outside of the vector's size/index range?

. . . Either that or another quick glance says that even though words is declared as a vector in vector<string> words and the capacity is remaining static when you should be checking to see if the vector needs to be resized to allow more text to be pushed to words, and thus you're trying to store things in the vector outside of its range.

Give those a thought. Those are just some potential issues that I saw glancing over what you have.
Last edited on
"Are you positive that rand() % words.size() isn't outside of the array's size/index range?"

Im not sure, someone was helping me and they wrote that part for me, im not too sure what it even does.

How would i resize the vector?
You would want to keep track of the current size, which conveniently is available as part of the vector in the STL. Check out the documentation at:
http://www.cplusplus.com/reference/vector/vector/vector/

and more specifically:
http://www.cplusplus.com/reference/vector/vector/resize/

To save you time and a headache of reading all of that:
The prototype of the resize method is void resize (size_type n);
Just use words.resize(new_size). Just fill in "new_size" with an integer variable...or could even be any digit, i.e. 27, or 104. Reference those docs if you need a more wide range of information (albeit decently cryptic...).

I'd keep track of it within the while loop (or somewhere else you deem appropriate) and use if-style logic to make sure the vector size isn't an issue. Do a check using the .size() method, and if it gets close to the current max size, then resize it in the body of the if-statement. Generally it's good practice to keep a cushion/buffer of a number of indexes that always remain open so you don't run into issues like you have.

Good luck!
ok so can you help me by modifying my code, im sorry but im a visual learner, its unbelievably difficult, pretty much impossible for me to just read what to do then do it.
I'm going to give you some code, but make sure you think about what it's doing. Also, it won't be plug-and-play code either. I think learning this idea and implementing it as you see fit will be much better, and knowing these concepts in the future (rather than copy-pasting someone's code) will help you avoid issues later.

Conceptually I'll explain how a couple methods from the vector class work, and then give an example of how you might use them. The beauty of a vector is that it's a dynamic array that is also a class. It benefits both from being able to dynamically adjust its size during program runtime (whereas a normal array is statically defined at initialization of the variable) as well as having access to a slew of useful methods.

.size() returns the number of elements currently in the vector that's calling the method.
.capacity() returns the total number of elements that can currently be contained within the vector that's calling the method.

Example:
Say you have words, which is like the vector in your code. When it's defined it has a certain capacity as well as a current size. Using these methods can be incredibly useful to determine whether or not your vector is getting "full" and when you should allocate more memory to it.

You can then use logic to perform an operation to adjust the capacity if need be. Forgive my previous post, but resize() is not the method you would want to use in this case, since resize() directly affects the vectors size (i.e. the current number of elements in it right now) and not its overall capacity. The capacity is what we care about since we're trying to make sure that the program isn't trying to add elements to a vector that's "full". If you want to think of a vector as a glass of water, adjusting the capacity to fit more elements is basically like increasing the size of the glass to fit more water. Instead we would want to use the reserve() method (found at http://www.cplusplus.com/reference/vector/vector/reserve/).

Some example code to support this idea is as follows:
1
2
3
4
if (words.size() == words.capacity())
  {
	words.reserve(words.size() * 2);
  }

This code checks to see if the size of the vector named words is equal to its defined capacity. If the logic operator == evaluates that statement to true, then the capacity of the vector words is altered to a new value dependent on the parameter of the reserve() method. I used words.size() * 2 as the new value as this gives plenty of new room for the vector and is also more robust because it depends on its current variable size. You could also use something like words.reserve(words.size() + 10) or something similar if you'd rather keep the size of the vector down for performance sake. For your case though, I don't think it matters.
thank you very much for that detailed explanation, i understand it alot better now. so basically what that code does is say that if the vector is full give it more room? is that why my program is failing because im overloading the words vector? also could you give me a hint on what parts of the code i should change, like say around what lines? oh and what does .reserve() do?
Last edited on
Calling the reserve() method allocates more space for the vector, and increases its capacity. You want to use that instead of resize(), because resize() actually is used for something else and you don't want to use it in this case.

so basically what that code does is say that if the vector is full give it more room?

Yep! Reading that if-statement out like it would be evaluated would sound like:
"If the size of vector words is equal to the capacity of vector words, then reserve more space for it."

The prototype of the method for reserve() is:
void reserve (size_type n); Where n is the number of items that will "fit" in the vector after calling the method.

And to be honest, I'm not entirely sure this is your problem, but it definitely could be. Not keeping track of a vector's size and adding some variable amount of elements to it is not a good idea, because if you happen to try to put too many in the vector it will cause problems.

I'd take a look at:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string word_list(string RW, string GWL)
{
    string store_word = " ";
    srand(time(0));

    ifstream fileIn;
    fileIn.open(GWL.c_str());
    vector<string> words;

    while(!fileIn.eof())
    {
        fileIn >> store_word;
        words.push_back(store_word);
    }

    RW = words[rand() % words.size()];

    return RW;
}

Start in here, and specifically look at your while-loop. Before you store something in words you need to make sure that it's not in danger of being full.
Pages: 12