7. Introduction to c++: Basic input and output in C++ :: Videos
Description:
C++ Basics #7 Basic Input and Output
Topics covered in this video:
- Standard output (cout)
- Standard input (cin)
- stringstream
What is input and output?
-
Interaction with the user.
-
Changing program according to user input.
-
C++ uses streams to perform input and output.
-
Stream is object where an object can either insert or extract characters to/from it.
-
C++ have iostream file in library where the input and output streams objects are declared.
- Standard Output (cout):
-
Default standard output is screen.
-
To give output we use ‘cout’ with << (Two less than signs)
-
Example:
-
cout << 23;
(gives 23 in screen)
-
cout << x;
(prints the value of x to screen)
-
cout << “Hello world!”
(prints Hello World to screen)
-
cout << “My age is: “ << age;
-
Using endl or “n” will take output to next line.
- Standard Input(cin):
-
cin is used with >> to get a input from the stream.
-
Example: cin >> VariableName(To store the input)
-
To request more than one datum we can use multiple >>
-
Example:
cin >> data1 >> data2;
Which is same as
cin >> data1;
cin >> data2;
-
When getting string with cin, it will stop taking character as soon as it gets to a blank space. Use getline() instead
-
Using same variable to get input in different places will just replace the previous value of variable with new one.
- stringstream
|