# Thread: help with finding lowest number entered

1. ## help with finding lowest number entered

Code:
```#include <iostream>
#include <limits.h>
using namespace std;

void getValues(void);
int findLowest(int);

int main ()

{
getValues();

return 0;

}

void getValues(void)
{
int score;
int count;

cout << "Enter five test scores" << endl;

for (int i = 0; i < 5; i++)
{
cin >> score;

if (score > 100 || score < 0)
{
cout << "You must enter a score less than 100";
cout << " and greater than 0" << endl;
}

else
{

findLowest(score);

if(count == 5)
break;
}
}

cout << "The lowest number is "<< findLowest(score) << endl;

}

int findLowest(int score)
{
int lowest = INT_MAX;

if (score < lowest)
lowest = score;

return lowest;

}```
Why isn't this program returnng the lowest value?

2. Since your cout statement lies outside the loop, it will always print the last inputted value. You should store the lowest value in a variable.

Also, count is uninitialized.

3. Originally posted by Magos
Since your cout statement lies outside the loop, it will always print the last inputted value. You should store the lowest value in a variable.

Also, count is uninitialized.
I tried putting the cout statement inside the loop, but it still didn't do what I wanted it to do.

Also, how would I store the lowest value in a variable?

Maybe like this:

a = findLowest(score);

4. I'm still having trouble with this

5. >I'm still having trouble with this
Not terribly difficult, read the first value and assign it as the lowest. For every subsequent value, check it against the lowest and if it's lower, assign it as the new lowest. When you run out of input, print the value of lowest.
Code:
```#include <iostream>

int main()
{
std::cout<<"Enter five numbers: ";

int lowest;
std::cin>> lowest; // Get the first number

for ( int i = 1; i < 5; i++ ) {
int next;

std::cin>> next; // Get the next number

if ( next < lowest ) // See if it's lower
lowest = next;
}

std::cout<<"The lowest number was: "<< lowest <<std::endl;
}```
-Prelude

6. You made it pretty simply. Thank you, Prelude.

By the way, wouldn't it be a little simpler if you had just used namespace std;?

You're making your cin and cout statements too long with that std:: thing.

7. >By the way, wouldn't it be a little simpler if you had just used namespace std;?
Probably, but simpler isn't always better. I prefer not to pollute the global namespace simply for my own notational convenience.

-Prelude

8. What do you mean by pollute? What do you have against namespace std?

9. >What do you mean by pollute? What do you have against namespace std?
When you say
Code:
`using namespace std;`
What it does is open the whole of the std namespace to be accessable from the global namespace. Even if you only plan on using cin and cout, you still get everything. I consider it bad form to ask for something you don't plan on using, it's akin to doing this:
Code:
```#include <iostream>
#include <fstream>
#include <iosfwd>
#include <ios>
#include <streambuf>
#include <istream>
#include <ostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cstdio>
#include <cwchar>
#include <locale>
#include <utility>
#include <functional>
#include <memory>
#include <iterator>
#include <ctime>
#include <algorithm>
#include <cstdlib>
#include <exception>
#include <stdexcept>
#include <cassert>
#include <cerrno>
#include <string>
#include <cctype>
#include <cwctype>
#include <cstring>
#include <limits>
#include <climits>
#include <cfloat>
#include <new>
#include <typeinfo>
#include <cstddef>
#include <cstdarg>
#include <csetjmp>
#include <csignal>
#include <complex>
#include <valarray>
#include <numeric>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>

int main()
{
std::cout<<"Hello, world!\n";
}```
What a waste, you only use one of those headers, so why include them all? The principle is the same, why make everything in namespace std accessable when you only use one or two of the names it defines?

-Prelude

10. But namespace std doesn't consume a lot of memory or cause any errors or warnings, right?

Prelude, you seem like the type of person who would buy an item that costs \$9.95 with \$9.95 instead of a ten dollar bill.

By the way, do you like my avatar?

11. >But namespace std doesn't consume a lot of memory or cause any errors or warnings, right?
No, the only bad thing about it is potential name clashes and namespace pollution. Which is why I do my own thing and don't tell other people what to do in a matter of style.

>Prelude, you seem like the type of person who would buy an item that costs \$9.95 with \$9.95 instead of a ten dollar bill.
Well, if I have exact change then yes.

>By the way, do you like my avatar?

-Prelude

12. Originally posted by Prelude
What a waste, you only use one of those headers, so why include them all? The principle is the same, why make everything in namespace std accessable when you only use one or two of the names it defines?
You know,you could do this:
Code:
```using std::cout;
using std::cin;```
with the stuff you want, leaving the rest out.

13. >You know,you could do this:
Yes, I know. If the prefix form takes up too much space then that's precisely what I do.

-Prelude