I just purchased accelerated C++ and I'm working through the examples and I'm at a road block trying to figure out what this line means. I was wondering if someone could explain this to me in a different way? The part of the code I do not understand are bolded.

I was also wondering if there is a way to make the font size of the codes bigger inside of Code Blocks?

#include <iostream>
#include <string>

int main()
{
    std::cout<<"Please enter first name:";
    std::string name;
    std::cin>>name;


    const std::string greeting = "Hello,"+name+"!";

    const [B]std::string spaces(greeting.size(),'*');[/B]
    const [B]std::string second ="*"+spaces+"*";[/B]

    const std::string first(second.size(),'*');
    
    std::cout<<std::endl;
    std::cout<<first<<std::endl;
    std::cout<<second<<std::endl;

    std::cout<<"*"<<greeting<<"*"<<std::endl;


    return 0;
}
const std::string spaces(greeting.size(),'*');
From basic_string(3C++)
     basic_string (size_type n, charT c,
                  const Allocator& a = Allocator());

        Constructs a string containing  n  repetitions  of  c.  A
        length_error  exception  is  thrown  if  n  ==  npos. The
        effects of this constructor are:

        data()   points to the  first  element  of  an  allocated
                 array  of  n  elements, each storing the initial
                 value c

        size()   n

        capacity()   a value at least as large as size()

second one is concating strings..

Hey, thanks a lot but I was wondering if you could explain it in layman's term? Okay, maybe I'll try and explain it to what I think "spaces" means.

const std::string spaces(greeting.size(),'*');

Spaces is string that is constant throughout the program that reads the string greeting and it places a * anywhere it can on the screen?

>but I was wondering if you could explain it in layman's term?
If greeting.size() is 5, spaces will be "*****". If greeting.size() is 3, spaces will be "***". The number of asterisks matches the number of characters in greeting .

Ahh.. Okay I see. I was getting confused on what the book is saying. Let me show you to make sure if I got it right.


To complete our understanding of spaces, we need to know that when we construct a string
from an integer value and a char value, the result has as many copies of the char value as the
value of the integer. So, for example, if we were to define
std::string stars(10, '*');
then stars.size() would be 10, and stars itself would contain **********.
Thus, spaces contains the same number of characters as greeting, but all of those characters
are blanks.

The command std::string stars(10,'*); is a completely different command from stars.size()? The part that confused me was the part where I thought you could type stars.size(10). stars.size() is a command in it's on right that needs to be defined before it would do anything. Do I have this right?

>The command std::string stars(10,'*); is a completely different command from stars.size()?
Yes, those are two different "commands". If you do std::string stars(10,'*); , then stars.size() will return 10.

>stars.size() is a command in it's on right that needs to be defined before it would do anything. stars is an object of the std::string type. std::string already defines the size member function for you. But yes, a function needs to be defined before it'll do anything.

Remember a string is just bunch of characters. Thus

std::string stars(10,'*);

says the stars consists of 10 characters, where those
10 characters are all '*'; string.size() returns the number of characters
the string contains.

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.