Array problem

I just started learning C++ this week, and I've been experimenting with different concepts. I wrote the program below to see what happens. Eventually, I want to make a code-breaking game.

#include<iostream>
using namespace std;

int main ()
{
char code[4], guess[4];

cout << "Enter the code: ";
cin >> code;
cout << "Enter the guess: ";
cin >> guess;

cout << code[0];
cout << code[1];
cout << code[2];
cout << code[3] << endl;

cout << guess[0];
cout << guess[1];
cout << guess[2];
cout << guess[3];

return 0;
}

The problem is that the first digit of the code doesn't show up. For example, if I enter '1234' for both code and guess, it will output this:

234
1234

Why doesn't the '1' show up? Any help would be appreciated!
I have literally no idea why this worked, but when I switched the order that the user inputs the numbers (guess then code), it outputted "1234" for each one.

Edit: just switched the order back and it still works, even though it didn't before. Weird.
Last edited on
I just tried that and it worked for me as well. That's strange...
I have no idea why it wouldn't work before, and I REALLY have no idea why switching the order made it work... lol
code and guess both have storage for 4 chars. Using cin>>code and cin>>guess requires storage for 5 chars each (assuming you enter 4-char strings,) so you are trampling memory you don't own.

Welcome to the wild and wonderful world of undefined behavior.
Last edited on
I tried switching it back (code then guess) and it didn't work again. 'Guess then code' works fine though. Yes, it is very strange... Thanks for your help though!
As cire pointed out, doing cin >> into an array is dangerous. Moreover, because you're not preventing buffer overflow, it makes your program hackable in the worst possible way: a malicious user can trick it to execute arbitrary code.

Either (and very much preferred) use strings:

1
2
3
4
5
6
string code, guess;

cout << "Enter the code: ";
cin >> code;
cout << "Enter the guess: ";
cin >> guess;

Or at least limit the input to the size of your buffers:

1
2
3
4
5
6
7
char code[4], guess[4];

cout << "Enter the code: ";
cin >> setw(4) >> code;
cin.ignore(1000, '\n'); // in case you entered too much
cout << "Enter the guess: ";
cin >> setw(4) >> guess;

(although you may want to make them code[5] and setw(5), if you want to enter four-characters trings)
Last edited on
The result

234
1234


is simply explained.

Your declared variables

char code[4], guess[4];

are placed in the memory the following way

1
2
guess           code 
[_][_][_][_] [_][_][_][_]


When you entered code the memory become to look the following way

1
2
guess           code 
[_][_][_][_] [1][2][3][4] and additional byte for '\0' ['\0']


That there was memory overflow

When you entered guess. The memory become to look the following way

1
2
guess           code 
[1][2][3][4] ['\0'][2][3][4] and additional byte for '\0' [0]


The symbol '\0; is not displayable. So you got the result

234
1234



The problem is that you have to declare your arrays as

char code[5], guess[5];

because one byte is required for terminating zero.
Last edited on
Thank you for the explanation cire, Cubbi and vlad from moscow. It really helped! I think I'll use strings instead of arrays.
Last edited on
Topic archived. No new replies allowed.