Thread: scanf() question.

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    scanf() question.

    Before my program quits, I would like a user to press enter before the program ends. I want to do something similiar to simple:
    getchar();
    but with using scanf();
    Is there anyway of doing it like that ?

    Regards,
    apacz.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there anyway of doing it like that ?
    I suppose:
    Code:
    scanf ( "%*c" );
    But why do you what to use scanf? It's for formatted input, and this task implies unformatted input. getchar is better suited in principle and in practice.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    76
    because I have sucha code:
    Code:
    #include <stdio.h>
    
    int main()
    {
            int     s;
            scanf("%d\n", &s);
            getchar();
            return  0;
    }
    and it doesn't work neither if there is '\n' in scanf or not.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Which is why we usually suggest fgets() + sscanf() rather than trying to cope with the randomness which simple scanf() leaves behind.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >and it doesn't work neither if there is '\n' in scanf or not
    Using scanf to read a single character won't save you from that either because you're simulating the functionality of getchar. The effect is identical. Now, the proper solution is to use fgets for reading a line and then parsing the line in memory. This avoids the "whitespace stuck in the stream" problem:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      char buf[BUFSIZ];
      int x;
    
      printf ( "Enter a whole number: " );
      fflush ( stdout );
    
      if ( fgets ( buf, sizeof buf, stdin ) != NULL ) {
        if ( sscanf ( buf, "%d", &x ) != 1 ) {
          fprintf ( stderr, "Invalid input\n" );
          return EXIT_FAILURE;
        }
    
        printf ( "You entered: %d\n", x );
      }
    
      getchar();
    
      return EXIT_SUCCESS;
    }
    Alternatively, you can "flush" the stream with an ugly hack:
    Code:
    int ch;
    
    while ( ( ch = getchar() ) != EOF && ch != '\n' )
      ;
    I don't recommend that.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    76
    Thank you very much .

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    17
    Quote Originally Posted by apacz
    Before my program quits, I would like a user to press enter before the program ends. I want to do something similiar to simple:
    getchar();
    but with using scanf();
    Is there anyway of doing it like that ?

    Regards,
    apacz.
    ya..there is a way..
    just decalre a variable of type char and use scanf to read it..i think it will work..u can also do with int

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need a variable. There's a format specifier modifer that allows the discarding of what you read. However, your solution wouldn't work any better than just a single getchar call. More than one character in the stream would have the same problem. There are format specifiers that allow you to read a everything up to a newline, so you'd need that.

    It doesn't make it a good solution. But you could do it.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    15
    Instead of using getchar(), there is another way to make the program stop before quitting and wait for the user to type anykey.
    The answer is... using : system("pause"); at the end of your code.

    Just try!

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    No, don't EVER use system() anything if you can help it. . .

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The answer is... using : system("pause"); at the end of your code.
    This isn't recommended because it's neither portable nor secure.
    My best code is written with the delete key.

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Prelude
    >The answer is... using : system("pause"); at the end of your code.
    This isn't recommended because it's neither portable nor secure.
    Yeah... Windows specific function
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For more information on system("pause") and the alternatives, read these FAQs:

    BTW, using getchar() is greatly preferable to using system("pause"), although you may have to put it into a loop (as the second FAQ link does) if there's something in the input buffer.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  2. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  3. Scanf Question
    By shiyu in forum C Programming
    Replies: 4
    Last Post: 01-31-2003, 08:48 AM
  4. Simple question with scanf()
    By MadStrum! in forum C Programming
    Replies: 3
    Last Post: 01-20-2003, 10:41 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM