hello guys,

sorry if it's wrong topic name

but in accelerated c++ book

and in chapter 2 exactly

i have a problem

#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 separate 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 written 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;
}

this is the code of the example i understand every thing in the example of c++ language

but i can't never understand what is this program and how to figure out the columns and the rows

is that's ok or i can't programming ???

i can do any thing with what i learnt but i can't really figure out how to do this example

is it ok ????

Here is what it looks like when I run the code:

Please enter your first name:  Blake

*********************
*                   *
*   Hello, Blake!   *
*                   *
*********************

What are you having trouble with?

i have a trouble with understanding how to do columns and rows

i can't figure my self how to make the rows and columns

Ok so lets break it down a little bit:

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

This little number here is the "pad"ding

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

This is where you're calculating the row and column sizes

The first line --> rows = pad * 2 + 3, comes out to 5 rows.
The second line --> cols = greeting.size() + pad * 2 + 2 takes the size of the name you enter along with the greeting text, and adds to it 2 times the padding value plus 2 more. In my case it uses the size of my name along with the greeting "Hello, " + name + "!" ---> 13 chars plus 2 times the padding + 2 ---> 13 + 4 = 17

thanks for giving me this :D
solved

Sometimes the best way to look at a problem is to reiterate it. So here goes. Feel free to ask questions about this comment.

The objective of the code is to write "Hello, NAME" were NAME is replaces with whatever you say your name is. However, it is to have in in a block of stars. (*).

e.g.

******************************
*                            *
* Hello, VeryLongNamedPerson *
*                            *
******************************

The issue with rows are the number of "*<blanks>*" lines to add. e.g You might like 6 above and below. HOWEVER, the code as written does NOT do that. The line rows = pad*2 does not need the pad*2. Then later in the code the placement of the greetings text is not centred.

cols on the other hand is a calculated variable, ie it depends on the length of your name, However, it also depends on a const variable pad, i.e how many spaces from the edges of the *s to your greeting.

This examples to my mind is poorly set out . I would have much prefered to see code start as this

int main()
{
   const int pad(3);           // spaces to the side of your greeting
   const int rows(5);          // Total rows displaced including greeting and 
                               // ***** at the top / bottom.

   // Calculated values here:
   //.....

i.e this is a very clear definition of what is a parameter and what is calculated -- it comes back to the principle code should convey intent as well as function. If you have two different pieces of code that do the same thing, normally it is better pick the one that is more obvious what it is doing [unless there is a big performance difference between them].

However, that is what you are going to have to deal with this book. I think this is just a very poor example. If you understand roughly how it works, just move on, the educational value of this is minimal.

look guys

i save the code in my head and i can write it

but i got the problem with the looping i don't know when i use ++c or the rows = 0 or all of this things

i don't know how to do this frame

i can do any thing with looping but i can't do some thing like this

is this ok or i'm in bad position ????

Forget about it. If you get the looping part then move on. This example is a bit confusing for a beginner to be honest.

thanks.........

you give me a push

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.