>u need to put cin.ignore(2); and then you will be able to see the results before the complier shuts it down.
Unless there are 3 extra characters. Or 4. Or 5. Where do you want to stop? It's better to avoid this problem entirely by using std::getline and std::string for all of your input needs and then converting using something like std::stringstream or boost::lexical_cast<>. However, if you hit this problem, the conventional solution is this:
Code:
#include <iostream>
#include <ios> // For streamsize
#include <limits> // For numeric_limits<>
int main()
{
// ...
std::cin.clear();
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cin.get();
}
These three lines clear the stream state so that you can read from the stream (if the stream is in a failure state, any input requests will fail), then remove all of the characters in the stream up to a newline, and finally make a blocking read. If there are extraneous characters in the stream, this will pause the program until you type a newline.
If the stream is empty, you'll end up having to type a newline twice because both cin.ignore and cin.get are blocking reads. You can fix that (if it's really an issue) with a little stream buffer magic trick:
Code:
#include <iostream>
#include <ios> // For streamsize
#include <istream> // For basic_istream<>
#include <limits> // For numeric_limits<>
namespace jsw {
template <typename CharT, typename Traits>
std::basic_istream<CharT, Traits>& ignore (
std::basic_istream<CharT, Traits>& in, CharT delim )
{
if ( in.rdbuf()->sungetc() != Traits::eof() && in.get() != delim )
in.ignore ( std::numeric_limits<std::streamsize>::max(), delim );
return in;
}
}
int main()
{
// ...
jsw::ignore ( std::cin, '\n' );
std::cin.clear();
std::cin.get();
}
How it works really isn't important at this point because if you're having trouble with basic I/O, throwing you head first into stream buffers probably isn't a good idea.