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 ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    62

    C++ Accelerated Questions Chapter 1.

    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.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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:
    Code:
    const std::string message = std::string("Hello") + ", world" + exclam;
    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.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    62
    Quote Originally Posted by EVOEx View Post
    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:
    Code:
    const std::string message = std::string("Hello") + ", world" + exclam;
    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?
    Quote Originally Posted by EVOEx View Post
    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.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    62
    So if I get it right "hello" result in an array and ("hello") would result in a string (std::string) and it is not allowed or impossible to add two arrays together using "+"?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, ("hello") is still an array. Parentheses don't have an effect on the type. It's std::string("hello") that results in a string.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    Quote Originally Posted by Kitt3n View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Not quite. >>string works like this:
    1) Skip in the stream until non-whitespace is found.
    2) Read until whitespace is found. Don't remove whitespace from stream.

    So:
    cin >> str1 reads "Hello". " World\n" is left in the input buffer.
    cin >> str2 skips " " and reads "World". "\n" is left in the input buffer.

    That's the entire problem behind >> and getline mixing.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Accelerated Questions Chapter 0.
    By Kitt3n in forum C++ Programming
    Replies: 23
    Last Post: 07-27-2010, 02:24 PM
  2. malloc problem
    By Tool in forum C Programming
    Replies: 23
    Last Post: 03-12-2010, 09:54 AM
  3. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  4. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21