Creating a sentinel loop

I am trying to build a program using a sentinel loop and I know I am missing a piece of the puzzle however I am clear on how to make it work. I want the smallest number when I terminate the program by using -99 but I am getting -99 as the smallest number. Any assistance I would greatly appreciate.

Thank you,

Gem925




#include <iomanip>
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;




int main()
{

int n;
int SENTINEL = -99;

cout << "Start here enter > ";
cin >> n;




while (n != SENTINEL)
{
cout << "Please enter the next number > ";
cin >> n;

}

cout << "The smallest number entered was > " << n << endl;




return 0;

}







You aren't storing the smallest number in another variable. Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...

int n;
int SENTINEL = -99;
int smallest = 255;

cout << "Start here enter > ";
cin >> n;

while (n != SENTINEL)
  {
    if (n < smallest) //if 'n' is smaller than the smallest number
      smallest = n; //make the smallest number equal to 'n'
    cout << "Please enter the next number > ";
    cin >> n;
  }

...
Thank you so much this really helped me out.

gem925
Topic archived. No new replies allowed.