C++ Tutorial
C++ Output
cout sends text to the console. endl and \n start a new line. This is how every first program talks back.
cout writes to the console
cout is the standard output stream. You send values to it with the insertion operator<<. Text goes in double quotes. After the program runs, you see those characters in the console — on StudyGrid, that is the output pane under the editor.
Example
#include <iostream>
using namespace std;
int main() {
cout << "Hello from cout";
return 0;
}Open examples with Try it in C++. That is /cpp/try: a g++ compile-and-run editor. It.
Chain several pieces with <<
One cout can take many insertions. Each << adds the next value to the same line unless you insert a newline. Mixing strings and numbers is normal.
Example
#include <iostream>
using namespace std;
int main() {
cout << "Score: " << 12 << " points" << endl;
cout << 3 << " plus " << 4 << " is " << 3 + 4 << endl;
return 0;
}The order is left to right. First the label, then the number, then more text. You do not need a separatecout for each value.
endl versus \n
Both start a new line. endl writes a newline and flushes the output buffer. \n is just the newline character inside a string. For these lessons either is fine. This tutorial uses endl in most complete programs so each line is easy to see.
Example
#include <iostream>
using namespace std;
int main() {
cout << "first line" << endl;
cout << "second line\n";
cout << "third line\nfourth line" << endl;
return 0;
}Without a newline, the next cout continues on the same line. Forget that and two messages glue together: HelloWorld instead of two words.
Numbers, characters, and strings
cout prints integers, floating-point values, single characters, and quoted text. Adouble may show without trailing zeros. A char prints as a glyph, not as a number, when you store a character literal.
Example
#include <iostream>
using namespace std;
int main() {
int count = 7;
double price = 2.5;
char grade = 'A';
cout << count << endl;
cout << price << endl;
cout << grade << endl;
cout << "done" << endl;
return 0;
}Spaces are your job
C++ does not insert spaces between chained values. If you write cout << "Hi" << "there", you get Hithere. Put a space in a string, or insert " ", wherever a gap should appear.
| Code | What you see |
|---|---|
cout << 2 << 5; | 25 |
cout << 2 << " " << 5; | 2 5 |
cout << "n=" << 3; | n=3 |
You need iostream
cout lives in <iostream>. Include that header at the top. Withusing namespace std; you write cout; without it you write std::cout. This track keeps the shorter form.
Example
#include <iostream>
int main() {
std::cout << "same stream, longer name" << std::endl;
return 0;
}Next: comments, so you can leave notes next to the lines that print.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.
Physics
Cells in series
Identical cells in series add their emfs. Four 1.5 V cells give 6.0 V, ignoring internal resistance. The count of cells is an int. The voltage is not.
cout prints both on one line. Setprecision belongs later; here the default already shows 6.
V_total = n × V_cell
Real batteries sag under load. This is the open-circuit sum, not what a motor would actually see.
Example
#include <iostream>
using namespace std;
int main() {
int cells = 4;
double volts = 1.5 * cells;
cout << cells << " cells -> " << volts << " V" << endl;
return 0;
}Astronomy
A Mars year
Mars takes about 687 Earth days to go once around the Sun. That is a sidereal year, measured against the stars, not a calendar date on Mars.
cout can mix a name and an integer on one line. Mission logs look like this: an object, then a number, then a unit implied by the sentence.
Example
#include <iostream>
using namespace std;
int main() {
cout << "Mars year ~ " << 687 << " Earth days" << endl;
return 0;
}