views:

314

answers:

5

Well, not random, because its the same every time, but

#include<iostream>
using namespace std;
int main()
{
    char box[10][10];
    for(int i=-1;i<11;i++)
    {
        cout<<"---------------------"<<endl<<"|";
        for(int j=0;j<10;j++)
        {
            cout<<box[j][i]<<"|";
        }
        cout<<endl;
    }
    intx;cin>>x;
    return 0;
}

outputs a series of international characters (well, not all of them are 'international' per se, but I get things like pi and spanish inverted question mark). Anyways, I know this is becuase the program access chars that have not been initialized, but why do particular values create particular symbols, what are the ASCII values of the symbols (if they have ASCII values) and how can I get the symbols without glitching my program?

+3  A: 

Your loop over i doesn't make sense...

for(int i=-1;i<11;i++)

This will hit two indices that aren't valid, -1 and 10, when you reference box here:

cout<<box[j][i]<<"|";

It should be 0 to < 10 like the other loop.

Also you haven't initialized the contents of box to anything, so you're printing uninitialized memory. You have to put something into your "box" before you can take anything out.

The symbols themselves are probably extended ASCII, you can get at them through any extended ASCII table. This one came up first on google. For instance, you could do:

cout << "My extended ascii character is: " << (char)162 << endl;

to get a crazy international o.

Dan Olson
Yeah, the idea was to create a border, thus, I needed to display one more and one less than the limits of the array.
Keand64
As an extra note, you can determine the code page in use by the console with the CHCP command (on my Windows command line, this reports code page 850).To get the exact extension characters for that code page, you can look for a table specific to that code page (e.g. http://en.wikipedia.org/wiki/Code_page_850). There are Windows APIs to get/set the code page programmatically (GetConsoleOutputCP, SetConsoleOutputCP).
Dave Cluderay
A: 

for(int i=-1;i<11;i++)

That line is suspect. -1 to 10? Should be 0 to 9.

+2  A: 

For the same reason that

#include <iostream>
using namespace std;
int main()
{
   int x;
   cout << x;
}

displays a random value. Uninitialised variables (or arrays) contain garbage.

anon
A: 

The ASCII-code is

(int)(box[j][i])

You just print ordinary chars which are ASCII-characters with codes from 0 to 255. When printing wchar_ts the same memory is interpreted as other characters.

Your loop should be in [0; 10[ not in [-1; 11[

Dario
A: 

The symbols displayed by the program depend on:

  • the contents that happens to be in the uninitialised variables, and
  • the locale of your computer or the encoding used in presentation.

A Unicode reference would probably be the best source for identifying the codes required to display particular symbols.

Consider that your users may not have the same encoding (or fonts with the correct symbols) selected by default. You should either check the current locale, or force one specific to your output to ensure your output renders the way you wish.

Pekka