I absolutely agree that using the string class is preferred over using C style char arrays. You still might need to only read in a certain number of characters if you use strings, though. Here is another way to do that that works with strings or char arrays:
Code:
#include <iostream>
#include <string>
#include <iomanip>
int main()
{
int numLettersToRead = 5;
char chArray[10];
std::string stdString;
// Use numLettersToRead+1 to allow for the null character
// that is automatically tacked on.
std::cin >> std::setw(numLettersToRead+1) >> chArray;
// Use numLettersToRead since you don't have to worry about
// the null terminator with the string class.
std::cin >> std::setw(numLettersToRead) >> stdString;
std::cout << chArray << std::endl;
std::cout << stdString << std::endl;
return 0;
}
Of course, if you don't like all the std:: things everywhere, you can remove them and put using namespace std; after the #include's.