1

I am doing this

char *draw_line(int n, char ch)
{
    char *line = new char[50];
    for(int i = 0; i <= n; i++)
        line[i] = ch;
    return line;
}

and while calling the function I am writing this:

char *res_line = draw_line(50, '=');
cout<<*res_line;

but instead of getting = printed 50 times in the console window, it just show = sign one time. My main aim is to return the = or any character as many times I want and output the same to a text file. That's it.

9
  • 2
    your "string" is not null terminated. And use std::string Commented Aug 4, 2014 at 17:50
  • 1
    @quantdev thats not the problem in this case Commented Aug 4, 2014 at 17:52
  • If you think n and 50 are secretly related, you're right. once you fix the terminator, try cout << ch50. And plz consider just using std::string(50,ch). Use the standard lib. It's whats for dinner. Commented Aug 4, 2014 at 17:53
  • 1
    I imagine they suggest using std::string because it can be safer and more versatile (and because C++ standard); however, if you want to use char[] or char*, then more power to you in my opinion. It just means you may run into more problems to overcome. A learning experience, if you ask me. Commented Aug 4, 2014 at 17:56
  • 2
    @Serge One important reason to use std::string is that it won't leak memory. (Of course, if he can accept a maximum n, it's possible to write a thread safe version using a static variable, and avoid all dynamic allocation completely.) Commented Aug 4, 2014 at 17:59

3 Answers 3

5
cout<<*res_line;

is printing one char because *res_line is char, not char*.

Write:

cout<<res_line; 

But wait — that is not going to work either because res_line is NOT null-terminated.

Use std::string or std::vector<char>avoid explicit memory allocation, use RAII idiom instead:

std::string draw_line(int n, char ch)
{
    return {n, ch}; //C++11
}

So simple!

Or if you use std::vector:

std::vector<char> draw_line(int n, char ch)
{
    return {n, ch}; //C++11
}

which is almost same.

In C++03, however, you've to write:

return std::string(n, ch);        //in the first case
return std::vector<char>(n, ch);  //in the second case

That is, invoke the constructor explicitly.

Sign up to request clarification or add additional context in comments.

2 Comments

It's worth noting that the result of draw_line is not null terminated. (You should probably put that in your answer.)
+1 for std::vector. std::string is just as good because it is element-wise accessible in the same way vector is, plus it is made to be used with characters.
2

The valid code will look as

char* draw_line( int n, char ch )
{
    char *ch2= new char[n + 1]();

    std::memset( ch2, ch, n );

    return ch2;
}

//...

char *ch50 = draw_line(50,'=');
cout << ch50;
//...
delete []ch50;    

Take into account this statement

char *ch2= new char[n + 1]();

But it would be much better to write simply

std::cout << std::string( 50, '=' );

Comments

1
char* draw_line(int n, char ch)
{
    char *ch2= (char *) malloc(sizeof(char)*(n+1)); // (n+1) here
    for(int i=0;i<n;i++) // < instead of <=
        ch2[i]=ch;
    ch2[n] = 0; // terminator
    return ch2;
}

char *ch50 = draw_line(50,'=');
cout<< ch50; // changed from *ch50 to ch50

ADDON: look at string-fill constructor

cout << string(50, '=');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.