[Solved] values of array gets changed automatically after taking string input


The problem is that you input too many characters for cin >> ch;. Since m == 40, you declared it as char ch[40]. This allows you to enter 39 characters (not 40 because of the trailing '\0' character), but you entered much more than that, so it wrote outside the array boundary, which results in undefined behavior. In this case, it overflowed into the arr array.

I suggest you use std::string rather than a char array, then it will automatically expand to the proper size.

10

solved values of array gets changed automatically after taking string input