Here are a few questions to help you clarify your understanding of mid-level input/output: peek'ing at a stream's boat, recognizing your cargo master for the failure that s/he is, administering the cargo handler's selective memory erasure medicine (SMEM), and making decimal numbers come out all nice and pretty and stuff.
The peek function (one of those in cin's class):
returns the next character in the keyboard streamYES
removes the next character in the keyboard streamNO
advances past the next character in the keyboard streamNO
is called as 'ch = peek(cin);'NO
is called as 'peek(cin, ch);'NO
is called as 'ch = cin.peek();'YES
is called as 'cin.peek(ch);'NO
The ignore function (one of those in cin's class):
returns the next character in the keyboard streamNO
removes the next character in the keyboard streamYES
advances past the next character in the keyboard streamYES
is called as 'ignore(cin);'NO
is called as 'cin.ignore();'YES
Each line of user input is terminated with a newline ('\n') character (an escape sequence).
Show how you can detect that the next character in cin's buffer is the end of the input or not.
if ( cin.peek() == '\n' ) { // next character in cin's buffer is end of input } else { // next character in cin's buffer is NOT end of input }
You are tasked with writing the portion of a program that will read a mathematical interval from the user.
Your overall goal is to show how you can determine that the user is entering either a '[' or a '(' at the left end of the interval and either a ']' or a ')' at the right end of the interval. Between the left and right positions, there should also be a ','. Finally, when reading the left and right positions of the interval, check whether the user has a numeric value or if they've entered the sequence of characters 'inf' to represent ∞.
(Hint: Even though the user sees this as entering an interval, you can code it as reading five pieces of data — since an interval is five separate entities. Take each one alone and then put them together in order. Since there is some similarity/symmetry to the notation, you could use that to your advantage.)
(Hint 2: In fact, we could take the concept further and visualize the reading of a single 'number' as two pieces of information: a sign and a magnitude. Then, before continuing with the program, we'd just re-attach the sign to the magnitude so our program can function properly.)
left end symbol: (How will your program remember later whether the left numeric position is to be inclusive or exclusive? i.e. whether the left numeric position is/is not to be included?)
char t; bool left_included; cin >> t; left_included = t == '['; // alternative is '(' for excluded
right end symbol: (How does it differ from the left end code?)
bool right_included; cin >> t; right_included = t == ']'; // alternative is ')' for excluded
comma in the middle: (How does it differ from the end symbol codes?)
cin >> t; // just pull this out and throw it away...
a sign for a numeric value: (It could be either '+' or '-'!)
bool negative; if ( cin.peek() == '-' ¦¦ cin.peek() == '+' ) { cin >> t; negative = t == '-'; // alternative is '+' for positive } else { negative = false; }
a numeric value (which may be the character sequence 'inf'): (How do you represent an ∞ in a double?)
if ( tolower(cin.peek()) == 'i' ) { cin.ignore( 2, 'f' ); // can't tolower here, but we'll just be // skipping two chars, anyway... num = 1 / 0.0; } else { cin >> num; }
a signed numeric value: (Perform the sign and numeric value in sequence; then re-attach the sign to the numeric value — possibly while converting ∞...)
bool negative; if ( cin.peek() == '-' ¦¦ cin.peek() == '+' ) { cin >> t; negative = t == '-'; // alternative is '+' for positive } else { negative = false; } if ( tolower(cin.peek()) == 'i' ) { cin.ignore( 2, 'f' ); // can't tolower here, but we'll just be // skipping two chars, anyway... num = 1 / 0.0; } else { cin >> num; } if ( negative ) { num = -num; }
If you needed to put all of this together into one stretch of code, would there be any other considerations/interactions?
Make sure to adjust each bound (lower and upper) for negativeness as
it comes in — we've only got one negative bool here.
The ignore function (one of those in cin's class) has a [fraternal] twin with similar but distinctly different behavior. Describe these different behaviors and what input(s) help the compiler tell which of the twins you wished to employ.
The first version takes no inputs and ignores a single character from cin's input stream. The second takes two inputs. The first input describes a number of characters to be ignored and the second describes a special character that can make the ignoring stop before the count. If the number of characters is numeric_limits<streamsize>::max(), ignore will not actually count out the characters being ignored and will instead keep ignoring until the special terminating character is found (and ignored).
The clear function (one of those in cin's class):
clears the screenNO
empties the input bufferNO
clears cin's memory of all input problemsYES
is called as 'clear(cin);'NO
is called as 'cin.clear();'YES
How can you use cin's function fail to detect [possible] problems with the input of numeric values into your program?
Briefly explain the fail function and then show how you would use it to try to fix a translation error. (Hint: is the help of another cin function needed?) (Hint: you are making a decision as to whether cin's last action — an extraction/>> — failed or not.)
fail reports true when a [recent] numeric extraction has left cin upset. To help fix this problem, we'll need the help of clear to make cin forget about the erroneous translation. cout << "prompt"; cin >> number; while ( cin.fail() ) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "message\nprompt"; cin >> number; }