Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

COUT gives strange number for simple C++ code

I am trying to add a ASCII asterisk (*) frame around the hello world in a very simple C++ code.
It works but gives this strange number before the frame. Here is my sample code.

#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;

}

Output

Please enter your name
tester
10794**************10794
10784              8234
10784Hello, tester!8234
10794**************10794
10784              8234
Press any key to continue . . .

Why does it add these numbers before and after the string? Please help. Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try double quote, so it looks like this:

cout << "**" << stars    <<"**" << endl;

🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading