On Tue, 19 May 2020 15:25:26 -0700 (PDT),
ainepapl...@gmail.com
wrote:
>Hello! I am making Hangman game with FLTK. I have a problem - I would like my function to compare each letter of the string word_4 to a certain character(entered from input widget). For example, my string word is "sun" and I would like to compare the first letter with the entered letter(from input). So I need to compare 's' with string 's'.
>There is part of the code:
>
>struct Dictionary
>{
> string Word_4[150];
> string file_4;
> string word_4;
>};
>
>struct Info
>{
> Fl_Input* instr;
> char sval[40];
>};
>
>void readLetter_4(string Word_4[], string &word_4){
>
> vector<string> words;
> ifstream file("letter_4.txt");
> string line;
> if (!file)
> printf("File does not exist");
> else
> while (getline(file, line))
> {
> words.push_back(line);
> srand(time(0));
> }
>
> word_4 = words[rand() % words.size()];
> file.close();
>}
>
>void showLetters_cb(Fl_Widget* w, void* param) {
>
> string word_4; string Word_4[150];
> Info* input= reinterpret_cast<Info*>(param);
> strcpy(input->sval, input->instr->value());
> readLetter_4(Word_4, word_4);
>
> if (
word_4.at(0) == (input->sval)) //this does not work
word_4 is a string (of char).
word_4.at(0) is the first char in that
string.
input->sval is an array of char. In this context, the expression is
converted to the address of the first element of the array with type
char*.
You if statement asks the question "Is the first char of word_4 equal
to the address of the first char of input->sval." They will never be
equal.
So what we need to know is do you want to compare the first char of
word_4 with the first char of input->sval OR do you want to determine
the fist char or word_4 appears anywhere in input->sval.
If the former, then add a subscript to input->sval. If the latter,
consider using strchr or something similar.
> {
> raide14->value(
zodis_4.at(0));
All of these names are a mystery.
> }
>
> /*if (word_4[1] == (limput->sval)) //This kind of comparing does not work. How could I fix this?
What is limput? It appears to be a typo. When posting code, do not
retype; use cut and paste! Other than the fact that it uses the
second char or word_4, this is identical to the previous if statement.
Unless x is out of range, str[x] and
str.at(x) are equivalent.
> {
> raide14->value(word_4 + 1, 1);
> }
>
> }*/
>
>}
--
Remove del for email