Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STRING OF REPEATING CHARACTERS

Status
Not open for further replies.

ARCITS

Programmer
Apr 4, 2002
99
GB
In VB you can write:
string("=",20) to generate the string "===================="
Is there an equivalent in C++?
 
#include<iostream.h>
void String( char *caracter, int number )
{
for( int i = 0; i < number; i++ )
cout<<caracter;
}
void main()
{
String( &quot;=&quot; , 20 );
}
 
Or maybe this will do:

#include<iostream.h>
#define _MAXSTRING_ 100
char* string;
void String( char character, int number )
{
string = new char[_MAXSTRING_];
for( int i = 0; i < number; i++ )
string = character;
string = '\0';
}
void main()
{
String( '=' , 20 );
cout<<string;
delete string;
}
 
there is a much easier way

first you need to remember to have it have an extra character for the null terminator and just do

char buffer[21];
memset(buffer,'=',20);
buffer[20]=0;

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top