+1

[help] i have set the size of character array 2 byte then how can it store more than 2 byte???

i am using visual studio 2017 code : #include<iostream> using namespace std; int main() { char a[2]; cout << "enter the name" << endl; cin>>a; cout << "the name is " << a << endl; cout << endl; system("pause"); return 0; } output: enter the name class the name is class

c++

11/6/2019 7:52:50 AM

Sami

2 Answers

New Answer

+4

C++ don't check for array bounds. You can write in memory which is beyond limit of that array but you should not do that. It can cause loss of some important data by overwriting memory. Better use a std::string for this purpose.

+1

Allocate more bytes than the name might use. Use try and catch to deal with exceptions.