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.