Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: need clarification  (Read 3147 times)

0 Members and 1 Guest are viewing this topic.

EEVIAC

  • Guest
need clarification
« on: February 18, 2010, 03:20:44 PM »
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 say
« Last Edit: February 18, 2010, 03:31:28 PM by EEVIAC »

EEVIAC

  • Guest
Re: need clarification
« Reply #1 on: February 18, 2010, 05:01:03 PM »
I forgot to mention that this is a C++ tutorial I'm reading this out of

EEVIAC

  • Guest
Re: need clarification
« Reply #2 on: February 19, 2010, 02:56:45 PM »
I 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















Treval



    Hopeful

    Thanked: 14
    Re: need clarification
    « Reply #3 on: February 19, 2010, 08:22:37 PM »
    That's very nice EEVIAC. =P

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

    Get it! =P
    I recommend it so much.