-
February 23rd, 2009, 10:19 AM
#1
Help with finding highest and lowest age
Hi, I'm writing a program that find the largest and lowest number from inputted values but I don't know how to get the program to do it.
See below the code I have so far:
Code:
#include(iostream)
using namespace std;
int bob,tim,harry,jon;
do
{
cout << "Enter BOB'S age: ";
cin >> bob;
if (bob<0)
{
cout << "Error, number cannot be under 0";
}
}
while (bob<0)
do
{
cout << "Enter JIM'S age: ";
cin >> jim;
if (bob<0)
{
cout << "Error, number cannot be under 0";
}
}
while (bob<0)
do
{
cout << "Enter TIM'S age: ";
cin >> tim;
if (tim<0)
{
cout << "Error, number cannot be under 0";
}
}
while (tim<0)
do
{
cout << "Enter HARRY'S age: ";
cin >> harry;
if (harry<0)
{
cout << "Error, number cannot be under 0";
}
}
while (harry<0)
}
How could I get the program to find the highest AND lowest aged people. Do I need to totally change this code?
This is what I would like it to look like:
Code:
Enter BOB'S Age: 45
Enter JIM'S Age: 64
Enter TIM'S Age: 22
Enter HARRY'S Age: 65
The oldest fellow is: HARRY at 65
The youngest fellow is: TIM at 22
Thanks in advance for your help
Last edited by p.robinson; February 23rd, 2009 at 10:26 AM.
-
February 23rd, 2009, 10:37 AM
#2
Re: Help with finding highest and lowest age
I don't see a main function there.
Since you're dealing with 4 individual variables, there's no way to find the largest or smallest without explicitly doing 3 compares in each case. (Well, some compares might be re-usable, but let's not over-complicate.)
If you were storing the entries in an array (or any collection, a std::map might be appropriate here) it would be much easier.
-
February 23rd, 2009, 10:38 AM
#3
Re: Help with finding highest and lowest age
you could remember the age in a vector and just sort the vector. if you can't use the sort function, you could write one of your own.
Code:
#include <vector>
#include <algorithm>
int _tmain(int argc, _TCHAR* argv[])
{
vector <int> v1;
vector <int>::iterator Iter1;
v1.push_back(82);
v1.push_back(11);
v1.push_back(76);
v1.push_back(78);
v1.push_back(33);
cout << "Original vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
sort(v1.begin( ), v1.end( ) );
cout << "Sorted vector v1 = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")" << endl;
_getch();
return 0;
}
-
February 23rd, 2009, 10:41 AM
#4
Re: Help with finding highest and lowest age
Note that the desired output includes the name as well as the age. A std::multimap<int,std::string> would do nicely for that without any loops or whatnot.
-
February 23rd, 2009, 10:42 AM
#5
Re: Help with finding highest and lowest age
Originally Posted by Lindley
Note that the desired output includes the name as well as the age. A std::multimap<int,std::string> would do nicely for that without any loops or whatnot.
right...well the OP could customize it according to his needs.
-
February 23rd, 2009, 11:19 AM
#6
Re: Help with finding highest and lowest age
Wouldn't something like this work, then do the same to find the lowest? I'm a beginner myself so I'm curious to the answer of this thread also.
Code:
int highest;
highest = bob;
if (highest < tim)
highest = tim;
else if (highest < jon)
highest = jon;
else if (highest < harry)
highest = harry;
Last edited by brianlb; February 23rd, 2009 at 11:23 AM.
-
February 23rd, 2009, 01:39 PM
#7
Re: Help with finding highest and lowest age
That would certainly get you the highest value. By itself it would *not* also give you the ability to print out the name of the corresponding person, if that's a requirement.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|