Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 14-05-2009
Member
 
Join Date: Apr 2009
Posts: 87
How to avoid problems with scanf?

By using the scanf function, I notice that the program becomes crazy when you type a letter (the pointer stop quickly), my scanf also gives me the same problem. Is there a way to avoid such problems?

Thank you for your answers
Reply With Quote
  #2  
Old 14-05-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: How to avoid problems with scanf?

Quote:
By using the scanf function, I notice that the program becomes crazy when you type a letter (the pointer stop quickly), my scanf also gives me the same problem. Is there a way to avoid such problems?
The problem is known. solution is to systematically test the value returned by scanf () and put the buffer of the standard input stream after each call:
Code:
#include <stdio.h>

void clean_stdin(void)
{
    int c;

    do
    {
        c = fgetc(stdin);
    }
    while (c != '\n' && c != EOF);
}

int main(void)
{
    int n;
    int rv;

    do
    {
        printf("Enter the number":\n");
        rv = scanf("%d", &n);
        clean_stdin();
    }
    while (rv != 1);

    printf("this is the number" %d\n", n);

    return 0;
Reply With Quote
  #3  
Old 14-05-2009
Member
 
Join Date: Jan 2009
Posts: 199
Re: How to avoid problems with scanf?

is an example of a secure attachment with scanf:

Code:
 # include <stdio.h> 
  # include <stdlib.h> 
  int main (void) ( 

    int number = 0; 
    verify int = 0; 
    while (! verif) / / until the user does not have 
    ( 
       printf ( "enter a number: \ n"); 
       verify = scanf ( "% d", & number) / / we check the return value of scanf 
       while (getchar ()! = '\ n') () / / empty the buffer on 
    )  
    printf ( "You type:% d \ n", number); 
    return 0; 
  )
Reply With Quote
  #4  
Old 14-05-2009
Member
 
Join Date: Apr 2009
Posts: 87
Re: How to avoid problems with scanf?

scanf() takes the new line by using '\n' or ' '. i solve that problem by using fgets() to read a string.Because that using the function fflush () on stdin causes undefined behavior ...But how can I read a integer with no use of scanf()?
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to avoid problems with scanf?"
Thread Thread Starter Forum Replies Last Post
How to avoid Power Problems in Toshiba P305 - S8844 laptop Thenral Portable Devices 5 07-04-2011 10:19 AM
How to use frame work properly in web pages to avoid problems? Kallol Technology & Internet 5 11-03-2010 06:21 AM
Solve numerically erf(x) and alternative to scanf Ricky58 Software Development 4 26-10-2009 12:46 AM
Add scanf without newline Zool Software Development 3 08-10-2009 02:34 PM
Avoid Low Memory Problems FlashX Tips & Tweaks 2 28-03-2009 11:30 AM


All times are GMT +5.5. The time now is 06:57 AM.