Why are you wanting to read whitespace? The code you have given us implies there shouldn't be any whitespace. What exactly are you trying to accomplish here?
#include <iostream>
#include <string>
#include <cctype>
int main()
{
const std::size_t nchars = 8 ; // number of characters to be input
std::string str ;
std::cout << "enter " << nchars << " characters (including whitespace characters): " ;
while( str.size() < nchars ) // as long nchars characters haven't been added
{
char c ;
// get() is an unformatted input function (does not skip leading whitespace)
std::cin.get(c) ; // read in one char (may be whitespace)
str += c ; // or: str.push_back(c) ; // and append it to the string
}
// print out all nchars characters (all characters that are in the string)
// range based loop: http://www.stroustrup.com/C++11FAQ.html#forfor( char c : str ) // for each character c in the string
{
// print the character
if( std::isprint(c) ) std::cout << c ; // printable character, print it as it is
elseif( c == '\t' ) std::cout << "\\t" ; // tab character, print its escape sequence
elseif( c == '\n' ) std::cout << "\\n" ; // new line character, print its escape sequence
else std::cout << "\\" << std::oct << int(c) ; // other unprintable character, print its octal escape sequence
// also print its integer value
std::cout << " " << std::dec << int(c) << '\n' ;
}
}