am to create a program that calculates the marks of unknown number of students in a class and arrange them in an order of merit. pls help

Edited 3 Years Ago by victor tawiah

i've been able to do it for 3 students, but i want to do it for an unlimited number of students, wher the user will enter th number of students h/she wants.

this is what i have so far

/*   THIS PROGRAM CALCULATES AND DISPLAYS*/
/*THE ORDER OF MERITS OF SOME STUDENTS IN A SCHOOL */

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    //VARIABLE DECLARATION
    int total1,total2,total3;
    int mark1,mark2,mark3,markI,markII,markIII,markOne,markTwo,markThree;
    string subject1,subject2,subject3;
    string student1,student2,student3;

    cout<<"THIS PROGRAM CALCULATES AND DISPLAYS THE ORDER OF MERITS"<<endl;
     cout<<"OF THREE STUDENTS IN A SCHOOL"<<endl;
    cout<<""<<endl;

    //ACCEPTING INPUTS 
  cout<<"enter the 3 subjects"<<endl;
  cin>>subject1;
  cin>>subject2;
  cin>>subject3;
  cout<<"               "<<endl;

  cout<<"enter the name of the first student"<<endl;
  cin>>student1;
  cout<<""<<endl;

  cout<<"enter the marks for "<<student1<<endl;
  cout<<subject1<<"..... ";cin>>mark1;
  cout<<subject2<<"..... ";cin>>mark2;
  cout<<subject3<<"..... ";cin>>mark3;
  cout<<""<<endl;

  //COMPUTING FOR THE TOTAL
  total1=mark1+mark2+mark3;
  cout<<student1<<"'s total mark is "<<total1<<endl;

  cout<<"             "<<endl;

  //ACCEPTING INPUTS
  cout<<"enter the name of the second student"<<endl;
  cin>>student2;
  cout<<""<<endl;

  cout<<"enter the marks for "<<student2<<endl;
  cout<<subject1<<"..... ";cin>>markI;
  cout<<subject2<<"..... ";cin>>markII;
  cout<<subject3<<"..... ";cin>>markIII;
  cout<<""<<endl;

  //COMPUTING FOR THE TOTAL
  total2=markI+markII+markIII;
  cout<<student2<<"'s total mark is "<<total2<<endl;

  cout<<"              "<<endl;

  //ACCEPTING INPUTS
  cout<<"enter the name of the third student"<<endl;
  cin>>student3;
  cout<<""<<endl;

  cout<<"enter the marks for "<<student3<<endl;
  cout<<subject1<<"..... ";cin>>markOne;
  cout<<subject2<<"..... ";cin>>markTwo;
  cout<<subject3<<"..... ";cin>>markThree;
  cout<<""<<endl;

  //COMPUTING FOR THE TOTAL
  total3=markOne+markTwo+markThree;
  cout<<student3<<"'s total mark is "<<total3<<endl;
  cout<<""<<endl;

  //DISPLAYING THE ORDER OF MERIT
  cout<<"..................................."<<endl;
  cout<<"NOW DISPLAYING THE ORDER OF MERIT ."<<endl;
  cout<<"..................................."<<endl;

  if(total1>total2&&total1>total3)
  {
  cout<<student1<<" was first with "<<total1<<" marks"<<endl;
  }
  else if(total2>total1&&total2>total3)
  {
  cout<<student2<<" was first with "<<total2<<" marks"<<endl;
  }
  else if(total3>total1&&total3>total2)
  {
  cout<<student3<<" was first with "<<total3<<" marks"<<endl;
  }

  if(total1>total2&&total1<total3||total1>total3&&total1<total2)
  {
  cout<<student1<<" was second with "<<total1<<" marks"<<endl;
  }
  else if(total2>total1&&total2<total3||total2>total3&&total2<total1)
  {
  cout<<student2<<" was second with "<<total2<<" marks"<<endl;
  }
  else if(total3>total1&&total3<total2||total3>total2&&total3<total1)
  {
  cout<<student3<<" was second with "<<total3<<" marks"<<endl;
  }

  if(total1<total2&&total1<total3)
  {
  cout<<student1<<" was third with "<<total1<<" marks"<<endl;
  }
  else if(total2<total1&&total2<total3)
  {
  cout<<student2<<" was third with "<<total2<<" marks"<<endl;
  }
  else if(total3<total1&&total3<total2)
  {
  cout<<student3<<" was third with "<<total3<<" marks"<<endl;
  }
    system("PAUSE");
    return EXIT_SUCCESS;
}

Hmm... Here is something you can start with:

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

using namespace std;

struct Student {
    string name;
    vector<int> marks;
    int total;
    void sum() { total = 0; for (int i=0; i<marks.size(); i++) total += marks[i]; return; };
    bool operator() (Student i, Student j) { return (i.total > j.total);}
};

void addStudent(vector<Student>& students)
{
    Student student;
    int mark;

    cout<<"enter the student name: ";
    cin >> student.name;
    cout<<"enter the marks for "<<student.name<<endl;
    for (int i=0; i<3; i++) {
        cout << "mark: ";
        cin >> mark;
        student.marks.push_back(mark);
    };
    student.sum();
    students.push_back(student);

    return;
};

void printStudentsList(vector<Student> students)
{
    cout << "Total   Student" << endl;
    cout << "---------------" << endl;

    for (int i=0; i<students.size(); i++) {
        cout << students[i].total << "      " << students[i].name << endl;
    };

    return;
};

int main()
{
    vector<Student> students;
    Student student;
    int option;

    cout << "1. Add new student" << endl;
    cout << "2. Print list" << endl;
    cout << "3. Exit" << endl;
    cout << "your choice: ";
    cin >> option;
    while (option > 0 && option < 3) {
        switch (option) {
            case 1:
                addStudent(students);
                break;
            case 2:
                sort(students.begin(), students.end(), student);
                printStudentsList(students);
                break;
        };
        cout << "1. Add new student" << endl;
        cout << "2. Print list" << endl;
        cout << "3. Exit" << endl;
        cout << "your choice: ";
        cin >> option;
    };

    return 0;
}

This is just an example. It's up to you to improve the code quality and functionality. Note the use of struct, vector and sort. I hope it will help you.

This article has been dead for over six months. Start a new discussion instead.