Sorry to trouble, as I am still learning C++, however, can anyone give me any insight how to work this problem. I have 2 functions, one to get the highest number out of the array, and one for the lowest. Both these functions work and give me the correct number, however, the problem is, I need to get the name associated with these numbers. The names are read in from a file (the file is a name and number on each line). If anyone has any tips what so ever - I am more than grateful!!!!!!!!!!

int getNameMost(int num[], string name[], int count)
{
  int highest=num[0];
  for(int i=0; i<count; i++)
   {
  if (highest<num[i])
    highest=num[i];
    }
  return highest;
}
int getNameLeast (int num[], string name[], int count)
{
  int lowest=num[0];
  for(int i=0; i<count; i++)
    {
      if (lowest>num[i])
      lowest=num[i];

    }
  return lowest;
}

Recommended Answers

All 7 Replies

for(int i=0; i<count; i++)
   {
  if (highest<num[i])
    highest=num[i];
    highestindex = i;    
}

But remember you may need to add 1 to highestindex before you output it since your text file probably starts with 1. rather than 0.

I have tried your suggestion above and thus used highestindex as the array value for the name. However, it is returning the name that is the highest value in the array - it is returning name[35] instead of name[0], which is the name associated with the highest number value. Then when I added one to highest index, it is returning no value, as I am assuming that it is trying to return name[36], and there is no name[36] - any further suggestions? :-(

Wow, I missed that one, not sure why. Put some braces around those two statements in the if, otherwise all it does get is the last one. Apologies....

for(int i=0; i<count; i++)
   {
  if (highest<num[i])
   [b] {[/b] 
       highest=num[i];
    highestindex = i;
    [b]}[/b]    
}

But remember you may need to add 1 to highestindex before you output it since your text file probably starts with 1. rather than 0.

What I meant by that statement was if your record is saying
1. Jane Smith 87
2. Bob Jones 89
then it's going to return index 1 for the highest number, but that's your person #2 is all.
(if only I can go back in time and write it like that yesterday)

Holy smokes - you are the best!! It was the braces that were misplaced. Thanks so much! I have stared at this for hours and tried all sorts of different things, low and behold - its the braces. How can something so small - be something so big :-) Thanks again!!!!

if (highest<num[i])
 {
       highest=num[i];
      highestindex=i;
}

did you add them there (I'm asking cause in your snippet you hadn't)?

I added them after I looked a little more closely! Thanks again!!!

YW, sorry I didn't catch it in the first place. Never a prob, post back anytime :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.