20.05.2020 01:21 Ainė Paplauskaitė kirjutas:
> 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:
Your code is not complete, contains references to undefined types, does
not compile and does not have a main() function. So it's hard to tell
anything about this, especially given that your only complaint is "does
not work".
My first recommendation is to get rid of all dangerous C-style
anachronisms like void* parameters, C-style arrays, strcpy(). Also raw
pointers and reinterpret_cast should not be needed in such a simple
program. At first glance it looks like the C-style
array-decay-to-pointer misfeature is the immediate cause of your
problems, so why not get rid of it and to concentrate on learning one
language, not a bad mix of two.
>
> 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
> {
> raide14->value(
zodis_4.at(0));
> }
>
> /*if (word_4[1] == (limput->sval)) //This kind of comparing does not work. How could I fix this?
> {
> raide14->value(word_4 + 1, 1);
> }
>
> }*/
>
> }
>