hangman definition game trouble

closed account (L092y60M)
Basically I have a text file called words. I'm supposed to extract a word randomly form the file and have the user guess the word. If they guess the word correctly in x number of tries they will receive the definition.

I'm having trouble receiving that random word and I'm getting the definitions from the file.

Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>  
#include <stdlib.h>
#include <time.h>

using namespace std;

int main(){
	int number;
	int count = 0;
	string word;
	ifstream inFile;

	inFile.open("words.txt");
	
	srand(time(0));
	while (count < 1){
		number = rand() % 10 + 1;
		count++;
	}
	for (count = 0; count < number; count++)
	{
		getline(inFile, word, '#');
	}
	cout << word << endl;
	cout << number << endl;

}


This is what is in the words.txt file
apple#the usually round, red or yellow, edible fruit of a small tree
boat#a vessel for transport by water
horse#a solid-hoofed plant-eating domesticated mammal with a flowing mane and tail, used for riding
television#a system for transmitting visual images and sound that are reproduced on screens
soup#a liquid dish, typically made by boiling meat, fish, or vegetables, etc.
bottle#a container, typically made of glass or plastic and with a narrow neck
barber#a person who cuts hair
toast#sliced bread browned on both sides by exposure to radiant heat
radar#a system for detecting the presence, direction, distance, and speed of aircraft, ships, and other objects
red#of a color at the end of the spectrum next to orange and opposite violet
closed account (L092y60M)
I think I've got something here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main(){
	int number;
	int count = 0;
	string word;
	ifstream inFile;

	inFile.open("words.txt");
	
	srand(time(0));
	while (count < 1){
		number = rand() % 10 + 1;
		count++;
	}
	for (count = 0; count < number; count++)
	{
		getline(inFile, word);
		getline(inFile, word, '#');
	}
	cout << word << endl;
	cout << number << endl;

}


But the only error I'm having now is that the last definition is not giving me the word but the whole line.

Please let me know if I'm doing something wrong.
Line's 10 through 13 are equivalent to the single line:
number = rand() % 10 + 1;

Given the format for the file, lines 16 and 17 should be reversed. If you want 'word' to actually contain the word before the '#', then after those lines are switched you should ensure line 17 isn't executed on the last iteration of the loop.

Alternately:

1
2
3
4
5
    for ( int count = 0; count < number; ++count )
    {
        getline(inFile, word, '#');    // get the word.
        inFile.ignore(1000, '\n');     // ignore the definition.
    }
closed account (L092y60M)
@cire

Thanks so much for the input, it worked correctly.

One more quick question if you don't mind. Now how would I match the word to the definition, so I can extract the definition and play the hangman game.
Topic archived. No new replies allowed.