Michael A
Michael A

Reputation: 3

How to end the program when a user enters "quit"

How can I end the program when a user enters "quit"? I tried an if statement before the loop and within the loop. I'm pretty new to programming and it's bothering me that I can't figure this out. I even tried a do while loop and that didn't work at all.

int main()
{
    char word[30];
    char yn;
    int loopcount;
    yn=0;
    loopcount=0;
    printf("Enter a word:");
    scanf(" %s", word);
    printf("Would you like to change the word? y=yes, n=no \n");
    scanf(" %c", &yn);
    if (yn=='n')
    {
        return 0;
    }

    while(yn>0)
    {
        printf("Enter a new word: \n");
        scanf(" %s", word);
        printf("New word is: %s \n");
        loopcount= loopcount+1;
        printf("You have changed the word %d times.\n", loopcount);
        printf("Would you like to change the word? y=yes, n=no\n");
        scanf(" %c", &yn);

        if (yn=='n')
        {
            return 0;
        }


        return 0;
    }

    return 0;
}

Upvotes: 0

Views: 1425

Answers (1)

Barmar
Barmar

Reputation: 782653

Use strcmp()

scanf(" %s", word);
if (strcmp(word, "quit") == 0) {
    return 0;
}

Upvotes: 3

Related Questions