Infinite loop problem

Pages: 12
well i know exactly what the problem is, the program stops working when i do this

string HWLF = word_list(random_word, open_txt_document);

and add string GWL to the word_list function the program quits working, why is that? i was trying to let the user input a word list name, then pass that string from main to word_list and open it but i cant seem to do that because it wont work.
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.


No, it won't. On the other hand if you try to access a vector element that doesn't exist, you will invoke undefined behavior and that's exactly what is happening. On line 39 you feed word_list a file name of " ", which means that the file is going to fail to open and you're going to try to access an element of a vector that doesn't exist (as well as divide by 0.)

Fix word_list so it doesn't fail if the file doesn't open or don't feed it the name of a file that doesn't exist. The first would be preferable.
Last edited on
Ok i did that and its always failing, 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
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <vector>

using namespace std;

string word_list(string RW, string GL)
{
    string store_word;
    srand(time(0));

    ifstream fileIn;
    fileIn.open(GL.c_str());

    if(fileIn.fail())
    {
        cout << "File failed to open" << endl;
    }

    vector<string> words;

    while(!fileIn.eof())
    {
        if(words.size() == words.capacity())
        {
            words.reserve(words.size() * 3);
        }
        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 choose_list;
    string HWLF = word_list(random_word, choose_list);
    string guess;

    int select;

    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;

    cin.ignore(50, '\n');

    cout << "Select list" << endl;
    getline(cin, choose_list);

    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;
                }
            }
        }
    }
}
Last edited on
Lines 48 and 49
1
2
string choose_list;
    string HWLF = word_list(random_word, choose_list);
does choose_list contain the filename when you call word_list?

Line 87 guess = " "; will cause the while loop at line 79 to keep repeating.
Last edited on
"does choose_list contain the filename when you call word_list?"

I dont know thats why im asking. Line 87 is the only thing keeping the program from looping so it is necessary unless there is a better solution.
Ok i did that and its always failing, what am i doing wrong?


The same thing you were doing before. You didn't fix anything. All you did was add some output into the mix.
well Im out of ideas i dont know what to do, you guys can tell me what i need to do all day but i wont get any closer to fixing it because i dont learn like that, i cant complete tasks like that. Can you guys please just show me whats wrong, explain why its wrong and show me the right code? I know you guys dont believe that anyone can learn like that but everyone learns differently and seeing my code working correctly will help me out.
Last edited on
Ok i think i figured it out i did this

1
2
3
4
cout << "Select list" << endl;
getline(cin, choose_list);

string HWLF = word_list(random_word, choose_list);


and it worked but i still have the problem of it giving me the same word every time.

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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
//Crypto Guess is based off of Crypto Logic for the Oddessy 2
//Created Tuesday, June 4th 2013 by Chay Hawk
*/

/*||EXPLAINING THE CODE||

*/


#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <vector>

using namespace std;

string word_list(string RW, string GL)
{
    string store_word;
    srand(time(0));

    ifstream fileIn;
    fileIn.open(GL.c_str());

    if(fileIn.fail())
    {
        cout << "File failed to open" << endl;
    }

    vector<string> words;

    while(!fileIn.eof())
    {
        if(words.size() == words.capacity())
        {
            words.reserve(words.size() * 3);
        }
        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 choose_list;
    string guess;

    int select;

    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;

    cin.ignore(50, '\n');

    cout << "Select list" << endl;
    getline(cin, choose_list);

    string HWLF = word_list(random_word, choose_list);

    cout << "\n";

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

            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;
                }
            }
        }
    }
}
Here is a working version of your code. Please see the comments for some explanation.
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <vector>

using namespace std;
/*
changed word_lists to return a vector of words,
now you can read the list in once and get random words from the list
*/
vector<string> word_list(string GL)
{
    string store_word;
   // srand(time(0)); only seed once

    ifstream fileIn;
    fileIn.open(GL.c_str());

    if(fileIn.fail())
    {
        cout << "File failed to open" << endl;
    }

    vector<string> words;

    while(fileIn >> store_word)
    {
	//this isn't needed!
	//vectors increase capacity automatically when full
        /*if(words.size() == words.capacity()) 
        {
            words.reserve(words.size() * 3);
        }*/
        //fileIn >> store_word; //moved ^
        words.push_back(store_word);
    }

    return words;
}

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

    string original_word;
    string shuffled_word;
   // string random_word;
    string choose_list;
   // string HWLF = word_list(random_word, choose_list); move this to line
    string guess;

    int select;

    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;
	
    cin.ignore(50, '\n');\

    /*
    selection only dictates which file to use
    */
    if(select == 1)
    {
	cout << "Select list" << endl;
	getline(cin, choose_list);//user selected file
    }
    if(select == 2)
    {
	choose_list = "words.txt";//default file
    }

    //moved from above, get the word list 
    vector<string> words = word_list(choose_list); 
    while(game_loop != false)
    {
	//get a random word, this will repeat when guess is correct
	original_word = words[rand() % words.size()];
	shuffled_word = original_word;
        random_shuffle(shuffled_word.begin(), shuffled_word.end()); //#2

        cout << shuffled_word << endl;

        cout << "\n";

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

	    if(guess == original_word)
            {
                cout << "Is correct!\n" << endl;
                guess = " ";//you where right about this
                break;
            }
            else if(guess == "Quit" || guess == "quit")
            {
                cout << "Goodbye thanks for playing" << endl;
                return 0;
            }
            else
            {
                cout << "Is wrong! Try again\n" << endl;
            }
        }
    }
}
Last edited on
Whelp...good jumping jehoshaphat batman...I've been turning you in the complete wrong direction.

Forgive my stupidity here, it's been nothing but ignorance. I learned in my C++ course that it was a good idea to keep track of capacity and make sure it doesn't get "full", but if I'll be darned I just looked it up and Cire and naraku are absolutely correct. Vectors automatically reserve more space when the capacity is reached, so vectors getting full do not cause problems...

I believe my thought about trying to add more elements making issues was actually seated in the idea that using an iterator outside of the bounds of the vector can cause issues. My bad, sorry for the misleading info. :/
Topic archived. No new replies allowed.
Pages: 12