1

Is putting "yes" in do while loop statement possible because I cannot figure out how. It's not working.

#include <stdio.h>
int main ()
{
    char choice [3];

    do 
    {
    
    printf ("Do you want to try again? ");
    scanf ("%s", &choice);
    
    } while (choice == "yes");

}
Harith
  • 4,663
  • 1
  • 5
  • 20
JL Minty
  • 23
  • 3
  • FYI: Hi, I voted your question down because you did not even try to indent your code. Note, here anyone can vote your question up or down as he wants to, and many of us dislike this "style". – peterh Oct 26 '22 at 10:47
  • Please show us a [mre] of the code you have problem with. – Some programmer dude Oct 26 '22 at 10:48
  • Oh I'm sorry, I'm new here in coding. I don't know what to do. – JL Minty Oct 26 '22 at 10:48
  • By the way, what have your books, tutorials or teachers said about `char` and the `%c` format specifier? You know that it's only for *one single* character, and can't be used to hold or read strings? – Some programmer dude Oct 26 '22 at 10:49
  • I edited it properly now. Thank you for you comments – JL Minty Oct 26 '22 at 10:58
  • 4
    4 Isssues: 1) `char choice[3]` cannot hold the string `"yes"`. You need an extra element for terminating 0 byte. => make it `[4]` 2) Using `scanf` to read astring without length limitation is dangerous. Use `scanf("%3s", choice)`. 3) You don't need an `&` for `choice` as it is an array that already decays to a pointer. 4) This is your actual question: You cannot compare a string with `==`. Use `strcmp` instead. – Gerhardh Oct 26 '22 at 11:02
  • You're on the right way with formatting the code, however, you should usually indent after each "{", so you should already indent `char choice [3];`. – jps Oct 26 '22 at 11:02

2 Answers2

3

The string "yes" contains 4 characters including the terminating zero character '\0'. So the character array that will accept the string shall be declared at least with 4 elements

char choice[4];

In this call of scanf

scanf ("%s", &choice);

the second argument has the type char( * )[3] (or char ( * )[4] if you will update the array declaration as shown above) instead of char *.

You should write at least like

scanf ("%3s", choice);

In the condition of the do-while statement

while (choice == "yes");

there are compared two pointers due to implicit conversions of the arrays to pointers to their first elements. So the condition will always evaluate to false because the array choice and the string literal "yes" occupy different extents of memory.

You need to use standard C function strcmp declared in the header <string.h> as for example

#include <string.h>

//...

} while ( strcmp( choice, "yes" ) == 0 );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

It’s possible to do what you’re trying (loop until “yes” is typed), but your code has a number of issues. First, your choice buffer is too small; it can’t hold the four bytes necessary for the string “yes” plus its terminating null character (and you’ll overrun it if more than two characters are entered). The conditional choice == yes is not comparing to buffers, it’s comparing the address of choice to a pointer to a string literal (turn your compiler warnings up). Use strcmp() or similar to compare strings.

Paul Lynch
  • 241
  • 1
  • 5