Thread: help with finding lowest number entered

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    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. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    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. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    I'm still having trouble with this

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

  6. #6
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    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. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

  8. #8
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    What do you mean by pollute? What do you have against namespace std?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

  10. #10
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    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. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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?
    Of course, I made it.

    -Prelude
    My best code is written with the delete key.

  12. #12
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the 3 largest numbers of the five entered
    By Bkgrant in forum C Programming
    Replies: 11
    Last Post: 02-13-2009, 01:08 PM
  2. Finding largest number in array
    By el_chupacabra in forum C Programming
    Replies: 2
    Last Post: 01-22-2009, 02:31 PM
  3. finding the largest number in a binary search
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 07-31-2008, 03:19 AM
  4. Finding the number of different paths on a rectangular grid
    By joejoefla in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2004, 02:13 PM
  5. finding already entered number
    By volk in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2003, 10:12 PM