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");
}
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 );