Ok so in my code im trying to beable to let the user enter the name of a list in main then pass the string to word_list function but my program just keeps saying file failed to open. If you spot any other problems please help me with them ive been working on this program for 4 days and im so frustrated.

#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());

            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;
                }
            }
        }
    }
}

Here you're calling word_list but passing choose_list and random_word which aren't initialized.

string random_word;
string choose_list;
string HWLF = word_list(random_word, choose_list);

Edited 3 Years Ago by tinstaafl

so what do i do? Im sorry im really slow when it comes to programming.

ok i got it never mind sorry i did this

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

string HWLF = word_list(random_word, choose_list);

Now another problem i have which has been annoying me for a while is when i enter the right word it just keeps giving me the same word.

Edited 3 Years Ago by Chay Hawk

ok here are some words from the list

america
zebra
tiger
animals
discombobulate
trampoline
tennis
transylvania
undertaker
morgue
vacuum
teeth
trapper
unconventional
contacting
unspecified

Well the program does everything correctly except when i enter the word correctly in the program it just scrambles up the same word and gives it to me, when it should randomly select another word from the list, scramble that up and give it to me to guess again.

Ok try this:

#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);

    if(select == 2)
    {
        while(game_loop != false)
        {
            string HWLF = word_list(random_word, choose_list);
            shuffled_word = HWLF;
            random_shuffle(shuffled_word.begin(), shuffled_word.end());
            cout << shuffled_word << endl;
            cout << "\n";
            bool done = false;
            while(!done)
            {
                cout << "Your guess: ";
                cin >> guess;
                if(guess == HWLF)
                {
                    cout << "Is correct!\n" << endl;
                    done = true;
                }
                else if(guess == "quit")
                {
                    cout << "Goodbye thanks for playing" << endl;
                    done = true;
                    game_loop = false;
                }
                else
                    cout << "Is wrong! Try again\n" << endl;
            }

        }
    }
}
This article has been dead for over six months. Start a new discussion instead.