Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. scanf bug?

scanf bug?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Deian
    wrote on last edited by
    #1

    Hello, I experienced a strange behaviour from the scanf function; if i put the following code char ch; printf("stuff"); scanf("%c", &ch); the program fills ch automatically and continues (skips input), anyone had the same problem ?

    D A Y 3 Replies Last reply
    0
    • D Deian

      Hello, I experienced a strange behaviour from the scanf function; if i put the following code char ch; printf("stuff"); scanf("%c", &ch); the program fills ch automatically and continues (skips input), anyone had the same problem ?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Deian wrote:

      the program fills ch automatically and continues (skips input), anyone had the same problem ?

      Perhaps there is already something in the stdio stream that is being accepted by scanf().


      "The largest fire starts but with the smallest spark." - David Crow

      "Judge not by the eye but by the heart." - Native American Proverb

      D 1 Reply Last reply
      0
      • D David Crow

        Deian wrote:

        the program fills ch automatically and continues (skips input), anyone had the same problem ?

        Perhaps there is already something in the stdio stream that is being accepted by scanf().


        "The largest fire starts but with the smallest spark." - David Crow

        "Judge not by the eye but by the heart." - Native American Proverb

        D Offline
        D Offline
        Deian
        wrote on last edited by
        #3

        I've read in many books and code snippets that you may use this pair printf/scanf, I tought that this is some kind of bug, I didn't want to use flush or similar function. Thanks for the reply.

        1 Reply Last reply
        0
        • D Deian

          Hello, I experienced a strange behaviour from the scanf function; if i put the following code char ch; printf("stuff"); scanf("%c", &ch); the program fills ch automatically and continues (skips input), anyone had the same problem ?

          A Offline
          A Offline
          Arvind Bharti
          wrote on last edited by
          #4

          It is working perfectly fine in Visual Studio 6.0. Could you please give us some more details about Compiler, Operating System OR any information worth to repeat this problem?

          D 1 Reply Last reply
          0
          • A Arvind Bharti

            It is working perfectly fine in Visual Studio 6.0. Could you please give us some more details about Compiler, Operating System OR any information worth to repeat this problem?

            D Offline
            D Offline
            Deian
            wrote on last edited by
            #5

            Visual Studio 6.0 / SP5, Windows XP SP2 g++, FreeBSD 6.1 Devshed C++ / Windows XP SP2 I experience the same behaviour at all OS's and compilers.

            A 1 Reply Last reply
            0
            • D Deian

              Hello, I experienced a strange behaviour from the scanf function; if i put the following code char ch; printf("stuff"); scanf("%c", &ch); the program fills ch automatically and continues (skips input), anyone had the same problem ?

              Y Offline
              Y Offline
              yogendra kaushik
              wrote on last edited by
              #6

              use sscanf Please mail me

              1 Reply Last reply
              0
              • D Deian

                Visual Studio 6.0 / SP5, Windows XP SP2 g++, FreeBSD 6.1 Devshed C++ / Windows XP SP2 I experience the same behaviour at all OS's and compilers.

                A Offline
                A Offline
                Arvind Bharti
                wrote on last edited by
                #7

                I've checked it on RH linux too with gcc compiler. Sorry sir, it's not repeatable. Could you please let me know some more details about the program? One possibility is that, this is the part of your big program and something is already there in stdin stream. Did you check the mentioned code as a separate program?

                D 1 Reply Last reply
                0
                • A Arvind Bharti

                  I've checked it on RH linux too with gcc compiler. Sorry sir, it's not repeatable. Could you please let me know some more details about the program? One possibility is that, this is the part of your big program and something is already there in stdin stream. Did you check the mentioned code as a separate program?

                  D Offline
                  D Offline
                  Deian
                  wrote on last edited by
                  #8

                  Yes, the fragment is part of a program, which uses scanf with %d, %s and %c, the problem of skipping the scanf function occurs only where the %c parameter is used. Why something is still staying in stdin ??

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups