Sign in to follow this  

getline, cin, help

This topic is 4855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to read from the console user input and I'm using VC6 Studio. I'm trying just to read a line... here's the code:
   cout << "\n\nEnter 8 more characters and press <Enter>: ";
   cin.read(c,8);

   // Extra +1 for null;
   const static int MAX_FILENAME_SIZE = 65;

   char whitespace[ MAX_FILENAME_SIZE ];
   char fileName_cstr[ MAX_FILENAME_SIZE ];
   cout << "\n\nEnter file you want to scan." << endl;
   cin.getline( whitespace, MAX_FILENAME_SIZE );
   cin.getline( fileName_cstr, MAX_FILENAME_SIZE );
   cout << fileName_cstr << endl;

I have to use the whitespace array so that the next line is read from after the newline not just after the first 8 characters are entered. Is there a better way of doing this than using a large char array? Is there some kind of flush() for the cin... or something to say hey I just want to start over here? Because what happens if the user enters more than 65 characters at the command line before pressing the enter key? All hell breaks loose probably, huh? I just think this is not very elegant and I'm looking for an alternative. Thanks, me.

Share this post


Link to post
Share on other sites
I get these errors... I suppose this is something that needed to be fixed for VC6?

d:\cpp\madness\madness.cxx(61) : error C2039: 'getline' : is not a member of 'std'

d:\cpp\madness\madness.cxx(61) : error C2065: 'getline' : undeclared identifier

d:\cpp\madness\madness.cxx(63) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no acceptable conversion)

when using your code fruny. And the last error when I try to output the string object line with cout... like so:

cout << line;


Man that irks me, because isn't the getline method part of the standard namespace, how can you mess something like that up?

I can't update my VC6 right at the moment... so I'm stuck for now.

me.

Share this post


Link to post
Share on other sites
Quote:
Original post by newOperator
Is there some kind of flush() for the cin... or something to say hey I just want to start over here?

Yes...
cin.ignore(numeric_limits<int>::max(), '\n');

should work. Remember to #include <limits>

Share this post


Link to post
Share on other sites

This topic is 4855 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this