Using cin for char array
Solution 1
I see that you enter (type) 1234567890
characters to input data for inp[5]
- it is a problem because imp
array is able to store 4 characters and null-terminator. When cin >> inp
store more than 4 characters to inp
array it leads to problem with data (somthing like undefined behaviour). So solution can be in allocation more memory for data, e.g.:
#include <iostream>
using namespace std;
int main(){
char inp[15], out[15]; // more memory
cin >> inp >> out;
cout << inp << endl;
cout << out << endl;
system("pause");
return 0;
}
Solution 2
When you read into a character array the stream keeps reading until it encounters whitespace, the stream is not aware of the size of the array that you pass in so happily writes past the end of the array so if your first string is longer than 4 characters your program will have undefined behaviour (an extra character is used after your input for the null terminator).
Fortunately c++20 has fixed this issue and the stream operators no longer accept raw char pointers and only accept arrays and will only read up to size - 1
characters.
Even with c++20 the better solution is to change your types to std::string
which will accept any number of characters end even tell you how many characters it contains:
#include <iostream>
int main(){
std::string inp, out;
std::cin >> inp >> out;
std::cout << inp << "\n";
std::cout << out << "\n";
return 0;
}
Solution 3
Its because, in memory layout of computer out[4] is laid out first and then inp[5]. Something like this: out[0],out[1],out[2],out[3],inp[0],inp[1],inp[2],inp[3],inp[4]
So, when you write 6789 in out[4], you are overflowing null character to inp[0]. So, inp becomes NULL.
ken
Updated on July 09, 2022Comments
-
ken almost 2 years
Here is my code:
#include <iostream> using namespace std; int main(){ char inp[5], out[4]; cin >> inp >> out; cout << inp << endl; cout << out << endl; system("pause"); return 0; }
when I type:
12345 6789
It gives me:
6789
Why I failed to save the 5 words char array 'inp' and it showed nothing? The second input looks normal though. However, when I set out[3] or out[5], it seems to work alright? It seem that two char array of [5] then followed by [4] would cause problem...
-
Andrew Prock about 9 yearsYou're first writing 11 characters (including the trailing
'\0'
) to a 5-character array. Luckily nothing blew up at that point. Then you write another 11 chars to a 4-character array. After running out of room inout
, the data spills out intoinp
. Fix your array sizes, or limit the length of your input.
-
-
ken about 9 yearsI just discovered something strange. The problem only happens when I set the first array with size = 5 and the second one with size = 4. When I set the array size of the second one as 5 or even 3, the problem vanished. I edited my question with input 12345 and 6789 instead of the long numbers
-
VolAnd about 9 yearsFor correct storage of 5 characters array must have at least 6
char
items, becausecin >>
adds'\0'
after characters typed by user (as well asscanf
function) -
prehistoricpenguin over 2 yearsIt depends on the endian.
-
Alexander over 2 yearsHi! This is a very old question that, as pointed out by some comments and answers, has quite a few issues in its code. If you're interested in answering some newer questions about C++, check out this list stackoverflow.com/questions/tagged/c%2b%2b