Data Entry check - // the invalid input, so we need to skip...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> # using namespace std; u int main () { cout << "Please enter a number followed by a <return> or <enter>: "; double number; while ( ! (cin >> number)) { // Enter this loop if input fails because of invalid data. cout << "Hey dummy, I said enter a number! Try again: "; cin.clear (); // reset the "failure" flag // The input "cursor" is still positioned at the beginning of
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // the invalid input, so we need to skip past it. cin.ignore (1000, '\n'); // Skip to next newline or 1000 chars, // whichever comes first. This is why // you have to follow the input with // &lt;return&gt; or &lt;enter&gt;. } cout &lt;&lt; &quot;OK, you entered &quot; &lt;&lt; number &lt;&lt; &quot;.&quot; &lt;&lt; endl; return 0; }...
View Full Document

Ask a homework question - tutors are online