Reputation: 57
I do not understand why nothing is showing up on the screen when I run this little program? Is the while loop even starting?
#include <stdio.h>
#include <string.h>
int main()
{
char word[20];
char TryAgain[20] = "y";
int WordLength = 0;
while (strcmp(TryAgain, "y"))
{
printf("\t\t\t Word \n\n");
printf("What is your word, bird?\n");
scanf("%s", word);
getchar();
WordLength = strlen(word);
printf("Result: ");
for (int i = 0; i < WordLength; ++i)
{
printf("%c*", word[i]);
}
printf("\nWould you like to try another word? [y/n]\n");
scanf("%s", TryAgain);
}
getchar();
return 0;
}
Upvotes: 2
Views: 75
Reputation: 2340
I prefer to change it to a do-while
do {
printf("\nWould you like to try another word? [y/n]\n");
scanf("%s", TryAgain);
} while (TryAgain=="y" || TryAgain=="Y");
the TryAgain must be boolean. see ya
Upvotes: 0
Reputation: 605
strcmp
returns 0 if its arguments are the same, so at the first iteration your while loop is checking while(0)
. 0 is equivalent to false
in boolean checks so the while loop is skipped. Try while(!strcmp(TryAgain, "y"))
.
Upvotes: 5