- C++ Keywords
- C++ Preprocessor
- Comments
- The main() function
- Namespaces
- Basic I/O using cin and cout
- cout and <<
- cin and >>
- cout examples
- cout, cin example1 using integers
- cout, cin example2 using integers
- cout, cin example3 using integers
- cout, cin example4 using doubles
- cout, cin example5 using integers and doubles
C++ has over 90 keywords. These are words that are reserved and cannot be used for variables, function names etc. We have seen the use of int and return keywords.
Identifiers are names created by programmers, such as a variable names or built-in library objects such as cout, cin or namespaces etc.
Identifiers cannot start with a number or have spaces.
Preprocessor examples
#include <iostream>
#include "myfile.h"The C++ preprocessor is a program that processes source code before the compiler.
-
It strips all comments from the source file, and replaces a comment with a single space.
-
It executes any preprocessor directives.
-
It removes any preprocessor directives from the source code.
Single line comment
// This is a single line comment.Multiple line comment
/*
This is
a multiple
line comment.
*/-
Every
C++program must have exactly 1 main() function. -
This is the starting point of the program.
-
return 0 indicates successful program execution
main() versions
int main()
{
// code
return 0;
}For accepting arguments at program execution via command line.
int main(int argc, char *argv[])
{
// code
return 0;
}Namespaces are used to prevent naming conflicts. std::cout the programmer explicitly declares the namespace std and object count for use in the source code.
cout usage not using a namespace directive
std::cout << "Enter your favorite number between 1 and 100? ";To save typing std::cout repeatedly, use a namespace directive.
Use all objects from the std(standard) namespace, not recommended.
#include <iostream>
using namespace std;Use only the objects used from the std(standard) namespace.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;cout usage using a namespace directive
cout << "Enter your favorite number between 1 and 100? ";preprocessor #include <iostream> required.
cout, cin, cerr and clong are objects representing streams.
cout
- standard output stream
- to console
cin
- standard input stream
- default from keyboard, ignores whitespace
<<
- insertion operator
- output to stream
>>
- extraction operator
- input from stream
- Insert the data variable into the
coutstream
cout << data;- Can be chained
cout << "data 1 is " << data1;- Does not automatically add line breaks
cout << "data 1 is " << data1 << endl>>;
cout << "data 1 is " << data1 << "\n">>;endl will also flush the stream.
- Extract data from
cinstream based on data's type.
cin >> data;if data is an integer then an integer is stored.
if data is an double then a real number is stored.
- can be chained.
cin >> data1 >> data2;-
New lines (pressing enter) terminate the extraction from cin.
-
Spaces entered on console, will be ignored.
-
It can fail if the type entered cannot be interpreted. For example
datais of typeint, but astringis entered on the console.datawill have an undetermined value.
#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout << "Hello world!"; // No newline
cout << "Hello world!" << endl;
cout << "Hello "; // No newline
cout << "world!" << endl;
cout << "Hello" << " world!" << endl;
cout << "Hello" << " world!\n"; // uses \n as newline
cout << "Hello\nOut\nThere\n"; // uses \n aas newline
return 0;
}Output
Hello world!Hello world!
Hello world!
Hello world!
Hello world!
Hello
Out
There#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int num1;
cout << "Enter an integer: ";
cin >> num1;
cout << "You entered: " << num1 << endl;
return 0;
}Output
Enter an integer: 7
You entered: 7#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int num1;
int num2;
cout << "Enter an integer: ";
cin >> num1;
cout << "Enter a second integer: ";
cin >> num2;
cout << "You entered " << num1 << " and " << num2 << "." << endl;
return 0;
}Output
Enter an integer: 45
Enter a second integer: 23
You entered 45 and 23.Here, cin is stored in a buffer, the buffer is not flushed, therefore the second integer is read from the buffer into num2, without the prompt being needed.
Output
Enter an integer: 23 99
Enter a second integer: You entered 23 and 99.Here, cin is chained to retrieve two integers from the buffer. This saves having to prompt the user multiple times.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int num1;
int num2;
cout << "Enter two integers separated by a space: ";
cin >> num1 >> num2; // cin chained
cout << "You entered " << num1 << " and " << num2 << "." << endl;
return 0;
}Output
Enter two integers separated by a space: 34 78
You entered 34 and 78.#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
double num3;
cout << "Enter a double: ";
cin >> num3;
cout << "You entered " << num3 << endl;
return 0;
}Here, the user entered a space between the double, so only the first part of the number was stored.
Output
Enter a double: 45 . 78
You entered 45Output
Enter a double: 45.6
You entered 45.6Here, a integer was entered and a integer is displayed.
Output
Enter a double: 23
You entered 23#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int num1;
double num3;
cout << "Enter an integer: ";
cin >> num1;
cout << "Enter a double: ";
cin >> num3;
cout << "The integer is: " << num1 << endl;
cout << "And the double is: " << num3 << endl;
return 0;
}Enter an integer: 10
Enter a double: 12.5
The integer is: 10
And the double is: 12.5Here, a integer 10 and a double .5 is found in the input stream and stored without the prompt need for the double prompt.
Enter an integer: 10.5
Enter a double: The integer is: 10
And the double is: 0.5Here, a string was entered not a integer, with unpredicted results.
Enter an integer: hello world
Enter a double: The integer is: 0
And the double is: 6.8991e-310Note: In the real world cin would not be used to read numbers, a string would be read and then cast into a number.