1.

Solve : need clarification?

Answer»

I'm a programming newbie and NEED help understanding what the following code and paragraph is saying..

Code: [Select]
cin.GET(); // add this statement
cin.get(); // and maybe this, too
return 0;
}



"The cin.get() statement reads the next keystroke, so this statement causes the program to wait until you press the Enter key. (No keystrokes get sent to a program until you press the Enter key.) The second statement is needed if the program otherwise leaves an unprocessed keystroke after its regular input. For example, if you enter a number, you'll type the number and then press enter. The program will read the number but leave the Enter keystroke unprocessed, and it then will be read by the first cin.get()"


I understand how the first statement is USED, but I don't understand how the second statement is used.. I don't know what the paragraph is trying to sayI forgot to mention that this is a C++ tutorial I'm reading this out ofI figured this out using a sample program in the book I'm using...
Except I substituted in some string values in other than what the sample code was using...

Code: [Select]#include <iostream>
using namespace std;
int main()
{
int fleas;

cout << "How many fleas does your cat have?\n";
cin >> fleas;
cout << "OH MY GOSH!! " << fleas << " is waaaaay too many. Bath time!\n";

cin.get();
cin.get();
return 0;
}



Output:

How many fleas does your cat have?
100
OH MY GOSH!! 100 fleas is waaaaay too many. Bath time!



The int fleas declaration means that subsequent uses of the word "fleas", being declared an integer variable, will be replaced by a random integer typed into the keyboard after the program is run, and the first output (in this case a question) is displayed.

The first cin.get(); statement forces the program to wait for the ENTER key to be pressed so the program doesn't CLOSE immediately after running.

The second cin.get(); is used to wait for a random integer that must be typed, before pressing enter, and displays the output as shown.. In this case the number was 100














That's very nice EEVIAC. =P

Here is the best book on C++
Deitel C++

Get it! =P
I recommend it so much.



Discussion

No Comment Found