1

I need to create a program that will allow the users to enter a series of integers with 0 as the sentinel. At the end, the largest integer will be displayed.

I originally had the statement largest = value under the if statement but changed it to a conditional statement which makes more sense to me since the previous way I had it just gave me the last value before the sentinel was entered. It seems to give me the "right" output, but is the way I set it up with the conditional statement correct? If not, how can I fix it?

Original output

This program finds the largest integer in a list.
Enter 0 to signal the end of the input.

Please enter an integer: 21

Please enter an integer: 15

Please enter an integer: 9

Please enter an integer: 6

Please enter an integer: 3

Please enter an integer: 0

The largest value is 3.

Press any key to continue . . .

Current output

This program finds the largest integer in a list.
Enter 0 to signal the end of the input.

Please enter an integer: 15

Please enter an integer: 200

Please enter an integer: 3

Please enter an integer: 5

Please enter an integer: 21

Please enter an integer: 0

The largest value is 200.

Press any key to continue . . .

Source code:

#include "stdafx.h"
#include <iostream> 
using namespace std;

const int SENTINEL = 0;

/* Main Program */
int main()
{
    
    cout << "This program finds the largest integer in a list.";
    cout << "\nEnter " << SENTINEL << " to signal the end of the input." << endl;

    int largest = SENTINEL;

    while (true)
    {
        int value; 
        cout << "\nPlease enter an integer: ";  
        cin >> value;

        if (value == SENTINEL) 
            break;
        largest = (value > largest) ? value : largest; 
    }

    cout << "\nThe largest value is " << largest << "." << endl;

    cout << endl;

    system("PAUSE");

    return 0;
}
Community
  • 1
  • 1
Mary
  • 211
  • 1
  • 8
  • 18
  • 2
    add in `if ( !cin ) break;` after `cin >> value` otherwise you will go into an inifinite loop if they type something strange. You might also want to start with `int largest = INT_MIN;` so that your program copes with negative numbers, and also try to handle the case where the first value they enter is the sentinel. – M.M Aug 18 '15 at 04:46
  • I followed your first suggestion and ran the program and tested out what would happen if I entered a symbol like !. The last sentence just said The largest value is 0. – Mary Aug 18 '15 at 04:55
  • try entering a non-zero number , press enter, and then do `!` next time it says "Please enter an integer". – M.M Aug 18 '15 at 05:11
  • If I enter 8, then !, at the end, it will say The largest value is 0. – Mary Aug 18 '15 at 05:20
  • If the first value entered is 0, the program will stop prompting and display that the largest value is 0 which makes sense since 0 is the sentinel value and is meant to stop the loop which prompts for more values. – Mary Aug 18 '15 at 05:22
  • OK, your compiler is being friendly and treating `int value` as `int value = 0` .Lucky or unlucky depending on how you want to look at it :) When you enter `!`, then `cin >> value` does not change `value`. – M.M Aug 18 '15 at 06:25
  • @MattMcNabb: `8` is read into `value`, then the `!` fails parsing which from C++11 is required to reset `value` to `0` (unspecified behaviour in C++03), coincidentally matching the `SENTINEL` and terminating the loop. The [output I see](http://coliru.stacked-crooked.com/a/2fab8ed830dfb06b) is `8` - consistent with that - not the `0` that Mary reports above, and which I can't explain. – Tony Delroy Aug 18 '15 at 08:06
  • @TonyD if your description is correct, then `8` is read successfully the first time, and the second time `0` is read – M.M Aug 18 '15 at 09:47
  • @MattMcNabb: depends on what you consider "read" - `value` is set but not to anything read from the stream - the stream is left in an error state. My points were just that your earlier "treating `int value` as `int value = 0`" was strange speculation because `value`'s overwritten with `8` and any prior value is irrelevant, and that "enter `!`, then `cin >> value` does not change `value`" is not true of C++11. – Tony Delroy Aug 19 '15 at 03:39
  • @TonyD Yep, got that – M.M Aug 19 '15 at 04:49

2 Answers2

2

Your approach is correct in my opinion.

However, you can simply replace:

largest = (value > largest) ? value : largest; 

by an equivalent if conditional statement:

if (value > largest){
    largest = value;
}

Also, note that just replace your declaration:

int largest = SENTINEL;

by

int largest =  INT_MIN;

where INT_MIN is minimum possible integer.

TryinHard
  • 4,078
  • 3
  • 28
  • 54
2

Just a few minor suggestions in inline comments, for whatever educational value you can extract from them...

#include <iostream> 

const int SENTINEL = 0;

int main()
{
    // can continue input across lines, and rarely need to use endl...
    // (any input attempt from cin will flush cout first)
    std::cout << "This program finds the largest integer in a list.\n"
        "Enter " << SENTINEL << " to signal the end of the input.\n";

    int num_inputs = 0;
    int largest;
        // before, shouldn't have used sentinel here - if input all negative
        //   largest would be 0 in error...
        // now won't use unless we see actual inputs so no need to init

    int value;
    while (std::cout << "\nPlease enter an integer: " &&
           std::cin >> value)  // use cin so unparsable input breaks
    {
        if (value == SENTINEL) 
            break;
        if (++num_inputs == 1)
            largest = value;
        else if (value > largest)
            largest = value;
    }

    if (num_inputs > 0)
        std::cout << "\nThe largest value is " << largest << ".\n\n";
    system("PAUSE");  // for Windoze
}

Using num_inputs lets us avoid outputing a message claiming to know the largest value when there were no non-sentinel inputs.

Yet another way to maintain largest is:

else
    largest = std::max(value, largest);

Regarding...

while (std::cout << "\nPlease enter an integer: " &&
       std::cin >> value) 

...this ensures input that can not be parsed as an integer causes the loop to terminate (because std::cin gets set in an eof, fail and/or bad state, any of which causes the std::istream::operator bool() const operator to return false in a boolean evaluation context. The trick of writing the prompt ("Please enter...") in the while condition is just so it's done before the first input attempt, then redone each time through the loop without having to repeat the same std::cout << ... source code at the end of the while loop. It's slightly more correct too, as output to std::cout could fail and terminating the loop's not an unreasonable reaction to that, though people very rarely bother to test the success of cout or cerr output operations.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • Thank you for your help, Tony D! It was very insightful and helped me fix the problem I had where the last statement kept saying "The largest value is 0". – Mary Aug 18 '15 at 05:48
  • Tony D, I did forget to ask, is it okay if the if, else if statements under the while loop were made into 3 separate if statements? – Mary Aug 20 '15 at 22:07