1.   Basic input/output operations

In this lesson we will focus on standard C++ input and output options that will give you the necessary information of successful programming. This input and output commands are not that difficult, quite the opposite in Windows programming (visual programming) you won’t need it at all. C++ input/output operations revolve around the notation of a data stream, where you can insert data into an output stream or extract data from an input stream. In previous example you have used the cout command. This command is the standard output command by the ISO/IEC C++ standard. In that first example you have also used the command cin and that is the complementary input stream from the keyboard. The cout and cin are commands which are defined by the std name space.

1.1.        The keyboard input

To obtain input in a C++ programming language you need to type a command cin, using the extraction operator for a stream, >>. To read two integer values from the keyboard into integer variables n1 and n2, you can write this statement:
std::cin >> n1 >> n2;
Or if you previously typed using namespace std; then just this:
cin >> n1 >> n2;
The extraction operator , > > , “ points ” in the direction that data flows — in this case, from cin to each of the two variables in turn. Any leading whitespace is skipped, and the first integer value you key in is read into n1. This is because the input statement executes from left to right. Any whitespace following num1 is ignored, and the second integer value that you enter is read into n2. There has to be some whitespace between successive values, though, so that they can be differentiated. The stream input operation ends when you press the Enter key, and execution then continues with the next statement. Of course, errors can arise if you key in the wrong data, but I will assume that you always get it right! Floating - point values are read from the keyboard in exactly the same way as integers, and of course, you can mix the two. The stream input and operations automatically deal with variables and data of any of the fundamental types. For example, in the statements,
int n1 = 0, n2 = 0;
double factor = 0.0;
std::cin > > n1 > > factor > > n2;
the last line will read an integer into n1, then a floating - point value into factor , and finally, an integer into n2 .

1.2.     Output to the Command Line

You have already seen output to the command line, but I want to revisit it anyway. Writing information to the display operates in a complementary fashion to input. As you have seen, the standard output stream is called cout , and you use the insertion operator, < < , to transfer data to the output stream. This operator also points in the direction of data movement. You have already used this operator to output a text string between quotes. I can demonstrate the process of outputting the value of a variable with a simple program.

1.1 .  Example

Now you will write an input/output program so open a new project and name it InputOutput1. After you name the project click OK and be sure to select a Win32 Console Application. In the Win32 Application Wizard click Next and then Finish. Enter a following code:
// InputOutput1.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
            int a = 5;
            int b = 6;
            cout << a << endl;
            cout << b << endl;
            cout << "You have written an example of input/outpu program."<<endl;
            return 0;

If you entered a given code correct than it won’t be an error debugging it. If however, your code is incorrect than you have to check the code manually or look for red underlined errors (syntax errors). After you debug your code press Ctrl+F5 simultaneously on your keyboard if your using the Visual Studio C++. The program should display the following window.

Let’s examine the code a little bit. First of all you have used the following STD libraries: stdafx.h and the iostream. These are the standard libraries defined by the ISO/IEC C++ standard. After that you have used the namespace std which contains the basic commands for input and output. Below the function main you have entered two integers (numbers) that is: a and b. You assigned numbers to them (a = 5, b = 6). Then you used basic command to output or present these numbers, and the following sentence “You have written an example of input/output program.”

1.3. Escape Sequences

When you write a character string between double quotes, you can include special character sequences called escape sequences in the string. The main function of the escape sequences is that they allow characters to be included in a string that otherwise could not be represented in the string, and the do this by escaping from the default interpretation of the characters. An escape sequence starts by the backslash character \, and the backslash character cues the compiler to interpret the character that follows in a special way.
Here is a list of all escape sequences and their function.
Escape sequence
The function of escape sequence
Sounds a beep
Single quote
Double quote
Question mark

Obviously, if you want to be able to include a backslash or a double quote as a character to appear a string, you must use the appropriate escape sequences to represent them. Otherwise, the backslash would be interpreted as the start of another escape sequence, and the double quote would indicate the end of the character string.

1.1.3.   Example of Escape Sequence

Open a new project, name it EscapeSequence and write or copy the following code:
// EscapeSequence.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
            char newline = '\n'; //NEWLINE ESCAPE SEQUENCE
            cout << newline;     //START ON A NEW LINE
            cout << "\" We\'ll make our escape in sequence\", she said.";
            cout << "\n\tThe program\'s over, it\'s time to make a beep.\a\a";
            cout << newline;        //START ON A NEW LINE
            return 0;

The first line in main() defines the variable newline and initializes it with a character defined by the escape sequence for a new line. You can then use newline instead of endl from the standard library. After writing newline to cout , you output a string that uses the escape sequences for a double quote ( \ “ ) and a single quote ( \' ). You don ’ t have to use the escape sequence for a single quote here because the string is delimited by double quotes, and the compiler will recognize a single quote character as just that, and not a delimiter. You must use the escape sequences for the double quotes in the string, though. The string starts with a newline escape sequence followed by a tab escape sequence, so the output line is indented by the tab distance. The string also ends with two instances of the escape sequence for abeep, so you should hear a double beep from your PC ’ s speaker.
Share on Google Plus