C++ 3
C++ 3
You have already learned that cout is used to output (print) values. Now we will
use cin to get user input.
cin is a predefined variable that reads data from the keyboard with the
extraction operator (>>).
In the following example, the user can input a number, which is stored in the
variable x. Then we print the value of x:
Example
#include <iostream>
using namespace std;
int main() {
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x;
return 0;
}
Run example »
Good To Know
cout is pronounced "see-out". Used for output, and uses the insertion operator
(<<)
cin is pronounced "see-in". Used for input, and uses the extraction operator (>>)
In this example, the user needs to input two numbers, and then we print the
sum:
Example
#include <iostream>
using namespace std;
int main() {
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
return 0;
}
Run example »
Example
#include <iostream>
#include <string>
using namespace std;
int main () {
// Creating variables
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myString = "Hello"; // String
float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient
for storing 7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient
for storing 15 decimal digits
Use int when you need to store a whole number without decimals, like 35 or
1000, and float or double when you need a floating point number (with
decimals), like 9.99 or 3.14515.
int
#include <iostream>
using namespace std;
int main () {
int myNum = 1000;
cout << myNum;
return 0;
}
Run example »
float
#include <iostream>
using namespace std;
int main () {
float myNum = 5.75;
cout << myNum;
return 0;
}Run example »
double
#include <iostream>
using namespace std;
int main () {
double myNum = 19.99;
cout << myNum;
return 0;
}
Run example »
float vs. double
The precision of a floating point value indicates how many digits the value can
have after the decimal point. The precision of float is only six or seven decimal
digits, while double variables have a precision of about 15 digits. Therefore it is
safer to use double for most calculations.
Scientific Numbers
A floating point number can also be a scientific number with an "e" to indicate
the power of 10:
Example
#include <iostream>
using namespace std;
int main () {
float f1 = 35e3;
double d1 = 12E4;
cout << f1 << "\n";
cout << d1;
return 0;
}
Run example »
Booleans
A boolean data type is declared with the bool keyword and can only take the
values true or false. When the value is returned, true = 1 and false = 0.
Example
#include <iostream>
using namespace std;
int main() {
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun << "\n";
cout << isFishTasty;
return 0;
}
Run example »
Boolean values are mostly used for conditional testing, which you will learn
more about in a later chapter.
Characters
The char data type is used to store a single character. The character must be
surrounded by single quotes, like 'A' or 'c':
Example
#include <iostream>
using namespace std;
int main () {
char myGrade = 'B';
cout << myGrade;
return 0;
}
Run example »
Example
#include <iostream>
using namespace std;
int main () {
char a = 65, b = 66, c = 67;
cout << a;
cout << b;
cout << c;
return 0;
}
Run example »
Tip: A list of all ASCII values can be found in our ASCII Table Reference.
Strings
The string type is used to store a sequence of characters (text). This is not a
built-in type, but it behaves like one in its most basic usage. String values must
be surrounded by double quotes:
Example
string greeting = "Hello";
cout << greeting;
To use strings, you must include an additional header file in the source code,
the <string> library:
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string greeting = "Hello";
cout << greeting;
return 0;
}
Run example »
You will learn more about strings, in our C++ Strings Chapter.
C++ Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Example
#include <iostream>
using namespace std;
int main() {
int x = 100 + 50;
cout << x;
return 0;
}
Run example »
Although the + operator is often used to add together two values, like in the
example above, it can also be used to add together a variable and a value, or a
variable and another variable:
Example
#include <iostream>
using namespace std;
int main() {
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
cout << sum1 << "\n";
cout << sum2 << "\n";
cout << sum3;
return 0;
}
Run example »
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators