Thread: C++ Accelerated Questions Chapter 2.( II )

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    7

    Red face C++ Accelerated Questions Chapter 2.( II )

    Hi im new, Im getting crazy and playing with this values (2+3, 2+2) what are they!>?:

    Code:
       const string::size_type cols = greeting.size() + spacer * 2 + 2;
        const int rows = pad * 2 + 3;
    Code:
    #include<iostream>#include<string>
    
    
    using std::cout;        using std::cin;
    using std::endl;        using std::string;
    
    
    int main()
    {
        // Ask the name of the person.
        cout << "Please enter your name: ";
    
    
        // Read the name.
        string name;
        cin >> name;
    
    
        // Build the greeting.
        const string greeting = "Hello, " + name + "!";
    
    
        // Set the ammount of spacing on the middle line between the stars and the greeting.
        const int spacer = 10;
    
    
        // Set the amount of space for the padding.
        const int pad = 2;
    
    
        // The amount of stars to be printed above and beneath the greeting (cols) and tbe amount of rows.
        const string::size_type cols = greeting.size() + spacer * 2 + 2;
        const int rows = pad * 2 + 3;
    
    
        // Write a blank line to seperate input from output.
        cout << endl;
    
    
        // Write rows of output.
        for(int r = 0; r != rows; ++r)
        {
            string::size_type c = 0;
    
    
            while(c != cols)
            {
                // Is it time to write the greeting?
                if(r == pad + 1 && c == spacer +1)
                {
                    cout << greeting;
                    c += greeting.size();
                }
                else
                {
                    // Are we on the border?
                    if(r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
                        cout << "*";
                    else
                    cout << " ";
                        ++c;
                }
            }
            cout << endl;
        }
        return 0;
    }
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... the comments should already explain what they are. Note that spacer * 2 + 2 is equivalent to (spacer * 2) + 2, not spacer * (2 + 2).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Err... the comments should already explain what they are. Note that spacer * 2 + 2 is equivalent to (spacer * 2) + 2, not spacer * (2 + 2).

    Thank you very much @laserlight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Accelerated Questions Chapter 2.
    By Kitt3n in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2010, 07:13 AM
  2. C++ Accelerated Questions Chapter 0.
    By Kitt3n in forum C++ Programming
    Replies: 23
    Last Post: 07-27-2010, 02:24 PM
  3. C++ Accelerated Questions Chapter 1.
    By Kitt3n in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2010, 06:23 PM
  4. Help with Accelerated C++ exercise
    By s_siouris in forum C++ Programming
    Replies: 5
    Last Post: 01-24-2010, 03:10 PM
  5. Few questions from "Accelerated C++"
    By s_siouris in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2008, 02:07 PM

Tags for this Thread