[Warning] It not the completed code. its just a rough idea

do{
printf("Please enter a number :")
scanf("%d", &no);
if (no <=0)
printf("invalid");
}while(no<=0);

Hi Guys!

I've a issue with scanf.

First loop the program. I entered '1', a valid input. Once i retry the program, i entered a value example 'abcwad'. It is confirm invalid. But the scanf will use back the first loop of the program value of '1'

any solution?

scnaf with %d will only accept numeric data -- anything else is ignored and scanf() does not change the value of the input parameter. Check the return value to see if scanf() successfuly converted any data.

Oh. If i typed none numeric data, scanf will use back the first loop program value.. any other solution to solve this?

Get the input data as a string, then your program can check its contents to see if it's numeric data or not. Since you are posting in the c++ forum I'd suggestion using cin and std::string, not scanf() or fgets() which are from C language.

do
   {
      fputs("Enter an integer (0-100): ", stdout);
      fflush(stdout);
   } while ( !mygeti(&value) || value < 0 || value > 100 );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.