Learning C++ from a couple library books: Using C++ (Rob McGregor, QUE) and C++ A Beginner's Guide (Herbert Schildt, McGrawhill/Osbourne 2nd ed). Both books are a few years old.
In "Beginner's Guide", each chapter concludes with a "Mastery Check" consisting of general knowledge review questions. In some of these, the reader is asked to write a program or fragment to attain a certain result etc.
Following the chapter in which loop types are discussed, there are two such programming problems:
1. Write a program that reads characters from a keyboard until a $ is typed. Have the program report the number of periods. Report the total at the end of the program.
11. (...)Write a program that reads characters from the keyboard. Have it convert all lowercase letters to uppercase, and all uppercase letters to lowercase, displaying the result. Make no changes to any other characters. Have the program stop when the user enters a period.
This puzzled me, as up to that point in the book, the only command to read keyboard input discussed is "cin>>var" which doesn't seem to process any keyboard input until [ENTER] is pressed. Having the iterations or the program cease immediately by typing "$" or "." seems beyond the scope of the cin.
So I looked up the answer online. Sure enough the syntax of the solution to problem 1 goes something like this:
#include <iostream>
using namespace std;
int main ()
{
char ch; int per=0;
cout<<"Enter a $ to stop.\n";
do {cin>>ch;if (ch=='.') per++;} while (ch != '$');
cout<<"\n\n";cout<<"Periods: "<<per;
}
The solution to problem 11 had similiar syntax.
Sure enough, running this program, I can type a massive string of characters including several "$"s and nothing is evaluated until after I press ENTER, at which point the entire string is evaluated left to right, character by character, until the first $ is encountered, at which point the string is evaluated up to that $, and the rest of the string is ignored.
I guess I'm having semantic issues with the phrasing of the challenge questions, but were they written that way for a reason? The book was published in 2004, has C++ changed in the seven years since?
Thanks in advance, nice to meet you all, and (sadly) this is probably not the last stupid newb question you can expect from me...
I guess I'm having semantic issues with the phrasing of the challenge questions
Yes you are. When the book says 'type' it does not mean to explain the whole process of how a keyboard press becomes a valid character input for your program (which includes pressing Enter).
While it is possible to allow input without pressing enter (using OS specific commands), that is irrelevant to this section of your book.