Printing text x times in one line?

Is there any way to do the following without an error?

1
2
3
4
5
6
7
8
#include <iostream>

using namespace std;

int main(){
   cout << "This will be printed 3 times!" * 3;
   return 0;
}
1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main()
{
   for ( int i = 1; i <= 3; i++ ) cout << "This will be printed 3 times!\n";
}



I quite like this one, too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

string operator *( unsigned int n, string s )
{
   string result;
   while ( n-- ) result += s;
   return result;
}

int main()
{
   cout << 3 * string( "This will be printed repeatedly!\n" );
}

or
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;

string operator *( unsigned int n, string s ) { return n ? ( n - 1 ) * s + s : ""; }

int main()
{
   cout << 3 * string( "This will be printed repeatedly!\n" );
}




I guess there's also
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;

#define TIMES( n ) for ( int i = 1; i <= ( n ); i++ )

int main()
{
   TIMES( 3 ) cout << "This will be printed repeatedly!\n";
}




If you want all output on one line just remove the \n from the string.
Last edited on
Woah! Thanks didn't expect this^^
Topic archived. No new replies allowed.