In this chapter we shall look into below topics:
- Basic C++ input and Output:
- Standard error
- Standard Log
- Type Casting with “cout”
- get() and put() functions
- getline() and write()
- C++ comments
- C++ new line
1. In C++ input and output occurs in stream of bytes.
If the stream of sequence of bytes go from keyboard to the main memory is called as input stream.
If the stream of sequence of bytes’ flows from main memory to console display or a printer is called as output stream.
iostream header file will provide with “cin” for input operations and “cout” “cerr” “clog” for output operations. Specifically, istream provides with “cin” and ostream provides with “cout” “cerr” “clog”.
Basic C++ input and Output:
#include<iostream> using namespace std; int main() { int age; cout <<"Please enter age"<<endl; cin >> age; cout <<"The entered age is "<<age<<endl; return 0; }
Output:
Please enter age 22 The entered age is22
We use cin for reading input and cout for output. C++ compiler will determine the data-type of the variable, we no longer need to use format specifier like “%d” or “%s” we use in c language. We will use extraction operator “>>” along with cin to accept a value from console and insertion operator “<<” with cout to print value to console.
2. Standard error:
We use “cerr” to display an error. “crr” is connected to standard error device. This is un-buffered stream; this means the error is displayed immediately as it occurs.
Example:
#include<iostream> using namespace std; int main() { int age; cerr <<"This is an error"<<endl; return 0; }
Output:
This is an error
3. Standard Log
We use “clog” to display an error. “clog” is connected to standard error device. This is buffered stream. This means the log is displayed once the buffer is filled.
Example:
#include<iostream> using namespace std; int main() { int age; clog <<"This is a log"<<endl; return 0; }
Output:
This is a log
4. Type Casting with “cout”:
In the below example, shows how to typecast different data-types with cout.
#include<iostream> using namespace std; int main() { cout << (char) 65<<endl; // converts integer to its corresponding character value cout << (int) 'A'<<endl; // converts character to its corresponding integer value cout << (int) 65.55<<endl; // converts float to its corresponding integer value cout << (char) 66.66<<endl; // converts float to its corresponding character value cout << (double) 12345678.87654<<endl; // converts float to its corresponding double value cout << (unsigned) -1<<endl; // converts signed value to its corresponding unsigned value return 0; }
Output:
A 65 65 B 1.23457e+07 4294967295
5. get() and put() functions:
Both of these functions are used to accept a single character and print a single character respectively.
get(): It is used to accept single character. The function has 2 variants.
- get(char*): This will assign the input character to the argument given.
- get(void): This will return the input character, so the data should be assigned to the variable on left-hand side.
put(): It is used to display a single character to output.
Example to show usage of get() and put():
#include<iostream> using namespace std; int main() { char ch; cout<<"Enter a character"<<endl; cin.get(ch); cout<<"The entered character is "; cout.put(ch)<<endl; return 0; }
Output:
Enter a character d The entered character is d
6. getline() and write():
Both of the above functions are used to read whole line from input and print a line to output.
getline function prototype:
cin.getline(variable, input_size);
write function prototype:
cout.write(variable, output_size);
#include<iostream> using namespace std; int main() { char ch[60]; cout<<"Enter a sentence. Press enter to end the sentence"<<endl; cin.getline(ch, 60); cout<<"The entered sentence is "<<endl; cout.write(ch, 60)<<endl; return 0; }
Output:
Enter a sentence. Press enter to end the sentence Hello from prodevelopertutorial.com The entered sentence is Hello from prodevelopertutorial.com
7. C++ comments:
Comments provide a developer to understand the program. It is always considered good programming practice if your program is well commented. It is a general practice to write small description before a function.
The comments are skipped during compilation. They are not part of object file.
In C++ we have 2 types of comments.
- Single line comment
- Multi-line comment
Comments in C++ will be ignored during compiling.
#include<iostream> using namespace std; int main() { // This is a single line comment /* *This is a multi line comment * *cout<< "This will not be executed"<<endl; *cout<< "This also, because it is commented section"; * * */ return 0; }
8. C++ New Line
To insert a new line, either use “\n” inside cout or use “endl” outside cout.
Example of new line using “\n”.
#include <iostream> using namespace std; int main() { cout<<"\n New line using \\n character"; return 0; }
Output:
New line using \n character
Example of new line using “endl”.
#include <iostream> using namespace std; int main() { cout<<"New line using endl"<<endl; return 0; }
Output:
New line using endl