Public Group

# getline, cin, help

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

## 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>: ";

// 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 on other sites
First, fix your VC6 library because it's broken.

Second
#include <string>#include <iostream>std::string line;std::getline( std::cin, line );

##### 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(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 on other sites
#include<string> -- it should work.

##### Share on other sites
Quote:
 Original post by newOperatorIs 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>

• 10
• 17
• 14
• 18
• 15