this code is for bring an output of "Hello, [name]!" with frame on its outside.

//ask for a person's name, and generated a frame greeting.

#include <iostream>
#include <string>

// say what standard-library names we use
using std::cin;         using std::endl;
using std::cout;        using std::string;

int main()
{
    // ask for the person's name
    cout << "Please enter your first name: ";

    // read the name
    string name;
    cin >> name;

    //build the message that we intend to write
    const string greeting = "Hello, " + name + "!";

    //the number of blanks surrounding the greeting
    const int pad = 1;

    //the number of rows and columns to write
    const int rows = pad * 2 + 3;
    const string::size_type cols = greeting.size() + pad * 2 + 2;

    //write a blank line to seperate the output from the input
    cout << endl;

    //write rows rows of output
    //invariant: we have written r rows so far
    for(int r = 0; r!= rows; ++r)
    {
        string::size_type c = 0;

        // invariant: we have c characters so far in the current row
        while (c != cols)
        {
            // is it time to write the greeting?
            if (r == pad + 1 && c == pad + 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;
}

as title say, these code somewhat messed up and i can't go along, especially at

string::size_type c = 0;

        // invariant: we have c characters so far in the current row
        while (c != cols)
        {
            // is it time to write the greeting?
            if (r == pad + 1 && c == pad + 1)
            {
                cout << greeting;
                c += greeting.size();
            }

            else
            {
                // are we on the border?
                if (r == 0 || r == rows - 1||
                    c == 0 || c == cols - 1)

let's go the problem one-by-one...

here :-

string::size_type c = 0;

why this and similar code declare at 0?

and here

c += greeting.size();

i know what this worked for, but, not understand how it solve it, what i mean is, if you remove this code from the program, u will get "Hello, [name]!" written infinitely by the program, at this point my brain can't get along very well, when this code in program it works fine...

by the way, this code is from Accelerated C++, Addison Wesley

You have to set c to 0, because it needs to increment until it counts the amount of characters in cols. When it reaches that amount, then you've finished printing one line.

As for c += greeting.size();, c is incremented by the amount of the "greeting" since in the previous statement, cout << greeting;, you've printed out greeting.size() characters.

thnx for reply ^_^

for first answer, hmm... thnx, got that really straight on...

for second answer, i got another question, to makesure my understanding is correct...
does the code c+= greeting.size() is for returning a value where the code below become wrong and the expression inside the function which is "cout << greeting" will not be executed again?,

if (r == pad + 1 && c == pad + 1)
{
cout << greeting;
c += greeting.size();
}

and because you already answer first question, so i think it's good to proceed with another problem?!

// are we on the border?
if (r == 0 || r == rows - 1||
c == 0 || c == cols - 1)

here, the "r ==0" and "c == 0" is understandable obviously, but, it is the other way around for both "r == rows - 1" and "c == cols - 1", I don't get it what "-1" for...

when I removed the "-1", the line of "*" at bottom either right as the frame of the greeting gone nowhere... pls explain..


again, ty very much for your answer...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.