在C ++中,我认为你可以做“字符串时间2” = stringstring? [英] In C++, I thought you could do "string times 2" = stringstring?
问题描述
我想弄清楚如何打印字符串几次。我收到错误。我只是试了一下:
I'm trying to figure out how to print a string several times. I'm getting errors. I just tried the line:
cout<<"This is a string. "*2;
我希望输出:这是一个字符串,这是一个字符串,但我didn不知道。这行有什么问题吗?如果没有,这里是整个程序:
I expected the output: "This is a string. This is a string.", but I didn't get that. Is there anything wrong with this line? If not, here's the entire program:
#include <iostream>
using namespace std;
int main()
{
cout<<"This is a string. "*2;
cin.get();
return 0;
}
我的编译器不打开,因为我在做病毒扫描,所以我可以不给出错误消息。但是鉴于这个网站的代码相对简单,我希望有人知道我是否通过简单的查找做错了。
My compiler isn't open because I am doing virus scans, so I can't give the error message. But given the relative simplicity of this code for this website, I'm hoping someone will know if I am doing anything wrong by simply looking.
感谢您的反馈。
推荐答案
理想情况下,您可以使用循环 / strong>。检查 / while / do-while 方法。
Well, ideally, you would use a loop to do that in C++. Check for/while/do-while methods.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int count;
for (count = 0; count < 5; count++)
{
//repeating this 5 times
cout << "This is a string. ";
}
return 0;
}
输出:
This is a string. This is a string. This is a string. This is a string. This is a string.
这篇关于在C ++中,我认为你可以做“字符串时间2” = stringstring?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!