Here are a few questions to help you clarify your understanding of the basics of if's, while's, and their helpers.
Fill in the following table with appropriate operators so that the aligning columns (i.e. entries in the same row) have matched meanings.
Mathematics | C++ |
---|---|
> | > |
≥ | >= |
< | < |
≤ | <= |
= | == |
≠ | != |
OR | ¦¦ |
AND | && |
NOT | ! |
What is wrong in the following code? (You can assume that all variables are declared and initialized properly.)
if (c > 4); { z = 4 * y; }; else; { z = z * 8; };
The semi-colons after the if condition, the else keyword, and after the closing braces are all excessive. The first three will cause errors and the last one is simply extra — an empty statement.
How does a 2-way branch differ from a 1-way branch?
A 2-way branch has an else clause but a 1-way branch simply has an if.
What sense does a 1-way branch make, anyway!? Do you have any examples of what one [pardon the pun] might be used for?
A 1-way branch is useful when we have to perform a special action which has no alternative. For instance, pluralization typically requires that we add an s to a noun's singular form. Not being plural, however, is just the singular form — no extra s attached: cout << "Sentence about " << count << " noun"; if ( count != 1 ) { cout << 's'; } cout << ".\n"; The singular form has already been displayed and an s is only displayed when plural form is required.
Recalling the cctype functions for classifying characters into groups, write a branching structure which can classify a character entered by the user into three (or more) of the following categories: letter, number, punctuation, alpha-numeric, hexadecimal digit, lowercase, or uppercase.
For example, if the user typed the character 'e', you might report to them that their character was a lowercase letter, alpha-numeric, and a hexadecimal digit. But if they type in a ',', you'd only be able to say it was punctuation.
cout << "The character '" << ch << "' is "; if ( isalnum(ch) ) { cout << "alpha-numeric (specifically "; // could pre-pend an if to check for hexidecimal digits if ( isalpha(ch) ) { // could also do isupper and islower tests here to classify as // capital or lower-case cout << "alphabetic" } else { cout << "numeric" } cout << ')'; } else if ( ispunct(ch) ) { cout << "punctuation" } else { cout << "unrecognized" }
Why is the category 'spacing' not listed above? Is this category somehow more difficult to read from the user?
Spacing is normally thrown out as not useful by cin's extraction operator (>>). In order to read it, we'd need to use a peek/ignore combo, for instance.
You are given this function:
bool toss(void);
which returns true when a coin comes up heads and false when the coin lands tails up. Use it to write a branch to tell the user which way the coin landed when your program 'tossed' it. (Hint: Remember that you can't print bool directly.)
cout << "Coin came up "; if ( toss() ) { cout << "head"; } else { cout << "tail" } cout << "s.\n";
What is wrong in the following code? (You can assume that all variables are declared and initialized properly.)
c = 0; while (c < 10); cout << c << ' '; c = c + 2; cout << c << endl;
There can be no semi-colon after the while's condition (or we'll loop the empty statement). From the indention, it appears that the programmer wanted the first cout and the increment of c to both occur inside the loop — but only the cout will even with the erroneous semi-colon removed; so we'll have to add braces around this pair: c = 0; while (c < 10) { cout << c << ' '; c = c + 2; } cout << c << endl;
Someone else has written a function with prototype 'void menu(void);'. Their function will print this output on the screen for the user:
1) enter X 2) enter Y 3) Add Them 4) Quit Choice:
(Where the is the cursor's position.)
Write all code necessary to read and process the user's choice from this menu. (You don't have to code the menu function since someone else already did that for you! You may, however, want to call it...)
bool quitting; char choice; quitting = false; while ( ! quitting ) { menu(); cin >> choice; cin.ignore(numeric_limits<streamsize>::max(), '\n'); choice = tolower(choice); if ( '1' == choice ¦¦ 'x' == choice ) { // input x } else if ( '2' == choice ¦¦ 'y' == choice ) { // input y } else if ( '3' == choice ¦¦ 'a' == choice ¦¦ 't' == choice ) { // total x and y } else if ( '4' == choice ¦¦ 'q' == choice ) { quitting = true; } else { // invalidity message } }
Write a loop to read in a user's test score. Make sure the score is non-negative before you continue with the program! (Don't worry about the user typing in non-numeric data.)
cout << "prompt"; cin >> score; while ( score < 0 ) { cerr << "error message"; cout << "re-prompt"; cin >> score; }
Write a loop to read in a user's test score. Make sure the user entered an actual numeric value before you continue with the program! (The value read can be anything the user likes.)
cout << "prompt"; cin >> score; while ( cin.fail() ) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cerr << "error message"; cout << "re-prompt"; cin >> score; }
Write code to read in a user's test score. Make sure not only that the user entered an actual numeric value but also that the score is non-negative before you continue with the program!
cout << "prompt"; cin >> score; while ( cin.fail() ¦¦ score < 0 ) { if ( cin.fail() ) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cerr << "failure error message"; } else { cerr << "domain error message"; } cout << "re-prompt"; cin >> score; }
You are given this function:
bool toss(void);
which returns true when a coin comes up heads and false when the coin lands tails up. Use this function in writing a loop to tell the user how many times their coin came up heads and how many times it came up tails during sequence of 'tosses'. Allow the user to specify how many tosses of the virtual coin there should be. (Hints: this will require a branch nested inside your loop.)
short count, heads; // same type as tosses... heads = 0; count = 0; while ( count < tosses ) { if ( toss() ) { heads = heads + 1; } count = count + 1; } cout << "During the " << tosses << " coin tosses we got " << heads << " heads and " << tosses - heads << " tails.\n";