integer hang forever

I am trying to solve the below problem, but my code hangs for ever and doesn't generate any output. Not sure what went wrong here?


https://www.chegg.com/homework-help/questions-and-answers/write-function-solution-given-integer-n-returns-maximum-possible-value-obtainable-deleting-q99034950

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>

using namespace std;

int main(){
    
    int m;
    cin >> m;
    string s = to_string(m);
    cout<<s;
    int nummax = INT_MIN;
    for(int j = 0; j < s.size(); j++){
    
        string temp = s;
        if(temp[j] == '5'){
    
            temp.erase(j,1);
            nummax = max(nummax,atoi(temp.c_str())); 
        }
        cout<<temp<<endl;
    }
    cout<<nummax;
}
Did you type a number and press enter?
It never hurts to include an input prompt so "the user" knows what is requested.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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.
George P wrote:
That last line of output looks rather suspiciously wrong, ...

The problem descriptions says you can assume there is at least one '5' digit.

George P wrote:
... along with most of the other lines of output.

The other lines looks like debug prints to me.
I tried with at least one five in the input and still the last line of output was 'suspiciously wrong.'

Enter a number: 12345678955

2147483647

2147483647
2147483647
2147483647
2147483647
2147483647
2147483647
2147483647
2147483647
2147483647
2147483647
-2147483648


chegg.com links refuse to load for me.

I entered input values as 12345 on the console and got the below output, which is not expected.

OUTPUT:

12345
1234512345
12345
12345
12345
1234
1234


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main(){
    
    int m;
    cin >> m;
    string s = to_string(m);
    cout<<s;
    int nummax = INT_MIN;
    for(int j = 0; j < s.size(); j++){
    
        string temp = s;
        if(temp[j] == '5'){
    
            temp.erase(j,1);
            nummax = max(nummax,atoi(temp.c_str())); 
        }
        cout<<temp<<endl;
    }
    cout<<nummax;
}
Last edited on
I entered input values on the console and got the below output, which is not expected.

So....what output are you expecting? You need to tell us here, please. chegg.com is useless for me, it throws up an "Access Denied" nag.
As per chegg problem statement, for 15958 input, I got below output. Looks like expected output now?

15958
1595815958
1958
15958
1598
15958
1958
Issue is solved, have commented other cout, so getting expected values. Earlier I was confused with multiple output values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cstdlib>
#include <algorithm>

using namespace std;

int main(){
    
    int m;
    cin >> m;
    string s = to_string(m);
    //cout<<s;
    int nummax = INT_MIN;
    for(int j = 0; j < s.size(); j++){
    
        string temp = s;
        if(temp[j] == '5'){
    
            temp.erase(j,1);
            nummax = max(nummax,atoi(temp.c_str())); 
        }
        //cout<<temp<<endl;
    }
    cout<<nummax;
}
George P wrote:
I tried with at least one five in the input and still the last line of output was 'suspiciously wrong.'

It's because you're exceeding the range of an int.

The problem descriptions says you can assume the input number is no larger than 999995.

@leo2008 Yeah, your program seems to have been correct all along.
Last edited on
Now do it without using strings.
If you're going to do it this way, then have you come across std::stoi() instead of using atoi() ?

https://cplusplus.com/reference/string/stoi/

 
nummax = max(nummax, stoi(temp));


Also, this code can be simplified:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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;

	const auto 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

Last edited on
Hi seeplus

I was not aware about std::stoi(), until you told me. Anyhow thanks for your suggestion.
Topic archived. No new replies allowed.