Printing Ascii symbols

May 19, 2010 at 4:30pm
I am trying to simply print an ascii code symbol in a visual studio c++ console app.

For example: when I run a program that contains:
cout<<"☺"<<endl;
It prints a question mark.

Is there a quick and easy solution to this?

Thanks.
Last edited on May 19, 2010 at 4:52pm
May 19, 2010 at 5:49pm
That's not an ascii symbol.
May 19, 2010 at 7:36pm
1
2
3
4
5
6
7
8
#include <iostream>
using namespace std;

int main()
  {
  cout << (char)1 << endl;
  return 0;
  }

It has nothing to do with C++ or your compiler. The problem is in your text editor. On Windows, trying to save a string containing the smiley character causes Notepad to complain. Ignoring the warning causes Notepad to substitute the '\?' character in the saved file.

Good luck!
May 19, 2010 at 8:30pm
@Duoas: If I may inquire, how in the 7 circles of hell did you know he was outputting to a text file? You seem to be right but in his post he said that he was outputting to a consule app which to me implies that he is not doing file I\O, and even his code shows no redeclaration of cout.
May 19, 2010 at 8:38pm
Ooh, look, a river of blood!

I think you misunderstood. He said there's a problem with the text editor being used, not that the OP was doing file I/O.

-Albatross
May 19, 2010 at 8:41pm
I wasn't trying to start a fight that was meant to be read with a sence of awe. I thought it was some kind of superhero deductive reasoning that Duoas was using.

EDIT: Also I couldn't even tell he was trying for an emoticon. I thought it was an @ symbol and my text format was just displaying it funny.
Last edited on May 19, 2010 at 8:43pm
May 20, 2010 at 12:08am
I had to RE-register under brian20 and I RE-posted before I saw your replies (sorry).

The ☺ is just an example of a symbol I typed in using ALT 1. I use alot of these symbols (such as ╔═ ) to create basic graphics for kids (I teach elementary school).

When I run a program with any of these symbols I get ? instead of the symbol I typed in. I do realize that I can use "char(1)" but that get very difficult with larger programs where you need to see what you've "drawn".

Any tips?

1
2
3
4
5
6
7
#include <iostream>
using namespace std;
int main()
{
cout<<☺<<endl;
return 0;
}

?
May 20, 2010 at 2:29am
Yes, I know it is obnoxious. There are only two solutions: use something like char(1) or named constants or the like, or use a text editor that is too stupid to mess with them.

Hmm, I can't find an appropriate editor. There is a third option... use your normal text editor, save as UTF-8 or something and then run your program through a preprocessor to convert it to the straight 8-bit codes. If you want, I'll write you up a program to do that. :-)
May 20, 2010 at 11:19am
Wow thanks. But don't mess with that unless its simple.
May 20, 2010 at 1:27pm
Here you go. :-)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// u2cp437.cpp
//
// Convert a UTF-8 Unicode text file to a Code Page 437 text file.
//
// Copyright (c) 2010 Michael Thomas Greer
// Distributed under the Boost Software License, Version 1.0.
// ( See file at http://www.boost.org/LICENSE_1_0.txt )
//
#include <iostream>
#include <string>
using namespace std;

#include <windows.h>

const char BOM[] = "\xEF\xBB\xBF";

//----------------------------------------------------------------------------
int main()
  {
  unsigned line_count = 0;
  string s;

  while (getline( cin, s ))
    {
    // Get rid of the stupid UTF-8 BOM
    if (!line_count)
      {
      if (s.find( BOM ) == 0)
        s.erase( 0, 3 );
      }
    line_count += 1;

    // The following stuff fails for empty lines, so we'll handle that here
    //
    if (!s.length())
      {
      cout << endl;
      continue;
      }

    // Convert UTF-8 to wchar_t[]
    //
    int count = MultiByteToWideChar(
      65001, // CP_UTF8
      0,
      s.c_str(),
      s.size(),
      NULL,
      0
      );
    if (!count)
      {
      switch (GetLastError())
        {
        case ERROR_INSUFFICIENT_BUFFER:    cerr << "line " << line_count << ": insufficient buffer\n";  break;
        case ERROR_INVALID_FLAGS:          cerr << "line " << line_count << ": invalid flags\n";        break;
        case ERROR_INVALID_PARAMETER:      cerr << "line " << line_count << ": invalid parameter\n";    break;
        case ERROR_NO_UNICODE_TRANSLATION: cerr << "line " << line_count << ": invalid unicode char\n"; break;
        default:                           cerr << "line " << line_count << ": fooey\n";
        }
      }

    wstring s_wide( count, '\0' );
    MultiByteToWideChar(
      65001, // CP_UTF8
      0,
      s.c_str(),
      s.size(),
      const_cast <wchar_t*> ( s_wide.c_str() ),
      count
      );

    // Convert wchar_t[] to CP 437
    //
    string s_cp437( count, '\0' );
    WideCharToMultiByte(
      437,
      0,
      s_wide.c_str(),
      count,
      const_cast <char*> ( s_cp437.c_str() ),
      count,
      NULL,
      NULL
      );

    // Display the converted line
    //
    cout << s_cp437 << endl;
    }

  // All done
  return 0;
  }

Save your text files as UTF-8.

Convert them to CP 437 at the command-line thus:
D:\prog\foo> u2cp437 < source.cpp > temp.cpp

Compile "temp.cpp" instead of "source.cpp" to create your executable.

Enjoy!
May 20, 2010 at 3:10pm
Super! Thanks a lot.
Topic archived. No new replies allowed.