Search code examples
cloopscontinue

Continue loop not working correctly


I have two continue loops in the below program- each time when a user enters 'X' or 'x', the program is supposed to again ask the user for input, ie the program is supposed to keep working normally. However, in my program, on entering X/x, it terminates.

I would really appreciate any suggestions as to why this is happening!

#include<stdio.h>
int main(int argc, char const *argv[])
{
    char card_name[3];
    int count = 0;
    while (card_name[0] != 'X')  
    {
        printf("\n Enter the card name\n");
        scanf("%2s", card_name);
        int val = 0;
        switch (card_name[0])
        {
        case 'K':
        case 'Q':
        case 'J': val = 10;
            break;
        case 'A': val = 11;
            break;
        case 'x':
        case 'X':
            printf("\n Game ended, thanks for playing!!!\n");
            printf("\n New Game begins!!!\n");
            continue;

        default: val = atoi(card_name);
            //break;
            if ((val < 1) || (val > 10))
            {
                printf("\n Incorrect card name entered! Please try again!\n");
                continue;
            }
        }

        printf("\n The current card value is:%i\n", val);

        //Card counting
        if ((val > 2) && (val < 7))
        {
            count++;
            printf("\n The count has gone up\n");
        }
        else if (val == 10)
        {
            count--;
            printf("\n The count has gone down\n");
        }

        printf("\n The current card count is %i\n", count );
    } 
    /* code */
    return 0;
}

Solution

  • If you enter an uppercase X, your continue goes back to the loop condition;

    while(card_name[0]!='X')
    

    ...where the value is X and it promptly exits.