Like Tree1Likes
  • 1 Post By Elkvis

Is there a better way to do string input in C++ via standard input?

This is a discussion on Is there a better way to do string input in C++ via standard input? within the C++ Programming forums, part of the General Programming Boards category; This is my little piece of code. I'm a little rusty, so I don't remember how to do this 100%. ...

  1. #1
    ysg
    ysg is offline
    Registered User
    Join Date
    Feb 2013
    Posts
    27

    Is there a better way to do string input in C++ via standard input?

    This is my little piece of code. I'm a little rusty, so I don't remember
    how to do this 100%. But basically, the below example will allow to
    enter a string up to 100 chars, output it and just for kicks, turn it into
    a string class object.

    It works, but it can be improved on.

    Now, my question is this, would there be a way that would liberate me
    from the C character array?
    Code:
    #include <iostream>#include <ios>
    #include <limits>
    
    
    using namespace std;
    
    
    int main(int argc, char * argv[])
    {
        char testString[100];
        std::cout << "Please enter a string: ";
        std::cin.getline(testString, 100);
    
    
        cout << "This is the string that you entered: " << testString << std::endl << std::endl;
        std::string simpleString(testString);
    
    
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cin.get();
    
    
        return 0;
    }
    I tried this example (all the way at the bottom) in my VC++ 2010 and
    when I typed in getline, my IDE told me the following:
    Error: no instance of 'getline' matches the argument list
    :-/

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    there is the wonderful string object
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    ysg
    ysg is offline
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Quote Originally Posted by rogster001 View Post
    there is the wonderful string object
    I know... but you can't do std::cin >> stringObjectVariable. I did that as my first guess.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    oops sorry wasnt reading your question right! - but you can? c++ ref website code below I never considered it before like:

    Code:
    // cin with strings
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
    	string mystr;
    	cout << "What's your name? ";
    	getline (cin, mystr);
    	cout << "Hello " << mystr << ".\n";
    	cout << "What is your favorite team? ";
    	getline (cin, mystr);
    	cout << "I like " << mystr << " too!\n";
    	return 0;
    }
    Last edited by rogster001; 02-27-2013 at 02:02 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    ysg
    ysg is offline
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Quote Originally Posted by rogster001 View Post
    oops sorry wasnt reading your question right! - but you can? c++ ref website code below I never considered it before like:

    Code:
    // cin with strings
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
        string mystr;
        cout << "What's your name? ";
        getline (cin, mystr);
        cout << "Hello " << mystr << ".\n";
        cout << "What is your favorite team? ";
        getline (cin, mystr);
        cout << "I like " << mystr << " too!\n";
        return 0;
    }


    Yup, and I tried that very example that I listed in VC++ 2010 and I got the following error:
    Error: no instance of 'getline' matches the argument list

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,266
    you can also do this:

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string s;
      std::cin >> s;
    
      return 0;
    }
    the standard defines the std::istream >> operator for std::string. see this link.
    rogster001 likes this.

  7. #7
    ysg
    ysg is offline
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Quote Originally Posted by Elkvis View Post
    you can also do this:

    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string s;
      std::cin >> s;
    
      return 0;
    }
    the standard defines the std::istream >> operator for std::string. see this link.
    Thanks.

    I swear I tried this and VS said that I was doing something wrong. Probably made a stupid
    mistake and thought that it could not be done. Will need to pay closer attention in the
    future.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    2,266
    keep in mind that the >> operator likes to stop reading when it encounters white space. if you need to accept data that contains white space, you will need to use std::getline.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,675
    Am I missing something? What is wrong with getline() with the std::string instead of using the C-string version of getline()?

    Jim

  10. #10
    ysg
    ysg is offline
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Quote Originally Posted by jimblumberg View Post
    Am I missing something? What is wrong with getline() with the std::string instead of using the C-string version of getline()?

    Jim
    How weird. This is what I tried in Visual Studio:
    Code:
    /**
     * hello-world.cpp:
     */
    #include <iostream>
    #include <ios>
    #include <limits>
    #include <string>
    #include <sstream>
    
    
    int main(int argc, char * argv[])
    {
        std::string stuff;
        std::cout << "Please enter a string: ";
        std::getline(std::cin, stuff, 100);
        std::cout << "The stuff: " << stuff << std::endl << std::endl;
    
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cin.get();
    
        return 0;
    }
    And the errors that I got:
    Code:
    1>------ Build started: Project: hello-world, Configuration: Debug Win32 ------1>  hello-world.cpp
    1>c:\documents\visual studio 2010\projects\hello-world\hello-world\hello-world.cpp(34): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
    1>          c:\program files\microsoft visual studio 10.0\vc\include\string(479) : see declaration of 'std::getline'
    1>c:\documents\visual studio 2010\projects\hello-world\hello-world\hello-world.cpp(34): error C2782: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : template parameter '_Elem' is ambiguous
    1>          c:\program files\microsoft visual studio 10.0\vc\include\string(468) : see declaration of 'std::getline'
    1>          could be 'int'
    1>          or       'char'
    1>c:\documents\visual studio 2010\projects\hello-world\hello-world\hello-world.cpp(34): error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
    1>          c:\program files\microsoft visual studio 10.0\vc\include\string(448) : see declaration of 'std::getline'
    1>c:\documents\visual studio 2010\projects\hello-world\hello-world\hello-world.cpp(34): error C2782: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : template parameter '_Elem' is ambiguous
    1>          c:\program files\microsoft visual studio 10.0\vc\include\string(395) : see declaration of 'std::getline'
    1>          could be 'int'
    1>          or       'char'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    This... should work...

    Maybe my VS install is borked.

    [edit]

    I tried the above code in GCC 4.5.3 (in cygwin), these are the errors that I got:
    Code:
    hello-world.cpp: In function ‘int main(int, char**)’:
    hello-world.cpp:14:38: error: no matching function for call to ‘getline(std::istream&, std::string&, int)’
    Last edited by ysg; 02-27-2013 at 02:41 PM.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,675
    No you're using the wrong getline() there are two versions of getline() one for std::string and one for C-strings. You need to use the proper version depending upon you're using a C-string or std::string.

    You must also get the parameters correct. The std::string getline() doesn't use a "size" parameter.

    Jim

  12. #12
    ysg
    ysg is offline
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Oooooh, I see where I went wrong. Gotcha. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-29-2011, 07:02 PM
  2. Standard input---Help!
    By neogst in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2011, 11:33 PM
  3. Standard input way?
    By Milhas in forum C Programming
    Replies: 6
    Last Post: 03-27-2008, 03:58 PM
  4. end of standard input
    By alphaoide in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2004, 08:43 PM
  5. Getting standard input
    By winsonlee in forum C Programming
    Replies: 1
    Last Post: 04-04-2004, 08:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21