Welcome Guest Donate | Search | Active Topics | Members | Log In | Register

Input and Output Operation in C++ Options
lingy
Posted: Friday, July 30, 2010 4:17:52 PM

Rank: Administration
Groups: Administration , Member

Joined: 5/8/2009
Posts: 1,576
Points: 5,631

Using the standard input and output library, we will be able to interact with the user by printing messages on the screen and getting the user's input from the keyboard.

The standard input device is usually the keyboard. Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream. The operator must be followed by the variable that will store the data that is going to be extracted from the stream. For example:

int age;
cin >> age;

The first statement declares a variable of type int called age, and the second one waits for an input from cin (the keyboard) in order to store it in this integer variable.

cin can only process the input from the keyboard once the RETURN key has been pressed. Therefore, even if you request a single character, the extraction from cin will not process the input until the user presses RETURN after the character has been introduced.

You must always consider the type of the variable that you are using as a container with cin extractions. If you request an integer you will get an integer, if you request a character you will get a character and if you request a string of characters you will get a string of characters.

Sponsor
Posted: Friday, July 30, 2010 4:17:52 PM


lingy
Posted: Friday, July 30, 2010 4:25:29 PM

Rank: Administration
Groups: Administration , Member

Joined: 5/8/2009
Posts: 1,576
Points: 5,631

scanf() and scanf_s()

Both scanf() and scanf_s() functions can be used to read data from standard input.

The secure versions (those with the _s suffix) of the scanf() family of functions require that a buffer size parameter be passed preceding each parameter of type c, C, s, S or [.

The width specification is separate and distinct from the buffer size argument required by the secure versions of these functions (i.e., scanf_s(), wscanf_s(), etc.). In the following example, the width specification is 20, indicating that up to 20 characters are to be read from the input stream. The buffer length is 21, which includes room for the possible 20 characters plus the null terminator:
  

char str[21];

scanf_s("%20s", str, 21);

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.


© 2010 Canaware Solutions. All rights reserved.
Powered by Canaware Forum version 2.4