getting words from .txt file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 10
Reputation: cjm771 is an unknown quantity at this point 
Solved Threads: 0
cjm771 cjm771 is offline Offline
Newbie Poster

getting words from .txt file

 
0
  #1
Aug 21st, 2005
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!
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 36
Reputation: Raven11 is an unknown quantity at this point 
Solved Threads: 1
Raven11 Raven11 is offline Offline
Light Poster

Re: getting words from .txt file

 
0
  #2
Aug 21st, 2005
If you want to use the fstream class then I suggest this link:
http://www.cplusplus.com/ref/iostrea...eam/index.html
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,681
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: getting words from .txt file

 
0
  #3
Aug 21st, 2005
>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:
  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:
  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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 10
Reputation: cjm771 is an unknown quantity at this point 
Solved Threads: 0
cjm771 cjm771 is offline Offline
Newbie Poster

Re: getting words from .txt file

 
0
  #4
Aug 21st, 2005
um my word is actually

char word[15];

because i dont have string headers and vector headers.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,681
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: getting words from .txt file

 
0
  #5
Aug 21st, 2005
>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?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: getting words from .txt file

 
0
  #6
Aug 21st, 2005
I suppose you'll run into problems if the word ends up being "superconductors", or "Lackawannanians", or "indistinguishable", or "incomprehensible".
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 10
Reputation: cjm771 is an unknown quantity at this point 
Solved Threads: 0
cjm771 cjm771 is offline Offline
Newbie Poster

Re: getting words from .txt file

 
0
  #7
Aug 21st, 2005
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: getting words from .txt file

 
0
  #8
Aug 21st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 10
Reputation: cjm771 is an unknown quantity at this point 
Solved Threads: 0
cjm771 cjm771 is offline Offline
Newbie Poster

Re: getting words from .txt file

 
0
  #9
Aug 21st, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC