Home / Programming / C++ Compiler or C++ CONSOLE Input/Output – Operations and Methoods

C++ Compiler or C++ CONSOLE Input/Output – Operations and Methoods

C++ Compiler or C++ CONSOLE Input/Output –

Operations and Methoods

 

Since C++ Compiler or C++ CONSOLE Input/Output is a higher part of C, all elements of the C language are also contained in the C++ language. This implies that all C programs are also C++ programs by default. (Actually, there are some very minor exceptions to this rule, which are discussed later in this book.) Therefore, it is possible to write C++ programs that look just like C programs. While there is nothing wrong with this per se, it does mean that you will not be taking full advantage of C++. To get the maximum benefit from C++, you must write C++ style programs. This means using a coding style and features that are unique to C++ Compiler or C++ CONSOLE Input/Output.

C++ Compiler or C++ CONSOLE InputOutput
C++ Compiler or C++ CONSOLE InputOutput

 

Read – Two versions of C++ programming language? Explained in details

As the most common C++ Compiler or C++ CONSOLE Input/Output specific feature used by C++ programmers is its approach to console I/O. While you may still use functions such as printf() and scanf(), C++ provides a new, and better, way to perform these types of I/O operations. In C++, I/O is performed using I/O operators instead of I/O functions. The output operator is <<and the input operator is >>. As you know, in C, these are the left and right shift operators, respectively. In C++, they still retain their original meanings (left and right shift) but they also take on the expanded role of performing C++ Compiler or C++ CONSOLE Input/Output. Consider this C++ statement:

cout << “This string is output to the screen.\n”;

This statement causes the string to be displayed on the computer’s screen. cout is a predefined stream that is automatically linked to the console when a C++ program begins execution. It is similar to C’s stdout. As in C, C++ Compiler or C++ CONSOLE Input/Output may be redirected, but for the rest of this discussion, it is assumed that the console is being used.

By using the <<output operator, it is possible to output any of C++’s basic types. For example, this statement outputs the value 100.99:

cout << 100.99 ;

In general, to output to the console, use this form of the <<operator:

cout << expression;

 

C++ compiler or CONSOLE I/O

Here, expression can be any valid C++ expression-including another output expression. To input, a value from the keyboard, use the >>input operator. For example, this fragment inputs an integer value into num:

int num; cin >> num;

Notice that num is not preceded by a &. As you know, when you use C’s scanf() function to input values, variables must have their addresses passed to the function so they can receive the values entered by the user. This is not the case when you are using

C++ compiler

’s input operator. (The reason for this will become clear as you learn more about C++.) In general, to input values from the keyboard, use the form if >>:

cin >> variable;

Note: The expanded roles of <<and >>are examples if operator overloading.

In order to use the C++ Compiler or C++ CONSOLE Input/Output operators, you must include the header <iostream> in your program. As explained earlier, this is one of C++’s standard headers and is supplied by your C++ compiler.

EXAMPLES

This program outputs a string, two integer values, and a double floating-point value:

#include <iostream> using namespace std;

int main()

{

int i, j; double d;

i = 10 ; j = 20 ; d = 99.101 ;

cout << “Here are some values: “; cout << i; cout << ’ ’; cout << j; cout << ’ ’; cout << d;

return 0 ;

}

The output of this program is shown here.

Here are some values: 10 20 99.101

Remember: If you are working with an older compiler, it might not accept the new-style headers and the namespace statements used by this and other programs in this book. If this is the case, substitute the old-style code described in the preceding section.

It is possible to output more than one value in a single I/O expression. For example, this version of the program described in Example 1 shows a more efficient way to code the I/O statements:

#include <iostream> using namespace std;

int main()

{

int i, j; double d;

i = 10 ; j = 20 ; d = 99.101 ;

cout << “Here are some values: “;

cout << i << ’ ’ << j << ’ ’ << d;

return 0 ;

}

Here the line

cout << i << ’ ’ << j << ’ ’ << d;

outputs several items in one expression. In general, you can use a single statement to output as many items as you like. If this seems confusion, simply remember that the >>output operator behaves like any other C++ Compiler or C++ CONSOLE Input/Output operator and can be part of an arbitrarily long expression.

Notice that you must explicitly include spaces between items when needed. If the spaces are left out, the data will run together when displayed on the screen.

This program prompts the user for an integer value:

#include <iostream> using namespace std;

int main()

{

int i;

cout << “Enter a value: “; cin >> i; cout << “Here;s your number: | << i << “\n”;

return 0 ;

}

Here is a sample run:

Enter a value: 100

Here’s your number: 100

As you can see, the value entered by the user is put into i.

The next program prompts the user for an integer value, a floating-point value, and a string. It then uses one input statement to read all three.

 

C++ CONSOLE I/O

#include <iostream> using namespace std; int   main()
{ int i; float f; char s[80] ;
cout << “Enter an integer, float, and string: “;
cin >> i >> f >> s;
cout << “Here’s your data: “;
cout << i << ’ ’ << f << ’ ’ << s;
} return 0 ;

As this example illustrates, you can input as many items as you like in one input statement. As in C, individual data items must be separated by whitespace characters (spaces, tabls, or newlines).

When a string is read, the input will stop when the first whitespace character is encountered. For example, if you enter the following into the preceding program

10 100.12 This is a test the program will display this:

10 100.12 This

The string is incomplete because the reading of the string stopped with space after This. The remainder of the string is left in the input buffer, awaiting a subsequent input operation. (This is similar to inputting a string by using scanf() with the %s format.)

By default, when you use >>, all input is line buffered. This means that no information is passed to your C++ Compiler or C++ CONSOLE Input/Output program until you press ENTER. (In C, the scanf() function is line buffered, so this style of input should not be new to you.) To see the effect of line-buffered input, try this program:

#include <iostream> using namespace std;

int main()

{

char ch;

cout << “Enter keys, x to stop.\n”; do

{

cout << “: “; cin >> ch;

} while(ch!=’x’);

return 0 ;

}

[contact-form][contact-field label=”Name” type=”name” required=”true” /][contact-field label=”Email” type=”email” required=”true” /][contact-field label=”Website” type=”url” /][contact-field label=”Message” type=”textarea” /][/contact-form]

When you test this program, you will have to press ENTER after each key you type in order for the corresponding character to be sent to the program.

 

Read My Other Posts

 

1.B logging For Starters – Learn How to start a successful blog in 2018

2.Learn C++ Proggraming Within 14 Days ( Introduction )

About rhtoufiq

Check Also

WHAT IS OBJECT ORIENTED PROGRAMMING?

WHAT IS OBJECT ORIENTED PROGRAMMING?   Object-oriented programming is a powerful way to approach the …

5 comments

  1. Just how long do you’ve planned on being with AC and why would
    you? Write about what perception best, as well as it is really as.
    In fact, why don’t you’re going and see independently. https://scr888.group/other-games-download/2493-download-sky777

  2. We have seen this because we have moved not in the natural production of
    meat to industrial levels. Now, I’m not going to speculate on whether that’s a
    Bush-ian slip, or a great honest-to-god wrong doing. https://scr888.group/live-casino-games/2485-3win8

  1. Pingback: Google

Leave a Reply