I'm still working on the same problem.
Im doing a text-twist code for c++ as part of our school requirement.
Last time, I'm having problems on delaying outputs using CLOCKS_PER_SEC and clock(), but thanks to some people, I solved that problem already.

Now, Im working on the game's play mode.
I SHOULD have a text file that contains all the words available/ all the words that can be used for playing my text-twist program. Right now, Im only using a substitute for it, ex: i put some text on the txt file just so i can test if the getline works.

So... the problem is getline.
Here's a part of my code

string getword(){
        /*this function will
        get a word from set of 50 words by using rand and seekg
        --use srand and rand to generate a number from 0-49
          --resulting number will be saved to int wordnum
        --wordnum then will be multiplied to a fixed number 20, which is the length or each line in the file "wordpool.txt"
            --take note that each line in the file contains a word and all its possible combinations, there are exactly 50 lines in the file, one line for each word.
        --using seekg, the cursor will be moved to the position of the word
        --getline will isolate that line (line that contains the word and all its combinations) into an array called "wordset"
        --file will be closed since leaving it open will not be necessary
        --and because the seven letter word comes first in the line, its easy to isolate it into another array called "word" by using substr.
        --lastly, since function playingproper1() called this function, and this function calls no other function, control will go back to playing proper.
        --return string wordset and word*/
        srand(time(0));
        int wordnum;
        //wordnum=(rand()%51);

        wordnum=0;//temporary value used for testing purposes. this will move pointer to the beginning of file... even if its not necessary .......delete this after getting official word list

        openWP.seekg(20*wordnum, ios::beg);

        string wordset="yes?";//tester for cout

getline(openWP,wordset);

        openWP.close();
cout<<wordset;//to test if the value of wordset changed after getline
        string word;
        word=wordset.substr(0,7);

        return wordset,word;
}

the explanation for the contents of the code are included there...

As you can see, i need to randomly access the file "wordpool.txt" so i can locate the words and the word combinations properly.
The constant 20 is the width of each line in the substitute txt file (because i dont have the actual words yet)

Let's have a substitute txt file named "wordpool.txt".
Open it then type the words "twister" and on the next line "elixir".
Save it then close it.

Run the code then see what you get.
When I run the code, I only see "yes?" which means the getline isnt working.

I heard that i maybe i can use flush, but i dont know how to use it~


NOTE about the code:
*the part of code for opening the file is in the function that called that(string getword() ) function...
*the program seems to ignore the getline part? why doesnt it work?
*and when does getline need flush? how to use flush, and how does flush work???
*fileobject is openWP

NOTE about my knowledge in C++
*im a beginner in coding
*i dont know anything about buffers

Please help me, thank you! And if ever you need the rest of the code, please just let me know.

why are you randomly accessing the file? if you are going to use the whole file i believe it would be faster to read the whole file into an array and then access the array at random points.

string array[256];  //  just a guess use whatever number you need to fit all of the words;
int i = 0;
fstream fin("test.txt"); // <-  this line opens the file
if (fin.fail())
{
       cout << "unable to open file.";
       return 0;
}
while (getline(fin, array[i]))
       i++;
fin.close()

with this you just access the array with your random number instead of accessing the file every time. also i don't see you opening the file associated with openWP so that is probably your problem

why are you randomly accessing the file? if you are going to use the whole file i believe it would be faster to read the whole file into an array and then access the array at random points.

string array[256];  //  just a guess use whatever number you need to fit all of the words;
int i = 0;
fstream fin("test.txt"); // <-  this line opens the file
if (fin.fail())
{
       cout << "unable to open file.";
       return 0;
}
while (getline(fin, array[i]))
       i++;
fin.close()

with this you just access the array with your random number instead of accessing the file every time. also i don't see you opening the file associated with openWP so that is probably your problem

the opening of the file is in another function that calls that function~
and I dont think using the array will be convenient because the words will consume too much memory..
the words should be in the file so i could edit the file without using the program, and I dont need to load the array everytime i open the program...

the program seems to ignore the getline part? why doesnt it work?

I'd suspect that openWP.seekg(20*wordnum, ios::beg) causes that to happen. If you seek to a position that's out of range, then getline() will fail.

You can see it for yourself by adding some output, for example

// snip ...

openWP.seekg(wordnum, ios::beg);
string wordset="yes?";

getline(openWP,wordset);

// what is the state of the stream?
cout  << boolalpha 
        << "openWP.eof() = " << openWP.eof() << endl
        << "openWP.bad() = " << openWP.bad() << endl
        << "openWP.fail() = " << openWP.fail() << endl;

Then the return statement is definitely wrong, you can return only a single value from a function, so change to

// return the word ...
return word;

the opening of the file is in another function that calls that function~
and I dont think using the array will be convenient because the words will consume too much memory..

For 50 words as described memory should not be even close to being an issue I have a code that loads WordNet every time it runs which is 500, 000 words ~ 30 seconds including look-ups and indexing.

If you load the words into a container from a file. You can edit the
file and just get the words you want. How often will you be loading the file not very it is unlikely that the number of words that you are talking about would even register in time.

so if you can control or know the format of the file you just edit the file and the program will load no problem.

if you use a vector it does not matter how many words are in your array the random number can take the vector .size()

#include <vector>
#include <string>
#include <fstream>

std::vector<std::string> words;
//load the words once 
std::ifstream word_file("word_file.txt");
/*
now you can get line until word size == 0
*/
/*this code I can see is  above so I will abreviate
if(fin.open())
{
int count(0), max_count(10000); //limit to 10000 words
while(count < max_count)
{
std::string word = fin.get_line();
if(word.size() == 0)
{
//empty line so end of words
break;
}
else
{
//add word to the list
words.push_back(word);
}
++count;
}
fin.close();
}
*/
int number_of_words = words.size();
//now you can get the word
int current_index = generate_random_number(number_of_words);
std::string random_word = words[current_index];

This code is obviously not complete and needs filling on file open
but this then lets you edit the file between runs
and change words. Just don't leave empty lines if you need to you can reload during run time if you want to edit the data while running.

the words should be in the file so i could edit the file without using the program, and I dont need to load the array everytime i open the program...

Unless you have a design limitation an init() method to load words from a small text file is a small overhead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.