Hangman Game - Multiple countries selected.

I'm writing a Hangman game.
It's a work in progress. I'm writing each function, testing it, then moving on.
I've come across a problem.

The program chooses a country from a numbered list of countries.

1. Afghanistan
2. Albania
3. Algeria
4. Andorra
5. Argentina
...
10. Belgium
11. Bhutan
12. Botswana
13. Brunei
14. Bulgaria
15. Cambodia


A random number is generated and the country with that number is supposed to be printed. The problem that I have is if the number is "2", for example - then not only will country number 2 be printed, but also 12, 20, 21, and any other country whose number has a 2 in it. How can I fix this?

EXAMPLE OUTPUT

ubuntu@ubuntu-laptop:~/Desktop$ ./Hangman
Input Category Number:
1-Countries
2-Movies
3-Books
4-Longest Words in English
1
3. Algeria13. Brunei23. Djibouti30. Gabon31. Gambia32. Germanyubuntu@ubuntu-laptop:~/Desktop$



SOURCE 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int getRandom(); //generates the random number
int getCategory(); //asks user to choose category.
void getWord(int, int, char []);

int main()
{
	int random = getRandom();
	int category = getCategory(); 
	char word[100];  //array that stores word to be guessed
	getWord(random,category, &word);
	//...
	return 0;
}

void getWord(int random,int cat,char w[])
{
	string srandom;
	srandom=itoa(r,10);
	ifstream iFile;

	switch (cat) //load words from file of appropriate category
	{
	case 1:
		{
			iFile.open("~/Desktop/C++/Hangman/Categories/Countries.txt");
			break;
		}
	default:
			break;
	}

	string line;
	while(getline(iFile,line)) 
	{
		if(line.find(srandom) != string::npos)	
		{
			cout<<line;
		}
	}
		
}





So you have an enumerated list an want to print an specific line.
Why don't just count the lines while you are reading?
OH MY GOD! You're a Genius!
Thanks!

The thought honestly didn't even cross my mind! :$
Topic archived. No new replies allowed.