We'll begin by reviewing the part of the program that we don't have to rewrite:
#include <iostream> #include <string> int main()
{
// ask for the person's name
std::cout << "Please enter your first name: "; // read the name
std::string name; std::cin >> name;
// build the message that we intend to write
const std::string greeting = "Hello, " + name + "!"; // we have to rewrite this part...
return 0; }
As we rewrite the part of the program that the we have to rewrite this part... comment represents, we shall already be in a context that defines name, greeting, and the relevant names from the standard library. We will build up the new version of the program a piece at a time, and then, in §2.5.4/29, we'll put all the pieces together.
2
Looping and counting
In §1.2/11, we developed a program that writes a formatted frame around a greeting. In this chapter, we're going to make the program more flexible so that we can change the size of the frame without rewriting the program.
Along the way, we'll start learning about arithmetic in C++, and how C++ supports loops and conditions, and we'll explore the related idea of loop invariants.
2.1 The problem
The program in §1.2/12 wrote a greeting with a frame around it. For example, if our user gave us the name Estragon, our program would write
******************** * * * Hello, Estragon! * * * ********************
The program built up the output a line at a time. It defined variables named first and second to contain the first and second lines of the output, and wrote the greeting itself, surrounded by some characters, as the third line. We didn't need separate variables for the fourth or fifth output lines, because those were the same as the second and first lines respectively.
This approach has a major shortcoming: Each line of the output has a part of the program-and a variable-that corresponds to it. Therefore, even a simple change to the output format, such as removing the spaces between the greeting and the frame, would require rewriting the program. We would like to produce a more flexible form of output without having to store each line in a local variable.
We will approach this problem by generating each character of the output separately, except for the greeting itself, which we already have available as a string. What we shall discover is that there is no need to store the output characters in variables, because once we have written a character, we don't need it any more.
2.2 Overall structure
We'll begin by reviewing the part of the program that we don't have to rewrite:
#include <iostream> #include <string> int main()
{
// ask for the person's name
std::cout << "Please enter your first name: "; // read the name
std::string name; std::cin >> name;
// build the message that we intend to write
const std::string greeting = "Hello, " + name + "!"; // we have to rewrite this part...
return 0; }
As we rewrite the part of the program that the we have to rewrite this part... comment represents, we shall already be in a context that defines name, greeting, and the relevant names from the standard library. We will build up the new version of the program a piece at a time, and then, in §2.5.4/29, we'll put all the pieces together.
2
Looping and counting
In §1.2/11, we developed a program that writes a formatted frame around a greeting. In this chapter, we're going to make the program more flexible so that we can change the size of the frame without rewriting the program.
Along the way, we'll start learning about arithmetic in C++, and how C++ supports loops and conditions, and we'll explore the related idea of loop invariants.
2.1 The problem
The program in §1.2/12 wrote a greeting with a frame around it. For example, if our user gave us the name Estragon, our program would write
******************** * * * Hello, Estragon! * * * ********************
The program built up the output a line at a time. It defined variables named first and second to contain the first and second lines of the output, and wrote the greeting itself, surrounded by some characters, as the third line. We didn't need separate variables for the fourth or fifth output lines, because those were the same as the second and first lines respectively.
This approach has a major shortcoming: Each line of the output has a part of the program-and a variable-that corresponds to it. Therefore, even a simple change to the output format, such as removing the spaces between the greeting and the frame, would require rewriting the program. We would like to produce a more flexible form of output without having to store each line in a local variable.
We will approach this problem by generating each character of the output separately, except for the greeting itself, which we already have available as a string. What we shall discover is that there is no need to store the output characters in variables, because once we have written a character, we don't need it any more.
2.2 Overall structure
We'll begin by reviewing the part of the program that we don't have to rewrite:
#include <iostream> #include <string> int main()
{
// ask for the person's name
std::cout << "Please enter your first name: "; // read the name
std::string name; std::cin >> name;
// build the message that we intend to write
const std::string greeting = "Hello, " + name + "!"; // we have to rewrite this part...
return 0; }
As we rewrite the part of the program that the we have to rewrite this part... comment represents, we shall already be in a context that defines name, greeting, and the relevant names from the standard library. We will build up the new version of the program a piece at a time, and then, in §2.5.4/29, we'll put all the pieces together.