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

Re: Finding string characters

31 views
Skip to first unread message
Message has been deleted

Barry Schwarz

unread,
May 19, 2020, 9:24:28 PM5/19/20
to
On Tue, 19 May 2020 15:21:23 -0700 (PDT), Ain? Paplauskait?
<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

Please don't post the same message to multiple groups individually. If
your message needs to be in multiple groups, post a single message to
them by specifying the relevant groups.

--
Remove del for email

Paavo Helde

unread,
May 20, 2020, 1:05:30 AM5/20/20
to
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);
> }
>
> }*/
>
> }
>

Öö Tiib

unread,
May 20, 2020, 1:17:34 AM5/20/20
to
On Wednesday, 20 May 2020 01:21:33 UTC+3, Ainė Paplauskaitė 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'.

If you need to find position of char c in std::string s then why
do not you use string's member find() like s.find(c) ?

<https://en.cppreference.com/w/cpp/string/basic_string/find>


> There is part of the code:
>

Jorgen Grahn

unread,
May 20, 2020, 1:58:20 AM5/20/20
to
On Wed, 2020-05-20, Paavo Helde wrote:
> 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,

I bet that comes from that FLTK library or framework he's using.
That's a valid use case for void* and reinterpret_cast.

> 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.

Yes; his code should leave the C world earlier; use std::vector or
std::array instead of C arrays of strings; use std::string instead of
C arrays of char.

>> struct Dictionary
>> {
>> string Word_4[150];
>> string file_4;
>> string word_4;
>> };

I also don't understand what the _4 suffix is doing everywhere, and
why a dictionary contains 150 words, and one extra word.

Ah, I think I see now -- you sometimes play Hangman with four-letter
words. Shouldn't affect the naming of variables and members,
though.

Personally I would write classes for the core of the game and then
maybe write unit tests before I wrote the UI. Something like:

class Game {
public:
explicit Game(const string& answer);
// e.g. "_oo_ar"
void put(std::ostream&) const;
size_t size() const;
bool solved() const;
// total failed guesses
size_t mistakes() const;

// guess that the string is s; returns
// the number of errors in the guess/how much
// the hanged man should grow
size_t guess(const string& s);
};

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
0 new messages