getline, cin, help

Started by
3 comments, last by stay in school 19 years, 8 months ago
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.
Advertisement
First, fix your VC6 library because it's broken.

Second
#include <string>#include <iostream>std::string line;std::getline( std::cin, line );
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
#include<string> -- it should work.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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>

This topic is closed to new replies.

Advertisement