C++ Accelerated Questions Chapter 1.

This is a discussion on C++ Accelerated Questions Chapter 1. within the C++ Programming forums, part of the General Programming Boards category; Chapter 1.
1.0 Compile, execute and test the programs in this chapter.
Done.
1.1 Are the following definitions valid? Why ...

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?

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?

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.

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.

1.2 and 1.6 are wrong (also, there's a quote missing in two pieces of code but I assume that is a typo on your end).

1.2 is invalid because "Hello" is of the type "const char*", which does not have an operator+. The + operator is only supported on types that have it, such as an std::string. This, for instance, would be valid:

1.2 is invalid because "Hello" is of the type "const char*", which does not have an operator+. The + operator is only supported on types that have it, such as an std::string. This, for instance, would be valid:

There I am missing it. How is the string "hello" different from ", world"? Or did you take the variable "hello" from exercise 1.1. Meaning it is a constant and the "+" operator can not be used on constant variables and therefor you create a new string "Hello" in your example?

Originally Posted by EVOEx

1.6... This happens because "std::cin"'s operator>> to a string stops reading at a space, not at the end of the line. So if you type "A[space]B", it will read A the first time, then B the second time.

Ok, I thought that what was behind the space would get trashed: I did not presumed it would be used again at a later time in the program.

A string literal looks like this:
"Hello"
It's type is const char[6], i.e. an array of six characters. In most situations, this will decay to a const char*, a pointer to the first element of that array.

Therefore, what "Hello" + ", World" does is try to add two pointers together. This is not valid.

std::string("Hello"), on the other hand, constructs a std::string from the string literal. The type of this expression is std::string. The expression std::string("Hello") + ", World" has a std::string on the left side, so the + operator for std::string + const char* is used to resolve this operation, and this operator does proper string concatenation.

Ok, I thought that what was behind the space would get trashed: I did not presumed it would be used again at a later time in the program.

See it like this:
Everytime you call cin >>, it will try to read from the input buffer. More specifically, it will read until it hits a whitespace.
If the input buffer is empty, it will try to read some data from the user and save it into the buffer. This buffer is line buffered, so it will read an entire line.
cin >> will extract a token, and when reading into a string, stops at the next whitespace.

So if you enter: Hello World
cin >> str1 reads "Hello". "World" is left in the input buffer.
cin >> str2 reads "World". Nothing is left in the input buffer.