Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: How to find string character?

21 views
Skip to first unread message
Message has been deleted

Barry Schwarz

unread,
May 19, 2020, 9:21:29 PM5/19/20
to
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
Message has been deleted

Francis Glassborow

unread,
May 20, 2020, 6:41:04 AM5/20/20
to
On 20/05/2020 10:14, ainepapl...@gmail.com wrote:
> Sorry. I am using my native language for variable names, so I changed them to English before posting and I accidentally missed some variables. raide14 is the output name that appears later in the code. My code is long, I just put few parts of the code, so it may be difficult to understand what is what.
>> {
>> raide14->value(word_4.at(0)); \
>
> limput is a typo, it has to be input. Like this:
>> if (word_4[1] == (input->sval))
>
>
> So input->sval gives me value from FL_input widget (if person types 'a', I get the value 'a', but value form FL_input is a string. So that means I can't compare char with string). So I need to convert somehow one letter from word_4 to a string, because char and string comparing is not allowed.
>
> To explain the situation : a person enters a letter, if the word_4 has that entered letter inside the word, then it will be shown in the output.
> So basically, I have a word and I need to take every letter from it and compare to entered letter.
> (input->sval) is a string and for example, word_4[2] is not. So I need to make word_4[2] a string somehow.
>
>
> if (word_4[2] == (input->sval))
> {
> raide14->value(word_4 + 2, 1);
> }
>
> if (word_4[3] == (input->sval))
> {
> raide14->value(word_4 + 3, 1);
> }*/
>

You have it backwards. You need to extract a char from the string not
convert char into a string. Both are so trivial that this has a smell of
homework which we do not do.
Francis
0 new messages