Friday, January 27, 2012

C++ Programming(text twist like game)?

int main()

{

int a = 0;

ifstream readWords;

readWords.open("words.txt");

string word , checker[2000] ;

string dictionary[1000];

cout%26lt;%26lt;"Enter a word: ";

cin%26gt;%26gt;word;

std::transform(word.begin(),word.end(鈥?::tolower);



while(!readWords.eof() )

{

readWords%26gt;%26gt;checker[a];

a++;

{

for(int x = 0 ;x%26lt;1900 ;x++)

if(word == checker[x])

{

cout%26lt;%26lt;"\nYou got a correct answer\n";

}

}

}

system ("pause");

return 0;

}



im creating a game(console) in c++ which is like a text twist



i had a file named "words.txt" which contains a list of words ; i tried to store each word to an array ;and check the user input if that is in the list of words(sorry for my bad explanation)



there is an error whenever i compiled this program :(



p.s. my program can already read the contents of the words.txt



im having trouble with the checking of the user input if it is in the list of words



thnx for those who will help :DC++ Programming(text twist like game)?
As was mentioned, you're searching 1900 elements of checker[ ] for each word read. I'm sure this is not what you intended to do; check your curly braces. What you really need is a better way to store the words from the file, and a more efficient way to search the dictionary. Try something like this:



#include %26lt;iostream%26gt;

#include %26lt;fstream%26gt;

#include %26lt;string%26gt;

#include %26lt;map%26gt;

#include %26lt;sstream%26gt;

#include %26lt;algorithm%26gt;

#include %26lt;cctype%26gt;



using namespace std;



void toLower(char%26amp;);

string lcStr(const string%26amp;);

const char *infileName = "words.txt";



int main(int argc, char *argv[]) {

聽 聽 string line, word, lword;

聽 聽 map%26lt;string, string%26gt; dictionary;

聽 聽 map%26lt;string, string%26gt;::iterator i;

聽 聽 stringstream ss;

聽 聽 ifstream wordFile(infileName);



聽 聽 if (wordFile) {

聽 聽 聽 聽 while (wordFile) {

聽 聽 聽 聽 聽 聽 getline(wordFile, line);

聽 聽 聽 聽 聽 聽 ss.clear(); ss.str(line); lword.clear();

聽 聽 聽 聽 聽 聽 while (ss %26gt;%26gt; word) {

聽 聽 聽 聽 聽 聽 聽 聽 lword = lcStr(word);

聽 聽 聽 聽 聽 聽 聽 聽 dictionary.insert(pair%26lt;string, string%26gt;(lword, word));

聽 聽 聽 聽 聽 聽 }

聽 聽 聽 聽 }



聽 聽 聽 聽 cout %26lt;%26lt; dictionary.size() %26lt;%26lt; " words in dictionary." %26lt;%26lt; endl;

聽 聽 聽 聽 wordFile.close();

聽 聽 聽 聽 while (true) {

聽 聽 聽 聽 聽 聽 cout %26lt;%26lt; endl %26lt;%26lt; "Enter a word to find: ";

聽 聽 聽 聽 聽 聽 getline(cin, word);

聽 聽 聽 聽 聽 聽 lword = lcStr(word);

聽 聽 聽 聽 聽 聽 if ((i = dictionary.find(lword)) != dictionary.end()) {

聽 聽 聽 聽 聽 聽 聽 聽 cout %26lt;%26lt; "'" %26lt;%26lt; i-%26gt;second %26lt;%26lt; "'";

聽 聽 聽 聽 聽 聽 } else {

聽 聽 聽 聽 聽 聽 聽 聽 cout %26lt;%26lt; "'" %26lt;%26lt; word %26lt;%26lt; "' not";

聽 聽 聽 聽 聽 聽 }

聽 聽 聽 聽 聽 聽 cout %26lt;%26lt; " in the dictionary" %26lt;%26lt; endl;

聽 聽 聽 聽 }

聽 聽 } else {

聽 聽 聽 聽 cout %26lt;%26lt; infileName %26lt;%26lt; " not found." %26lt;%26lt; endl;

聽 聽 }

聽 聽 return 0;

}





string lcStr(const string %26amp;w) {

聽 聽 string lw = w;

聽 聽 for_each(lw.begin(), lw.end(), toLower);

聽 聽 return lw;

}



void toLower(char %26amp;c) {

聽 聽 c = tolower(c);

}



#if 0



Sample run:



5 words in dictionary.



Enter a word to find: black

'Black' in the dictionary



Enter a word to find: Orange

'Orange' in the dictionary



Enter a word to find: white

'white' not in the dictionary





Where:



$ cat words.txt

Black Blue

Yellow Orange Green

Red



#endifC++ Programming(text twist like game)?
http://www.daniweb.com/

This is a better place to ask your questions on programming and please be clear with what you want your program to do..C++ Programming(text twist like game)?
Your code reads a word from the dictionary file into checker[a], then compares the search word against the first 1900 words in the checker[] array, even though it's not filled yet.



Hope that helps.

No comments:

Post a Comment