E
E
Egor2016-05-02 13:35:48
C++ / C#
Egor, 2016-05-02 13:35:48

How to make std::sort work?

The vector is not sorted, what should I do?
Condition:


At the input of the program, information is given about the passing of exams by students of the 9th grade of a certain secondary school. The first line reports the number of students N, which is not less than 10, but does not exceed 100, each of the following N lines has the following format: <Last name> <First name> <marks>, where <Last name> is a string consisting of no more than 20 characters, <Name> is a string consisting of no more than 15 characters, <ratings> is three integers separated by a space, corresponding to grades on a five-point system. <Last name> and <First name>, as well as <First name> and <grades> are separated by one space. Input string example:
Ivanov Petr 4 2 4
It is required to write a program that is as efficient as possible (indicate the version of the programming language used, for example, Borland Pascal 7.0), which will display the last names and first names of underachieving students (having at least one two in the exam results), arranging them in decreasing order of the number of twos.

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <algorithm>



class Losers
{
  public:
    std :: string name, lastname;
    int priority;
 
    Losers () : priority ( 0 ) {  }
 
    void error () 
    {
      std :: cout << "Error!" << std :: endl;
    }  
};



int main ( void )
{
  int numbers;
  std :: string name, lastname;
  int one, two, three;

  Losers error;
  std :: vector <Losers> losNL ( 0 );

  std :: cin >> numbers;

  if ( numbers > 2 && numbers < 100 )
  {
    for ( int i = 0; i < numbers; ++i )
    {
      std :: cin >> std :: setw ( 15 ) >> name; 
      std :: cin >> std :: setw ( 20 ) >> lastname;
      std :: cin >> one >> two >> three;

      bool protectOne = one == 2  || one == 3 || one == 4 || one == 5; 
      bool protectTwo = two == 2  || two == 3 || two == 4 || two == 5; 
      bool protectThree = three == 2  || three == 3 || three == 4 || three == 5; 

      if ( protectOne && protectTwo && protectThree )
      {
        if ( one == 2 || two == 2 || three == 2 )
        {
          losNL.resize ( losNL.size () + 1 ); 
         
          losNL[ i ].name = name;
          losNL[ i ].lastname = lastname;
 
          if ( one == 2 && two == 2 && three == 2 ) 
          {
            losNL[ i ].priority = 3;
          } 

          if ( one == 2 && two == 2 || one == 2 && three == 2 || two == 2 && three == 2 ) 
          { 
            losNL[ i ].priority = 2;
          }

          if ( one == 2 || two == 2 || three == 2 ) 
          { 
            losNL[ i ].priority = 1;
          }
        }
        else 
        {
          --i; --numbers;
        }
      }
      else 
      {
        error.error ();
      }
    }   
  }
  else 
  {
    error.error (); 
  }

  std::sort ( losNL.begin (), losNL.end (), []( const Losers & a, const Losers & b ) 
  {
    return ( a.priority < b.priority );
  });

  for ( int i = 0; i < losNL.size (); ++i )
  {
    std :: cout << losNL[ i ].name << " " << losNL[ i ].lastname << std :: endl;
  }

  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-05-02
@Nemovok

There's an error

if ( one == 2 && two == 2 && three == 2 ) 
          {
            losNL[ i ].priority = 3;
          } 

          if ( one == 2 && two == 2 || one == 2 && three == 2 || two == 2 && three == 2 ) 
          { 
            losNL[ i ].priority = 2;
          }

          if ( one == 2 || two == 2 || three == 2 ) 
          { 
            losNL[ i ].priority = 1;
          }

If there is at least one deuce, then the priority will be 1. Notice, not "exactly one", but at least one. It is necessary to interpose else after if'ov.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question