944,094 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12246
  • C++ RSS
Aug 21st, 2005
0

getting words from .txt file

Expand Post »
ok so last time I was here, I was programmin tic-tac-toe and its great. Now I have programmed hangman! . Anyways right now its for two players only. So I want it so the computer will choose a random word from a text file and assign that to my string called word. I know it requires ifstream but cant get it to work. If someone could write out the necessary code, that would be great! My txt file is called "hangtext.txt."

Looking at my actual hangman code is not necessary. All you have to know is that a random word needs to be selected from a text file (one word per line in the txt file, in case that affects anything), and set equal to my string word. I am much appreciative. thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjm771 is offline Offline
10 posts
since Nov 2004
Aug 21st, 2005
0

Re: getting words from .txt file

If you want to use the fstream class then I suggest this link:
http://www.cplusplus.com/ref/iostrea...eam/index.html
Reputation Points: 11
Solved Threads: 1
Light Poster
Raven11 is offline Offline
36 posts
since Mar 2005
Aug 21st, 2005
0

Re: getting words from .txt file

>If someone could write out the necessary code, that would be great!
I'm sure it would be, but that's not how it works. You write the code, and we help you with it. Too many people show up expecting us to write everything for them, and that's not the point of this forum.

>I want it so the computer will choose a random word from a text file
This is a surprisingly interesting problem, and you can go about it several different ways. If the file is small enough, you can read it into fast memory, generate a random number in the range of [0..N) where the file contains N words, then pick the one at that index:
C++ Syntax (Toggle Plain Text)
  1. ifstream in ( "somefile" );
  2. vector<string> words;
  3. string word
  4.  
  5. if ( !in )
  6. return;
  7.  
  8. while ( in>> word )
  9. words.push_back ( word );
  10.  
  11. return words[rand() % words.size()];
Of course, that's not memory efficient and if the file is a full dictionary listing then it's not practical either. If you know how many words are in the file then you can generate the random number first, and read that many words, returning the most recent word when the random number reaches 0:
C++ Syntax (Toggle Plain Text)
  1. ifstream in ( "somefile" );
  2. string word;
  3.  
  4. if ( !in )
  5. return;
  6.  
  7. int r = rand() % 10000; // Assuming 10,000 words in the file
  8.  
  9. do
  10. in>> word;
  11. while ( --r >= 0 );
  12.  
  13. return word;
Of course, even though that is memory efficient, it assumes that you know how many words are in the file. If you don't know this, you need to make a guesstimate. One popular solution is to read the entire file, and randomly select a survivor of two or more words:
C++ Syntax (Toggle Plain Text)
  1. ifstream in ( "somefile" );
  2. string word, survivor;
  3.  
  4. in>> survivor;
  5.  
  6. while ( in>> word ) {
  7. if ( rand() < RAND_MAX / 2 )
  8. survivor = word;
  9. }
  10.  
  11. return survivor;
You can change the weighting of whether the survivor is modified by changing the 2 in RAND_MAX / 2 to another constant, or even a variable that changes as more of the file is read. This is a flexible technique.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 21st, 2005
0

Re: getting words from .txt file

um my word is actually

char word[15];

because i dont have string headers and vector headers.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjm771 is offline Offline
10 posts
since Nov 2004
Aug 21st, 2005
0

Re: getting words from .txt file

>because i dont have string headers and vector headers
So use that instead of a string and an array instead of a vector. Is it really so hard to translate my example into something that works for you?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 21st, 2005
0

Re: getting words from .txt file

I suppose you'll run into problems if the word ends up being "superconductors", or "Lackawannanians", or "indistinguishable", or "incomprehensible".
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Aug 21st, 2005
0

Re: getting words from .txt file

Quote originally posted by Narue ...
>because i dont have string headers and vector headers
So use that instead of a string and an array instead of a vector. Is it really so hard to translate my example into something that works for you?
well i try that but it always comes up with an error sych as "23 invalid conversion from `char*' to `int' ". It's not hard to translate, It just doesn't work for me. Then i changed return word; to cout<<word; because I assumed they did the same thing. And it works, but doesnt print a random word, it prints one specific word off the list. Heres what i got.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <stdlib.h>
  3. //#include <conio.h>
  4. #include <stdio.h>
  5. #include <fstream.h>
  6. #include <time.h>
  7. #include <iomanip.h>
  8.  
  9.  
  10. main(){
  11. ifstream in ( "hangword.txt" );
  12. char word[15];
  13.  
  14. if ( !in )
  15. return 0;
  16.  
  17. int r = rand() % 5+0; // Assuming 10,000 words in the file
  18.  
  19. do
  20. in>> word;
  21. while ( --r >= 0 );
  22.  
  23. cout<< word;
  24. system("pause");
  25. return 0;
  26. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjm771 is offline Offline
10 posts
since Nov 2004
Aug 21st, 2005
0

Re: getting words from .txt file

You haven't used srand to seed the random number generator with say, the current time, so the random number generator outputs the same sequence of numbers every time you run the program.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Aug 21st, 2005
0

Re: getting words from .txt file

Quote originally posted by Rashakil Fol ...
You haven't used srand to seed the random number generator with say, the current time, so the random number generator outputs the same sequence of numbers every time you run the program.
thank you again rashakil Fol, you have helped me once again. And thank you Narue. And like I said I have hangman already written up and wasn't just asking for the code, I was confused about a piece of it and when I asked fori t to written out, I meant just that piece so I can understand. Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cjm771 is offline Offline
10 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Passing char * to function and populating inside
Next Thread in C++ Forum Timeline: Moving avatars





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC