printing multiple of a character just by defining it once

Hello,
I am learning C++ and I have a question which if I want to search it by my own it will last more time than I have.
first: I want to define a character i.e: const char deg = !; and use it in different part of my program just by multiplying a scalor by it.
I mean somwhere in my program I be able to write 4*deg and by a print out command I can have four consecutive of !!!!.
I do not know in which topic I have to search for such arraying in this way or printing by for instance printf with some special features for doing my order.

second:
we cannot define an array which the size of it will be dynamic by getting an input from user. what is the replace for it???
we cannot define an array which the size of it will be dynamic by getting an input from user. what is the replace for it???


Vectors.

The first part, whats wrong with using a basic loop?
Well for your first question, in order to do the first operation where you can do 4*deg and get an multiple of a character you'd need to write a class and overload the operators to work in the way you desire. There is no simple C++ way to do that otherwise. You could settle for having a loop or a function that prints it out a number of times when you need it as well but it will not have the same syntax as 4*deg.

You could write a function like this:
1
2
3
4
5
void deg(int size)
{
    for (int i = 0; i < size; ++i)
        std::cout << '!'; // writes ! to the output 'size' times
}


and is called like so:
deg(4);

Or use the std::string constructor to the same effect:
1
2
3
std::string(4, '!'); // makes a string of 4 '!'s which can be printed with cout

std::cout << std::string(4, '!');


As for your second question, the dynamic array in C++ is a std::vector.
It requires its own header to be used and a defined type when it is initialized.
1
2
3
#include <vector>

std::vector<int> vc;

More here: http://www.cplusplus.com/reference/stl/vector/
Topic archived. No new replies allowed.