vendredi 21 juillet 2017

For loop with IF statement C programming

char userChoice;

printf("Choose how you would like to search.\nEnter A to display all players information.\
            \nEnter N to search a player based on name.\nEnter J to search a player based on jersey number.\
            \nEnter P to search a player based on position.\nEnter your choice: ");
scanf("%c", &userChoice);

do
{
    if (userChoice == 'A' || userChoice == 'a')
    {
        for (index = 0; index < count; index = index + 1)
        {
            displayPlayers(&players[index]);
        }
    }

    if (userChoice == 'J' || userChoice == 'j')
    {
        int jerseyNumber;

        printf("\nEnter jersey number for the player: ");
        scanf("%i", &jerseyNumber);

        for (index = 0; index <= MAX_PLAYERS; index++)
        {
            if (jerseyNumber == players[index].jerseyNumber)
            {
                // If the condition is met the singleDisplay function is called.
                // Containing the array of struct
                singleDisplay(&players[index]);

            }
        }
    }
    if (userChoice == 'N' || userChoice == 'n')
    {
        char playerName[LEN_NAME + 1];

        printf("\nEnter name for the player: ");
        scanf("%s", playerName);

        for (index = 0; index <= MAX_PLAYERS; index++)
        {
            if (strcmp(playerName, players[index].firstName) == 0)
            {
                singleDisplay(&players[index]);

            }
        }
    }

Most of this code is just for context,the problem I am having is not being able to make an else statement that outputs a message to user that jersey they entered was not found. The problem is the else statements is inside the loop and will print its message no matter what multiple times, while it is comparing all numbers in array.

Aucun commentaire:

Enregistrer un commentaire