Getting input the rightway?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xlsmnt
    New Member
    • Feb 2007
    • 10

    Getting input the rightway?

    I've recently started working with C++ and wondered if there was a "right" way for getting input from the console window. From what I've seen whenever I attempted to implement it more than once, cin>>string seems to fail on items of the "string" type. The use of getch()/scanf() isn't really satisfactory because getch() requires lots of garbage add ons to recognize the end of the string, and backspacing, while scanf() seems to run into the same problem as cin>>string. I've tried dozens of different methods for handling input from the console window, and most of them seem to have seemingly random errors if implemented more than once in the program. If anyone could provide suggestions or examples for getting input for the "string" type I would appreciate it.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Look up getline in you C++ reference book

    Comment

    • xlsmnt
      New Member
      • Feb 2007
      • 10

      #3
      Originally posted by Banfa
      Look up getline in you C++ reference book
      It doesn't work correctly after multiple implementations , if I use, getline(cin, string); more than three times or so it begins to spit out random errors, and if I use it in a function that is implemented several times it spits out gibberish.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        You probably need to flush the input buffer between calls to getline with cin.flush().

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          I do not think flush is a member of basic_istream (type of cin) (at least not in standard C++) it is a member of basic_ostream.

          This makes sense all mirrors the badness of calling fflush on an input stream in C (it's bad don't do it).

          I think the cin.ignore may be the function to use.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            And don't use scanf() in C++. That's for C.

            Comment

            • xlsmnt
              New Member
              • Feb 2007
              • 10

              #7
              Originally posted by Banfa
              I do not think flush is a member of basic_istream (type of cin) (at least not in standard C++) it is a member of basic_ostream.

              This makes sense all mirrors the badness of calling fflush on an input stream in C (it's bad don't do it).

              I think the cin.ignore may be the function to use.
              cin.ignore() causes the function to eat the first character of every string after the first string, and I have no idea why. The upside is it is now providing me with strings instead of random ascii symbols.

              Comment

              • xlsmnt
                New Member
                • Feb 2007
                • 10

                #8
                Originally posted by xlsmnt
                cin.ignore() causes the function to eat the first character of every string after the first string, and I have no idea why. The upside is it is now providing me with strings instead of random ascii symbols.
                I found my own answer, the problem wasn't in getline, but in the variable I was referencing, for some reason my compiler didn't catch that I had a declared pointer "mystring" and a declared string "mystring" in the same function. So I wrote the getstring() function separately as
                Code:
                string getstring()
                {
                string input;
                getline(cin, input);
                return input;
                }
                This seems to actually work fine without any use of cin.clear() or cin.ignore()

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  Be careful; using getline like that, the program will look for any and all text from the start of the stream to the newline character ('\n, it's made when you press <Enter> or <Return>). It grabs that text, removes it from the stream, and returns it, but it leaves the '\n' still in the stream. This might mess you up if you call cin.get() or getline again after the original getline call. I ran into this problem a lot when I was learning C++.

                  Comment

                  • xlsmnt
                    New Member
                    • Feb 2007
                    • 10

                    #10
                    Originally posted by Ganon11
                    Be careful; using getline like that, the program will look for any and all text from the start of the stream to the newline character ('\n, it's made when you press <Enter> or <Return>). It grabs that text, removes it from the stream, and returns it, but it leaves the '\n' still in the stream. This might mess you up if you call cin.get() or getline again after the original getline call. I ran into this problem a lot when I was learning C++.
                    Well for some reason I'm not running into it now, does the system I'm compiling/running on effect that somehow?

                    Also I was wondering whether this
                    Code:
                    void getstring(string& ptr)
                    {
                    getline(cin, ptr);
                    }
                    int main()
                    {
                    string * ptr;
                    string mystring;
                    ptr = &mystring;
                    getstring(*ptr);
                    }
                    was the same as this
                    Code:
                     
                    string getstring()
                    {
                    input string;
                    getline(cin, string);
                    return string;
                    }
                    int main()
                    {
                    string mystring;
                    mystring = getstring();
                    }

                    Comment

                    Working...