| | |
getting words from .txt file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 10
Reputation:
Solved Threads: 0
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!
. 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!
•
•
Join Date: Mar 2005
Posts: 36
Reputation:
Solved Threads: 1
If you want to use the fstream class then I suggest this link:
http://www.cplusplus.com/ref/iostrea...eam/index.html
http://www.cplusplus.com/ref/iostrea...eam/index.html
>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:
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:
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:
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 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)
ifstream in ( "somefile" ); vector<string> words; string word if ( !in ) return; while ( in>> word ) words.push_back ( word ); return words[rand() % words.size()];
C++ Syntax (Toggle Plain Text)
ifstream in ( "somefile" ); string word; if ( !in ) return; int r = rand() % 10000; // Assuming 10,000 words in the file do in>> word; while ( --r >= 0 ); return word;
C++ Syntax (Toggle Plain Text)
ifstream in ( "somefile" ); string word, survivor; in>> survivor; while ( in>> word ) { if ( rand() < RAND_MAX / 2 ) survivor = word; } return survivor;
I'm here to prove you wrong.
•
•
Join Date: Nov 2004
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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?
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <stdlib.h> //#include <conio.h> #include <stdio.h> #include <fstream.h> #include <time.h> #include <iomanip.h> main(){ ifstream in ( "hangword.txt" ); char word[15]; if ( !in ) return 0; int r = rand() % 5+0; // Assuming 10,000 words in the file do in>> word; while ( --r >= 0 ); cout<< word; system("pause"); return 0; }
•
•
Join Date: Nov 2004
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
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.
![]() |
Similar Threads
- Capturing data from txt file using JavaScript/HTML (JavaScript / DHTML / AJAX)
- how to move to the second line in C++ .txt file reading? (C++)
- Search a txt file for words (C++)
- Password Protection from a txt.file (HTML and CSS)
- How can I read from a txt file? (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- uploading .txt file via phpmyadmin to Mysql (MySQL)
Other Threads in the C++ Forum
- Previous Thread: Passing char * to function and populating inside
- Next Thread: Moving avatars
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






