// excin_2.cppThis example requests a float number and only exits when it has one. If it cannot convert the input, it outputs an error message and calls clear() to clear the fail bit. The ignore function skips all the rest of the input line. 256 is a sufficiently large number of characters that the \n will be reached before all 256 have been read.
#include "stdafx.h" // Microsoft only
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
float floatnum;
cout << "Enter a floating point number:" <<endl;
while(!(cin >> floatnum))
{
cin.clear() ;
cin.ignore(256,'\n') ;
cout << "Bad Input - Try again" << endl;
}
cout << "You entered " << floatnum << endl;
return 0;
}
Note: An input such as 654.56Y will read all the way up to the Y, extract 654.56 and exit the loop. It is considered valid input by cin
Unformatted Input
This is a more powerful way of inputting characters or entire lines, rather than keyboard input but that will be left for a later lesson on file I/O.Keyboard Entry
All the input, using cin requires the Enter or Return key to be pressed. Standard C++ does not provide a way to read characters directly from a keyboard. In future lessons we'll see how to do that with third party libraries.This ends the lesson.

