The Getline() Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CyBerFirEZ
    New Member
    • Aug 2007
    • 48

    The Getline() Problem

    This is the second time I am experiencing a problem with getline.
    I've used it before successfully, but I dont know what I'm doing wrong here.
    Here is the code where the problem is:

    Code:
       if (a == 6)
        {
      string f;
      int g;
      cout << "Enter the message to display at the end of the countdown:" << endl;
      getline (cin, f);
      cout << endl;
      cout << "< Enter the starting number >- ";
      cin >> g;
    
      while (g>0) {
        cout << g << ", ";
        --g;
      }
    cout << f << endl;
    cout << endl;
    cout << endl;
       }
    The code never waits for user input at line 6, and skips straight to the code after it. String f is totally ignored throughout the entire code, and i have included strings. What to do? How do I fix it?
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    I know (or have a very educated guess) on what the problem is, but I need to mention a few tangential points. The first is to write properly indented code. It's a very small snippet, but your code is already showing inconsistencies in indentation. It's fine for a trivially small code snippet, but please make sure in the future that your code follows a proper indentation style. It helps you because you can spot mistakes faster, and it helps us to read your code.

    Also, avoid the user of one letter variable names. It's quick and convenient, but unless you have specific short term situations like an iterated indexer, in the end one letter variable names only obfuscate. Is it really that hard to type a longer name?

    When trying to check if a code is faulty, try to write a fully compilable code example that uses the faulty code. Had you done so, you would have determined that by itself, the code works. So something from before is creating problems.

    What causes getline to skip? Well, for one thing, getline isn't skipping. Getline returns when you hit that enter key. Remember, hitting the enter key is equivalent to entering a newline character into the stream. So if getline "skips", it's really because there's a leftover newline.

    The culprit is cin >> whatever. The >> operation will leave a \n in the stream buffer (cin). So when you use getline, it first encounters a \n. Equivalent to typing the enter key right away.

    See http://www.augustcouncil.com/~tgibso....html#problems . Either use getline for all input, or ignore or get rid of the newline in the buffer.

    Comment

    • CyBerFirEZ
      New Member
      • Aug 2007
      • 48

      #3
      so wat exactly do I have to get rid of?

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        You shouldn't have to get rid of anything. Just understand that, before your getline statement, there is an extraneous '\n' still in the input stream. Since getline reads information until the next '\n' is found, it will read this first character and stop reading. You have to get rid of that '\n' somehow - either by reading it and discarding it with a cin.get() statement, or with a cin.ignore() statement.

        Comment

        • CyBerFirEZ
          New Member
          • Aug 2007
          • 48

          #5
          Hey, it works. I inserted
          Code:
          cin.ignore();
          before the getline command, and now it works!

          Thanx a lot ganon

          :P

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by CyBerFirEZ
            Hey, it works. I inserted

            Code: ( text )
            cin.ignore();
            What if there is more than one character you need to ignore? You may need to flush the input buffer before your getline() call.

            Comment

            • CyBerFirEZ
              New Member
              • Aug 2007
              • 48

              #7
              how do i do that?

              ? ?
              ?
              ----

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Use fflush(stdin).

                stdin is the input buffer stream.

                If fflush() returns 0, the buffer was successfully flushed.

                Comment

                • oler1s
                  Recognized Expert Contributor
                  • Aug 2007
                  • 671

                  #9
                  weaknessforcats , please see http://faq.cprogramming.com/cgi-bin/...&id=1043284351

                  fflush is defined only for output streams. stdin happens to be an input stream. Quite often, the compiler has a non-portable extension for fflush(stdin), but it relies on non-standard behavior. There's a perfectly valid way to strip whitespace without breaking any standards.

                  Try cin.ignore(std: :numeric_limits <int>::max()) . You can also use the sync member function (http://www.cplusplus.com/reference/i...ream/sync.html ).

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    I forgot about sync. I was trying to avoid __flushall() since it is Microsoft specific.

                    Comment

                    Working...