Home > OS >  The scan function isn't taking values in... It is just displaying the print function
The scan function isn't taking values in... It is just displaying the print function

Time:12-22

void kmmil()
{

    int x, y;
    printf("a.KM TO MILS\n");
    printf("b.MILS TO KM\n");
    char c;
    scanf("%c", &c);
    printf("this is the value %c", c);
}

output:

this is the value (blank)

end;

CodePudding user response:

If the scanf "doesn't work" it usually does but not in a way we intended to. If you have another scanf in your code, that for example scans a number. You type in your terminal something like 123 and hit enter to confirm. That enter however stays in the input and waits to be scanned by something. This is probably why your scanf "doesn't work". Onto the solution to that:

I suggest you clear your input. You can do this in at least

scanf("%*[^\n]"); // scan and delete everything until newline character
scanf("%*c"); // scan and delete one more character (newline)

or

while(getchar() != '\n'); // get characters until newline (note that newline is deleted this way too!) 
  • Related