2

I am porting a program from Perl to C++ as a learning objective. I arrived at a routine that draws a table with commands like the following:

Perl: print "\x{2501}" x 12;

And it draws 12 times a '━' ("box drawings heavy horizontal").

Now I figured out part of the problem already:

Perl: \x{}, \x00        Hexadecimal escape sequence;
C++:  \unnnn

To print a single Unicode character:

C++:  printf( "\u250f\n" );

But does C++ have a smart equivalent for the 'x' operator or would it come down to a for loop?


UPDATE Let me include the full source code I am trying to compile with the proposed solution. The compiler does throw an errors:

g++ -Wall -Werror project.cpp -o project
project.cpp: In function ‘int main(int, char**)’:
project.cpp:38:3: error: ‘string’ is not a member of ‘std’
project.cpp:38:15: error: expected ‘;’ before ‘s’
project.cpp:39:3: error: ‘cout’ is not a member of ‘std’
project.cpp:39:16: error: ‘s’ was not declared in this scope


#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <iostream>

int main ( int argc, char *argv[] )
{
        if ( argc != 2 )   
        {
                fprintf( stderr , "usage: %s matrix\n", argv[0] );
                exit( 2 );
        } else {
                //std::string s(12, "\u250f" );
                std::string s(12, "u" );
                std::cout << s;
        }       
}
jippie
  • 937
  • 5
  • 15
  • 33
  • Related: [Multiply char by integer (c++)](http://stackoverflow.com/q/2596953), [In C++, I thought you could do “string times 2” = stringstring?](http://stackoverflow.com/q/5145792), [Multiplying a string by an int in c++](http://stackoverflow.com/q/11843226) – gx_ Nov 09 '13 at 17:59
  • C uses `printf`. In C++, we prefer `std::cout << `. – Lightness Races in Orbit Nov 09 '13 at 18:00

1 Answers1

3

No, C++ doesn't have the "x" operator, however you can create a string with character repetition:

std::string s(12, <any character>);

Then you can print it like below (the "printf" is inherited from C, it might be not good to use it in your assignment):

std::cout << s;

(Of course, you can use any number, not just 12)

UPDATE for your update above:

I can compile your code with only minor change ("u" replaced by 'u' because it must be a character). My compiler is GCC 4.6.2 for Windows XP (MINGW32 version). What OS/compiler do you use?

HEKTO
  • 3,876
  • 2
  • 24
  • 45