0

I wrote a program that should output the maximum int from user input:

15 20 0 5 -1

should output Max: 20

There seems to be an infinite loop because it never gets to the final cout statement. The while loop should stop with a negative number. It's not outputting anything right now.

Here is my code:

#include <iostream>

using namespace std;

int main()
{

    int currV;
    int maxSoFar;

    // 15 20 0 5 -1

    // should output 20

    cin >> currV;
    if (currV > 0)
    {
        maxSoFar = currV;
    }

    while (currV > 0)
    {

        if (currV > maxSoFar)
        {
            maxSoFar = currV;
        }
        cin >> currV;
    }
    cout << "Max: " << maxSoFar;
}
silverfox
  • 1,568
  • 10
  • 27
sdev
  • 85
  • 6
  • It should end the while loop when it hits -1 in the user input – sdev Jun 08 '21 at 22:05
  • 1
    Well, then you should change the condition of your while loop, because as it is it'll also exit when the user enters `0`. Right now I have the opposite problem you describe: Instead of the code looping forever, it exits prematurely on your input. – Nathan Pierson Jun 08 '21 at 22:05
  • [`std::cout << "Max: " << *std::max_element(std::istream_iterator(std::cin),{});](http://coliru.stacked-crooked.com/a/64ede3084d5f5e4b) – Mooing Duck Jun 08 '21 at 22:10
  • 2
    @MooingDuck "*The while loop should stop with a negative number*" - using `istream_iterator` like you show will not end on a negative number, only on EOF – Remy Lebeau Jun 08 '21 at 22:20

4 Answers4

4

Firstly, I recommend formatting your code better.

Furthermore, your code looks quite confusing and I recommend something like this for what you are trying to do:

#include <iostream>
using namespace std;

int main() {

    int currV = 0;
    int maxSoFar = 0;

    while (cin >> currV)
    {
        if(currV < 0)
        {
            cout << "Max: " << maxSoFar;
            return 0;
        }

        if(currV > maxSoFar)
        {
            maxSoFar = currV;
        }
    }
}
HARRIBO
  • 165
  • 10
  • 1
    You could do `cin >> currV; maxSoFar = currV;` before the `while` loop. Thus if on one value is entered, it will be the maximum. – Thomas Matthews Jun 08 '21 at 22:17
  • @ThomasMatthews: It already works that way, no need to special-case the first item and duplicate code. – Ben Voigt Jun 08 '21 at 22:33
1

You need to initialize maxSoFar. Reading/using an uninitialized variable will cause undefined behaviour, then afterward anything could happened. This could be solve by initializing maxSoFar = -1; beforehand.

You can use max() to get maximum value of 2 integers as well.

#include <iostream>
using namespace std;

int main()
{
    int currV, maxSoFar = -1; //initializing maxSoFar

    while(cin >> currV)
    {
        if (currV < 0) {break;}
        maxSoFar = max(maxSoFar, currV);
    }
    cout << "Max : " << maxSoFar; return 0;
}

Result:

5 20 0 5 -1
Max : 20

Also, see Why is "using namespace std;" considered bad practice?

Related : What happens to a declared, uninitialized variable in C? Does it have a value?

silverfox
  • 1,568
  • 10
  • 27
0
#include <iostream>

using namespace std;

int main()
{
    int currV, maxSoFar = -1;

    if (cin >> currV && currV >= 0)
    {
        maxSoFar = currV;
        while (cin >> currV && currV >= 0)
        {
            if (currV > maxSoFar)
            {
                maxSoFar = currV;
            }
        }
    }

    cout << "Max: " << maxSoFar;
}

Online Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0
int currV;
int maxSoFar = 0; // if all integers are negetive than `maxSoFar` should be 0
 
while (cin >> currV)
{
    if (currV < 0) break;
    maxSoFar = max(maxSoFar, currV);
}
cout << "Max: " << maxSoFar;

see this - https://isocpp.org/wiki/faq/input-output#stream-input-failure