Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to prevent the Error of input character with scanf() ? (C)

scanf() c error debug

Best Answer MPaunov , 18 October 2015 - 09:39 AM

Yes, I agree. getchar() is the better option.

Thank's for the correction!

 

For future readers, the above function should look like this:

int readNumber()
{	
	int number;
	int reads = scanf("%d", &number);
	while(reads != 1)
	{
		getchar();
		reads = scanf("%d", &number);
	}
 
	return number;
}
Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 yayapipi

yayapipi

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 09 October 2015 - 06:50 AM

 How to prevent the Error of input character with scanf() ?

 

I simply code a similar example of problem I confusing below :

#include <stdio.h>
int main(){
  int digit[10];
  int x;
  
  for(x=0;x<=10;x++){
printf("\nPlease Enter Number[x]: ",x+1);
scanf("%d",&digit[x]);
printf("%d\n",digit[x]);
}
}

The code is work well if the scanf input is a digit.

The problem is when a character or other an undigit things is input , the code crush.

I had search this problem in Google , and I found that fflush() is work , ya ,it clear the value of scanf if get error .

But the problem is fflush() is just work on some platform , it does'nt work on Linux or even Unix system.

So , I just wonder how to fix this problem ?

 

:thumbup: Thanks For Help :thumbup:

 

 

 

 



#2 MPaunov

MPaunov

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 10 October 2015 - 12:24 AM

Hi :)

 

You need to do a validation. First read the input and check if it was read successfully. If yes - continue, if not - ask again for a number.

 

You can do the validation, using scanf. The scanf function returns the number of successfully assigned values. In your case it will return 1 if it read successfully a number and 0 if it did not read the number.

 

If scanf does not read successfully a number, you need to do a "dummy" read that will move the input pointer to the new characters, otherwise it would enter an infinite loop, trying and failing to read the same character.

 

Here is an example of a function that reads an integer:

#include<stdio.h>

int readNumber();

int main(void)
{
	printf("The number is %d\n", readNumber());
	
	return 0;
}

int readNumber()
{	
	int number, reads;
	char dummy;
	reads = scanf("%d", &number);
	while(reads != 1)
	{
		scanf("%c", &dummy);
		reads = scanf("%d", &number);
	}

	return number;
}



#3 yayapipi

yayapipi

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 10 October 2015 - 08:09 AM

 

Hi :)

 

You need to do a validation. First read the input and check if it was read successfully. If yes - continue, if not - ask again for a number.

 

You can do the validation, using scanf. The scanf function returns the number of successfully assigned values. In your case it will return 1 if it read successfully a number and 0 if it did not read the number.

 

If scanf does not read successfully a number, you need to do a "dummy" read that will move the input pointer to the new characters, otherwise it would enter an infinite loop, trying and failing to read the same character.

 

Here is an example of a function that reads an integer:

#include<stdio.h>

int readNumber();

int main(void)
{
	printf("The number is %d\n", readNumber());
	
	return 0;
}

int readNumber()
{	
	int number, reads;
	char dummy;
	reads = scanf("%d", &number);
	while(reads != 1)
	{
		scanf("%c", &dummy);
		reads = scanf("%d", &number);
	}

	return number;
}

 

Wow, This Code Work !!!

Thanks for helping :biggrin: , you are awesome !



#4 AceInfinity

AceInfinity

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 313 posts

Posted 18 October 2015 - 01:33 AM

I would not suggest doing this to junk the bad characters:
scanf("%c", &dummy);
1. The format string needs additional parsing before it knows to extract a char from stdin
2. EOF isn't checked here

What's wrong with a function like getchar()? Less taxing on the CPU and no need to place the value in a variable.

Microsoft MVP (2012) - .NET Programming | ®Crestron DMC-T Certified Programmer
Posted Image


#5 MPaunov

MPaunov

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 18 October 2015 - 09:39 AM   Best Answer

Yes, I agree. getchar() is the better option.

Thank's for the correction!

 

For future readers, the above function should look like this:

int readNumber()
{	
	int number;
	int reads = scanf("%d", &number);
	while(reads != 1)
	{
		getchar();
		reads = scanf("%d", &number);
	}
 
	return number;
}





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download