0
#include<stdio.h>
main()
{
    char status;
    printf("STATUS     SYMBOL\n");
    printf("------     ------\n");
    printf("Senior        s\n");
    printf("Junior        j\n");    
    while(1)
    {       
        printf("Please Enter the Salesperson's Status : ");
        scanf("%c",&status);
        if (status=='s' || status=='j')
        {
            if(status=='s')
            {
                printf("Weekly Salary is --- $400\n");
                break;
            }
            else
            {   
                printf("Weekly Salary is --- $275\n");
                break;
            }
        }
        else
        {
            printf("Please Enter a Valid Symbol!\n");

        }
    }
}

When an Invalid data is entered,this program must ask user for a valid data.The While Loop used for validating the Data executing Two times,in order to ask the user for Input. Why the scanf function is being ignored on the First-Go? How to solve this Problem? Program Output

1 Answers1

0

Because when you type a<ENTER> you type 2 characters. So the first scanf get the a, the second one gets the \n (ENTER).

You can change your scanf to get 2 characters instead of one.

scanf("%c%c", &status, &newline);
SegFault
  • 1,097
  • 1
  • 14
  • 14