Accelerated C++ - Chapter 1
Solutions to exercises in Chapter 1, "Working with strings."
Solutions to exercises in Chapter 1, "Working with strings."
Are the following definitions valid? Why or why not?
const std::string hello = "Hello"; const std::string message = hello + ", world" + "!";
Solution
Yes, these declarations are valid. The first line initializes the variable
hello to the string "Hello". The second line
initializes the variable message with a concatenation of hello,
the string ", world", and the string "!".
The second line works because the + operator is left-associative. First it concatenates
hello and ", world", which yields a std::string
value. This value is then concatenated with "!" to yield the value with which
message is initialized.
Are the following definitions valid? Why or why not?
const std::string exclam = "!"; const std::string message = "Hello" + ", world" + exclam;
Solution
The first line is valid because it initializes the variable
exclam with the value "!". The second line is not
valid, however. The reason is that the + operator is
left-associative. This means that it is being used to concatenate "Hello" and
", world", which is not legal because one of the operands must be a
std::string object. In the exercise, "Hello" and ",
world" are both string literals.
Is the following program valid? Is so, what does it do? If not, why not?
#include <iostream>
#include <string>
int main()
{
{ const std::string s = "a string";
std::cout << s << std::endl; }
{ const std::string s = "another string";
std::cout << s << std::endl; }
return 0;
}
|
Solution
Yes, this is a valid program. It contains two scopes, and in each scope there is
a variable of type std::string named s. The variable ceases to exist
at the end of each scope. The output of the program is as follows:
a string another string
What about this one? What if we change }} to };} in the third line from the end?
#include <iostream>
#include <string>
int main()
{
{ const std::string s = "a string";
std::cout << s << std::endl;
{ const std::string s = "another string";
std::cout << s << std::endl; }}
return 0;
}
|
Solution
Yes, this is a valid program. There is a scope nested inside of an outer scope.
Because the inner scope defines a variable named s, it hides the variable of the
same name that is defined in the outer scope.
Changing the }} to };} in the third line from the end will have no effect on the meaning of the program. The semicolon denotes an empty statement in this situation.
#include <iostream>
#include <string>
int main()
{
{ const std::string s = "a string";
std::cout << s << std::endl;
{ const std::string s = "another string";
std::cout << s << std::endl; };}
return 0;
}
|
The output of the program is as follows:
a string another string
Is this a valid program? If so, what does it do? If not, say why not, and rewrite it to be valid.
#include <iostream>
#include <string>
int main()
{
{ std::string s = "a string";
{ std::string x = s + ", really";
std::cout << s << std::endl; }
std::cout << x << std::endl;
}
return 0;
}
|
Solution
This is not a valid program because the variable x is declared inside
a scope, and it is unavailable when the scope ends. Because of this, the line which attempts to
output x will not compile.
To make this program succeed, the simplest fix is to keep the code in one scope by removing the innermost braces, as follows:
#include <iostream>
#include <string>
int main()
{
{ std::string s = "a string";
std::string x = s + ", really";
std::cout << s << std::endl;
std::cout << x << std::endl;
}
return 0;
}
|
The output of the program is as follows:
a string a string, really
What does the following program do if, when it asks you for input, you type two names
(for example, Samuel Beckett)? Predict the behavior before running the program, then try it.
#include <iostream>
#include <string>
int main()
{
std::cout << "What is your name? ";
std::string name;
std::cin >> name;
std::cout << "Hello, " << name
<< std::endl << "And what is yours? ";
std::cin >> name;
std::cout << "Hello, " << name
<< "; nice to meet you, too!" << std::endl;
return 0;
}
|
SolutionThe first std::cin line will read input until it encounters a space, and
store the result in the variable name. The rest of the input remains buffered. The
second std::cin line will read the remaining name after the space without pausing to ask for more
input. The output of the program is as follows:
What is your name? Samuel Beckett Hello, Samuel And what is yours? Hello, Beckett; nice to meet you, too!