Print string * int

Feb 10, 2021 at 7:13pm
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 Feb 10, 2021 at 7:14pm
Feb 10, 2021 at 7:14pm
You need to use a for loop to print the string 4 times.
Feb 10, 2021 at 7:35pm
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.
Feb 10, 2021 at 7:37pm
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++.
Feb 10, 2021 at 7:38pm
Well... Thanks anyways.
Feb 10, 2021 at 9:59pm
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.
Feb 10, 2021 at 10:42pm
Some possibilities:


Feb 12, 2021 at 12:05am
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
Feb 12, 2021 at 12:18am
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.