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$
#include <iostream>
#include <fstream>
#include <cstdlib>
usingnamespace 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;
}
}
}