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:
Quote:
Error: no instance of 'getline' matches the argument list
:-/