C: How can I redesign the program so that I can answer "Yes" instead of "Y"?

I now have this code:

 int main() { char wand[2]; do { printf("Ist die Wand da? (Ja | Nein)\n"); scanf("%s", wand); } while (wand[0] != 'y' && wand[0] != 'Y'); printf("Drehung\n"); printf("Schritt!!\n");

But I'd like to answer yes instead of Y. How do I do that?

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
10 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
QLQuadratAchtel
1 year ago

Hello Daniel,

Don’t let yourself be on any grumpy “experts” that throw anything into the room and don’t show a centimeter of understanding for beginners.

here is a snippet which should clarify your actual question:

while (strcasecmp(wand, "ja") != 0 && wand[0] != 'y' && wand[0] != 'Y');

However, there is the problem that “char wand[2]” has only space for a single character and no zero-byte. So if you try to read a string with two characters like “Yes” into this array, the zero-byte is written outside the array, which can lead to errors.

Please also change the “char wand[2]” to “char wand[4]”

I also ask you that if you have serious intentions to learn a programming language, that you also think about what you actually want to achieve with it. All programming languages are good for a bit. In addition, I can see from your comments that you have little understanding of what you ask us. Oriente to any Youtube playlists, books or w3school.

Have fun!

Destranix
1 year ago

You make the array “wall” sufficiently large (complete zero character note) and then compare the string with strncmp.

Destranix
1 year ago
Reply to  DANIELdjldqwj

What don’t you understand?

Destranix
1 year ago

Like “he”? You can write any of the functions you have.

Destranix
1 year ago

As many as are written.

Destranix
1 year ago

You’ll make it big enough by giving a proper greeting to the array’s defionition.

strncmp is a library function. See:

https://pubs.opengroup.org/onlinepubs/000095399/functions/strncmp.html