C++ help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Undermine
    New Member
    • Oct 2008
    • 6

    C++ help

    [PHP]#include <iostream>

    int main()
    {
    using std::cout;
    using std::endl;
    using std::cin;

    int value, count=0, sum=0, average,total, smallest, largest;

    for(;;) // i.e. loop forever .... until break
    {
    for(;;) // ever ...
    {
    cout << "Enter value " << count+1 << ": ";
    value = -9999;
    cin >> value;
    if (value > largest )
    largest=value;
    cin.clear();
    cin.sync();

    if ( value == -9999 ) { cout << "Bad data entry ... "; continue; }
    if ( value == 9999 ) break; // break out of inner loop

    ++count;

    total= sum += value;
    average= total/count;



    } // end of inner forever loop ...

    if (value == 9999 ) break; // break out of outer loop



    } // end of outer forever loop ...


    cout << "For " << count << " values entered ...\n"
    // << "Smallest = " << smallest << endl
    << "Largest = " << largest << endl
    << "Sum = " << total << endl
    << "Average = " << average << endl;

    cout << "\nPress 'Enter' to continue ... ";
    cin.get();
    }[/PHP]

    I have this loop and it's supposed to find the largest # and smallest #, now i found largest, but i want to not include the sentinel 9999 exit loop value, how would i do that. And what would i do to find the smallest, nothing's working =(
  • mkborregaard
    New Member
    • Sep 2007
    • 20

    #2
    Test for '== 9999' (or better '>=9999') before setting 'largest' to 'value'. I do not see why you could not use 'if (value < smallest) value = smallest' to find the smallest? - alternatively you could use
    Code:
    smallest = min(smallest, value);
    But nothing will work unless you initialize smallest and largest properly, e.g.
    Code:
     smallest = 9998; largest = -9998;

    Comment

    Working...