Modern C++: The Good Parts/The world's response
Now, let's allow the world to return the greeting.
Code
[edit | edit source]#include <iostream>
#include <string>
int main()
{
std::string input;
std::cout << "Hello, world!\n";
// Notice the arrows point to the right.
std::cin >> input;
std::cout << "The world says: " << input << "\n";
}
Explanation
[edit | edit source]We have a few new things here.
std::string
means that input
is a string variable. A variable is a named place in memory to store information. Since it's named, you can refer to it again and again, to put something there or to see what's already there. And as a string variable it can store text - and only text. Other kinds of variables exist, and we'll cover some of those in the next chapter.
//
is a line comment, which means everything after it on the same line will be ignored by the compiler.
std::cin
has an arrow that points in the opposite direction from the arrows of std::cout
, because information is flowing in the opposite direction.
After "The world says: "
, there's another arrow, and then another. This just appends more text to the output.
Exercises
[edit | edit source]- Extend the conversation between your program and the "world". Don't bother with whether the program is responding correctly to what is typed.
Vocabulary
[edit | edit source]- variable
- a named place in memory to store information.
- line comment
- causes the compiler to ignore the rest of a line. Syntax:
//