c++ - Simple Word Guessing Game -


bool guess(char c) {     if (guesses[c])     {        guesses[] = c;        return true;     }     else if (c > ='a' && c <= 'z')     {        guesses[] = c;        return false;     } }  bool guesses[255] = {}; 

i need use see if person has enter char between - z , if haven't return true else return false. either way update guesses char. right don't understand how add char array, next time check false , tell them guessed. understand using ascii table beyond lost. explain why won't work.

i error

expected primary-expression before']'

but if take bracket out get

incompatible type char bool

which make sense how make char c mark true in boolean array

you've left brackets empty, aren't providing index:

guesses[c] = c; 

but don't want assign char guesses, you'd want assign bool:

guesses[c] = true; 

that compile* , fix problem.
* note have syntax error > =, assume copy+paste issue editor question, should fix >=. function guess can potentially not return (if neither if or else if true), undefined behaviour. should ensure control paths return value, , should make sure compile @ highest warning level warned these things.

but not design.

since you're dealing characters a-z, don't need allocate 255 elements do. minus character obtain correct index:

bool guesses[26];  if (c >='a' && c <= 'z')     guesses[c-'a'] = true; 

consider instead using std::set, container of unique elements, track whether character has been pressed:

#include <set>  std::set<char> guesses;  bool guess(char c) {     // have inserted character?     if (guesses.find(c) != std::end(guesses))     {         // character has been guessed:         std::cout << "this character has been guessed";         return true;     }     else if (c >= 'a' && c <= 'z')     {         // valid guess:         guesses.insert(c);         return false;     } } 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -