Step 1 of 5 · Reading · ~3 min
Learn
Getting Started
Hello, World
By the end of this lesson you can write a complete C++ program that reads words from the keyboard with std::cin and prints them back inside a sentence with std::cout.
The smallest program
Every C++ program starts running at main. Here is one that prints a fixed line:
#include <iostream>
int main() {
std::cout << "Good morning" << "\n";
return 0;
}
Output:
Good morning
#include <iostream>pulls in the standard input/output stream library, which is wherestd::cinandstd::coutlive. You include the headers you use.int main()is the entry point. Returning0tells the operating system the program succeeded.std::coutis the standard output stream. The operator<<inserts a value into it. You can chain as many<<as you like; each piece is written in order, with nothing added between them."\n"is the newline character. Without it, the next thing you print continues on the same line.std::endlalso ends the line but additionally flushes (forces the text out immediately);"\n"is the lighter choice.
std:: is a namespace: the standard library's family name, so its names never collide with yours. This course always writes std:: explicitly.
Chaining pieces: spaces are your job
<< glues pieces together exactly as given. It never inserts a space for you:
std::cout << "Tea" << "time" << "\n"; // prints: Teatime
std::cout << "Tea" << " " << "time" << "\n"; // prints: Tea time
Reading words with std::cin
std::cin is the standard input stream, and >> extracts from it. Reading into a std::string (from the <string> header) takes one token: a run of characters up to the next whitespace. Any whitespace (spaces, tabs, newlines) before the token is skipped.
#include <iostream>
#include <string>
int main() {
std::string animal;
std::string sound;
std::cin >> animal >> sound;
std::cout << "The " << animal << " says " << sound << ".\n";
return 0;
}
With the input cow moo this prints:
The cow says moo.
Trace it: the first >> skips nothing, reads cow, stops at the space. The second >> skips the space, reads moo. Because whitespace of any kind is skipped, the input cow on one line and moo on the next gives the same result, and so does cow moo.
Variables are filled in the order you read them. Writing std::cin >> sound >> animal; with the same input puts cow into sound, and the program prints The moo says cow.
Compile and run
$ g++ -Wall -o greet greet.cpp
$ ./greet
Always compile with -Wall. When g++ reports errors, read the first one only, fix it, recompile; later errors are often echoes of the first.
Common mistakes
- Missing a space inside a literal.
<< name << "is learning"printsAdais learning. Put the space inside the quotes:" is learning ". - Forgetting a
"\n"between two lines. Both sentences run together on one line, and the exact-output check fails. - Reading in the wrong order. The first
>>receives the first word of the input. Swapping the reads swaps the words in the output. - Printing a fixed example instead of the input. A program that always prints the sample answer fails as soon as the input changes; the hidden tests use different names.
Your exercise
Read a person's name and a language name, then print a two-line greeting built from them. The starter declares both std::string variables; you write the read and the output chain. Watch the spaces and punctuation: they must match character for character.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…