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.
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.
#include <iostream>
#include <string>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <vector>
usingnamespace 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;
}
elseif(guess == "Quit" || guess == "quit")
{
cout << "Goodbye thanks for playing" << endl;
return 0;
}
else
{
cout << "Is wrong! Try again\n" << endl;
}
}
}
}
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. :/