Print string * int

So, i know this is probably another stupid question, but how would i print a string * an int.
Something like:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main() {
string example = "Hello World!";

int print = 4;

cout << example * print;

}

my desired output would be:
1
2
3
4
Hello World!
Hello World!
Hello World!
Hello World!
Last edited on
You need to use a for loop to print the string 4 times.
Thanks i figured it out.
By the way... Do you know about any graphics that go along with c++? I know that there is no graphics made just for it, but i read an article that said alot of graphics from programming laguages like C#, C, ext.. Go along with it. I just need to know one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

string operator * ( unsigned n, const string &str )
{
   string result;
   while (n--) result += str;
   return result;
}
   
int main()
{
   string example = "Hello World!";
   cout << 4 * ( example + '\n');
}


You can do it "out of the box" in python, but not in c++.
Well... Thanks anyways.
there are different types of graphics, and which one you choose depends on your OS as well as your need. What would you like to do? Its highly recommended that you learn the core language first, then move into using graphics, but you can ignore this and jump right on in if you want.
Some possibilities:


You gave me the hardest 3D library to add to visual studio ever! lol.
P.S: i think i might just suck at installing it :P
Setting up SDL 2 on Visual Studio 2019 Community:
https://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/msvc2019/index.php
Topic archived. No new replies allowed.