Hello i'm trying to use a do-while loop to check the input and repeat the prompt until the user types in a correct integer. So that's my code:
#include <iostream>
#include <stdio.h>
#include <ctype.h>
int main ()
{
int a;
do
{
printf("Please type in your number: ");
}while(scanf_s("%d", &a) == 0);
std::cin.get();
std::cin.get();
return 0;
}
Well it seems to work. When I type in a number the program runs correctly. But when I type in a letter an infinite loop starts. Sincerly I don't know where the problem is.
scanfwill keep trying to scan for anint, but a letter will make it stop, and that letter will remain in the buffer until it's consumed/flushed. A common issue - stackoverflow.com/questions/1716013/scanf-causing-infinite-loopvisual-c++because of use of the Microsoft specificscanf_sfunction.std::cin::getisgetchar