COUT 为简单的 C++ 代码给出奇怪的数字
COUT gives strange number for simple C++ code
我正在尝试使用非常简单的 C++ 代码在 hello world 周围添加一个 ASCII 星号 (*) 框架。
它有效,但在框架之前给出了这个奇怪的数字。这是我的示例代码。
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Please enter your name" << endl;
string name;
cin >> name;
string greeting = "Hello, " + name + "!";
string spaces(greeting.size(), ' ');
string stars(greeting.size(), '*');
cout << '**' << stars <<'**' << endl;
cout << '* ' << spaces <<' *' << endl;
cout << '* ' << greeting << ' *' << endl;
cout << '**' << stars <<'**' << endl;
cout << '* ' << spaces <<' *' << endl;
return 0;
}
输出
Please enter your name
tester
10794**************10794
10784 8234
10784Hello, tester!8234
10794**************10794
10784 8234
Press any key to continue . . .
为什么要在字符串前后加上这些数字?请帮忙。谢谢
试试双引号,看起来像这样:
cout << "**" << stars <<"**" << endl;
:-)
我正在尝试使用非常简单的 C++ 代码在 hello world 周围添加一个 ASCII 星号 (*) 框架。 它有效,但在框架之前给出了这个奇怪的数字。这是我的示例代码。
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Please enter your name" << endl;
string name;
cin >> name;
string greeting = "Hello, " + name + "!";
string spaces(greeting.size(), ' ');
string stars(greeting.size(), '*');
cout << '**' << stars <<'**' << endl;
cout << '* ' << spaces <<' *' << endl;
cout << '* ' << greeting << ' *' << endl;
cout << '**' << stars <<'**' << endl;
cout << '* ' << spaces <<' *' << endl;
return 0;
}
输出
Please enter your name
tester
10794**************10794
10784 8234
10784Hello, tester!8234
10794**************10794
10784 8234
Press any key to continue . . .
为什么要在字符串前后加上这些数字?请帮忙。谢谢
试试双引号,看起来像这样:
cout << "**" << stars <<"**" << endl;
:-)