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. C++ Question.

C++ Question.

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
23 Posts 8 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.
  • S Stye

    Then simply use: cin.getline( line, 100) for the example given. The DEFAULT will be '\n' (carriage return) added as the final character when the return (enter) key is pressed.

    W Offline
    W Offline
    WREY
    wrote on last edited by
    #21

    In case you haven't been reading the other posts, 'getline()' has a bug in it, and is unfit to use (at least for VC++ 6). Even the fix that MS has issued, DOES NOT WORK!! Take my word for it, I am very acquainted with the various iostream member functions (including 'getline()') and the only reason why I posted the question on this forum, was in hope that somebody may have found a workaround for the problems this I/O function has been plagued with. I understand VC++ 7 does not have this problem, and that is good news for those who might want to use it. :suss: William Fortes in fide et opere!

    1 Reply Last reply
    0
    • W WREY

      In the use of

      string str;
      getline(cin, str); // data gets entered here
      if(str=="???" || str=="")
      {
      // invalid string
      }
      else
      {
      // VALID string
      }

      The logic is always executing the invalid string segment, even when valid data is entered. Why is this happening? Thanks for any insight. :) William Fortes in fide et opere!

      S Offline
      S Offline
      Stye
      wrote on last edited by
      #22

      I have 6.00 and works fine for me, try this out: #include "stdafx.h" #include "iostream.h" #include "string.h" int main(int argc, char* argv[]) { char line[100]; cout << "Type a line" << endl; cin.getline( line, 100); if(strcmp(line, "xxx") == 0) cout << "You entered xxx"; else cout << "You did not enter xxx"; return 0; }

      1 Reply Last reply
      0
      • W WREY

        In the use of

        string str;
        getline(cin, str); // data gets entered here
        if(str=="???" || str=="")
        {
        // invalid string
        }
        else
        {
        // VALID string
        }

        The logic is always executing the invalid string segment, even when valid data is entered. Why is this happening? Thanks for any insight. :) William Fortes in fide et opere!

        S Offline
        S Offline
        Stye
        wrote on last edited by
        #23

        Doing it the way you have you have to press the enter key twice after getline to get a response (the bug you were referring to). See http://support.microsoft.com/default.aspx?scid=kb;en-us;Q240015 for the fix ( the fix does work if you make the changes in the right file. The file referred to is in two different locations. You might as well change the code in both: MicrosoftVisualStudio\VC98\Include\String and MicrosoftVisualStudio\VC98\CRT\SRC\String I did and the enter key only has to be pressed once now and the code below which is like your way of doing it works fine. #include "stdafx.h" #include "string" #include "iostream" using namespace std ; int main(int argc, char* argv[]) { string s1; cout << "Enter a sentence:"; getline(cin,s1); if(s1 == "???" || s1 == "") cout << "First case"; else cout << "Second case"; return 0; }

        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