#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
int main()
{
std::cout << "Enter a number: ";
int m { };
std::cin >> m;
std::cout << '\n';
std::string s { std::to_string(m) };
std::cout << s << "\n\n";
int nummax { INT_MIN };
for ( int j { }; j < s.size(); j++ )
{
std::string temp { s };
if ( temp[ j ] == '5' )
{
temp.erase(j, 1);
nummax = std::max(nummax, std::atoi(temp.c_str()));
}
std::cout << temp << '\n';
}
std::cout << nummax << '\n';
}
Enter a number: 1234
1234
1234
1234
1234
1234
-2147483648
That last line of output looks rather suspiciously wrong, along with most of the other lines of output.
You are missing a number of headers for the functionality you use. Not every compiler implementation implicitly includes other headers in commonly used headers such as <iostream>. <string> is one that MSVC++ needs to be explicitly included if you want to use C++ strings. No include and C++ string code will not compile.
<algorithm> for std::max, <cstdlib> for std::atoi are also recommended.
#include <iostream>
#include <algorithm>
#include <string>
#include <limits>
int main() {
int m;
std::cout << "Enter a number containing at least one digit 5: ";
std::cin >> m;
constauto s { std::to_string(m) };
auto nummax { std::numeric_limits<int>::min()};
for (size_t j {}; j < s.size(); ++j)
if (s[j] == '5')
nummax = std::max(nummax, std::stoi(s.substr(0, j) + s.substr(j + 1)));
std::cout << nummax << '\n';
}
Enter a number containing at least one digit 5: 15958
1958