Click here to Skip to main content
15,171,053 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<pre>
#include <iostream>
#include <string>
#include <map> 

void Highscore(int string);

using namespace std;
int main() {

	map<int, string> Students;

	Students.insert(std::pair<int, string>(200, "Henry"));

	Students.insert(std::pair<int, string>(201, "Rogers"));

  Students.insert(std::pair<int, string>(178, "Bright"));

	cout << "Class size is: " << Students.size() << endl;

	cout << endl << "Grades in order is: " << endl;

	for (map<int, string>::iterator it = Students.begin(); it != Students.end(); ++it) 

		cout << (*it).first << ": " << (*it).second << endl;
   

   void Highscore (int string){
   
     int highnum = 0;
     for (int i = 0; i < string; i++) {
    if (num[i] > highnum)
            highnum = num[i];
        cout << num[i];
    }
    cout << "The highest grade is: "<< num[i]<< "!"<< endl;
   }
   
 

return 0;
}






What I have tried:

There is only one error in the compiler which is the '{', after the i++. It says "function definition not allowed here. I have had success with the maps compiling in with the grades next to them however I want the code to print who has the highest score.
Posted
Updated 1-Nov-21 13:45pm

You do not need to look for a highscore in your map via loop. Map sorts all of its keys upon insertion so your highest score will always be
C++
std::map<int, std::string>::end()-1; or std::map<int, std::string>::rbegin();
and lowest score will always be
C++
std::map<int, std::string>::begin();
   
v5
Comments
CPallini 2-Nov-21 3:24am
   
Indeed.
My 5.
You are defining the function Highscore within the body of main. Move it above main. Defining one function inside another is not allowed.
   
When you correct the error already noted, you will find there is another error. Here is the Highscore function separated out :
C++
void Highscore( int string )
{
    int highnum = 0;
    for (int i = 0; i < string; i++)
    {
    i   if (num[i] > highnum)
            highnum = num[i];
        cout << num[i];
    }
    cout << "The highest grade is: "<< num[i]<< "!"<< endl;
}
First, the argument to this function is very, very poorly named and I can't think of a worse name for it off hand. Second, it seems like it could be done in a better way. Generally speaking, I try to avoid implementations that determine a result and then just output it. I think it is better to return the result so it can be used in other ways. For this, I would change this function to return the item in the map with the highest score. Also, from what I can see, that function uses an undefined data item - num.

I would rewrite this function to use an iterator, as you have in another place, and return the value of that iterator. I would also define some types for your map and pair to simplify things a little bit. Here is how all of that could look :
C++
typedef std::map< int, std::string >  mintstr;
typedef std::pair< int, std::string > pintstr;

// output the contents of the map

void OutputScores( const mintstr & stmap )
{
    int count = 1;
   	for( mintstr::iterator it = stmap.begin(); it != stmap.end(); ++it )
    {
        std::cout << count << ".  " << (*it).first << " : " << (*it).second << std::endl;
        ++count;
    }
}

// determine the highest score and return an iterator pointing to it

mintstr::iterator Highscore( const mintstr & stmap )
{
    int highest = -1;     // initialize to an impossible value
    mintstr::iterator hit = stmap.end();

    for( mintstr::iterator it = stmap.begin(); it != stmap.end(); ++it )
    {
        if( highest < (*it).first )
        {
            highest = (*it).first;
            hit = it;
        }
    }
    return hit;
}


int main()
{
    mintstr students;

    students.insert( pintstr( 200, "Henry" ) );
    students.insert( pintstr( 201, "Rogers" ) );
    students.insert( pintstr( 178, "Bright" ) );

	std::cout << "Class size is: " << students.size() << std::endl;
	std::cout << std::endl << "Grades in order is: " << std::endl;

    OutputScores( students );

    auto it = Highscore( Students );
    std::cout << "highest is " << (*it).first << " : " << (*it).second << std::endl;

    return 0;
}
You might find that the iterator should be a const_iterator. The compiler will let you know if that is required.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900