1

Whenever I run this segment of my code, if I input anything but a number, it continues looping and prints many "Please enter a number" without stopping to prompt another input. Is scanf somehow reading what printf is printing?

int size;

printf("How many numbers would you like to store?\n"); while(1) {
if(scanf("%d", &size))  {
    printf("WORKING\n");
    break;  
}
else {
    printf("Please enter a number\n");
}

The code is meant to only accept an integer as an input, but for some reason when you input something but an integer it continues to loop without another prompt.

user438383
  • 5,716
  • 8
  • 28
  • 43
Ryan Moore
  • 11
  • 2
  • `scanf()` leaves the invalid input in the input-buffer(`stdin`). You need to clear it before another call. Say call `char buff[256]; fgets(buff, sizeof(buff), stdin);` at the end of `while()`. – जलजनक Apr 01 '22 at 16:56
  • *"Is scanf somehow reading what printf is printing?"* - Have you tried printing `size`? – klutt Apr 01 '22 at 16:56

0 Answers0