Chapter 1.
1.0 Compile, execute and test the programs in this chapter.
Done.
1.1 Are the following definitions valid? Why or why not?
Code:
const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";
Yes the "+" sign is correctly used in the second definition to attach multiple string to each other including the first definition.
1.2 Is the following program valid? Why or why not?
Code:
const std::string exclam = "!";
const std::string message = "Hello" + ", world" + exclam;
Both definitions are valid. In the first definition it states that exclam means "!" and in the second it uses this behind two other string that it attached to each other.
1.3 Is the following program valid? Why or why not?
Code:
#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;
}
Yes, first it builds the string "s" and displays it. Because it encounters the closing brace (}) all variables in that block will be destroyed meaning the part of the buffer that held the string gets flushed. Then it builds a new string stored in the variable "s" and displays it.
1.4 What about this one? What if we change "}}" to };} at the third line from the end?
Code:
#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;
}
No, the block of the main function is closed before the return statement meaning the statement falls outside the scope of the function.
1.5 Is this program valid? If so what does it do? If not, say why not, and rewrite it to be valid.
Code:
#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;
}
It does not work. The string "x" falls outside of the curly braces meaning its lifetime has ended and it can not be accessed. The correct way to write it would be.
Code:
#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;
}
1.6 What does the following program do if, when we ask you for input, you type two names (for example, Samuel Beckett)? Predict the behavior before running the program, then try it.
Code:
#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 to!" << std::endl;
return 0;
}
Prediction: It will stop reading the input at "Samuel", soon as the white space occurs it will stop reading into the buffer. As soon as the second name is given in the space in the buffer holding the string "Samuel" will be overwritten.
Outcome: It seems that after writing the name and hitting the enter key it skips asking for a name the second time and uses the name "Beckett" instantly.
I have no idea why this happens an explanation would be more then welcome.