characters after console entry

Hi,

I'm trying to find some footing with incput from the console, and have a question about the "remaining characters" from an input string. Say I buffer an array to take 16 characters, and the user inputs 10. The remaining 6 are filled with what seems to be gibberish... can someone explain where this comes from?

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
// definitions
int main()
{

  char s[15];

  cout << "Enter some characters... ";
  cin.get(s, 15);
  cout << endl << s << endl;

  //PrintChars(s);
  DocumentChars(s);

  return 0;
}

void DocumentChars(char s[15])
{
  int iAscVal;
  for (int i=0; i<16; i++)
  {
    iAscVal = int(s[i]);
    cout << "[" << i << "]ASCII: " << iAscVal
         << "\tCharacter: " << s[i] << endl;
  }
}


Enter some characters... asd fgh jkl

asd fgh jkl
[0]ASCII: 97    Character: a
[1]ASCII: 115   Character: s
[2]ASCII: 100   Character: d
[3]ASCII: 32    Character:
[4]ASCII: 102   Character: f
[5]ASCII: 103   Character: g
[6]ASCII: 104   Character: h
[7]ASCII: 32    Character:
[8]ASCII: 106   Character: j
[9]ASCII: 107   Character: k
[10]ASCII: 108  Character: l
[11]ASCII: 0    Character:
[12]ASCII: -96  Character: á
[13]ASCII: -3   Character: ²
[14]ASCII: 127  Character: ⌂
[15]ASCII: 0    Character:

Process returned 0 (0x0)   execution time : 5.772 s
Press any key to continue.


Output line 11 shows the null-terminator, but where to 12-15 come from?

Many thanks,
Jack
They are uninitialized characters, they contain whatever was present in the memory where your array is located
Ahh, that's it. Thank you Sir
Topic archived. No new replies allowed.