N1995
N1995

Reputation: 3

using the STL list to get highest value

Write a C++ program that asks the user to enter an integer m followed by m other names of students and numbers representing their final grades out of 100. Every time, the user has to input a name and a grade. Names and grades will be stored in separate lists.

After getting all the names and grades, the program will find and display the highest grade and the name of the student who has it

I tried it and was able to get the highest grade ,but the name I had it wrong ...

plzzzzzzzzzzzzzzzzzzzzz help!!!!!

#include <cstdlib>
#include<iostream>
#include<string>
#include<list>
using namespace std;

int main() {
    list<string>names;
    list<int>grades;
    int m, grade;
    string studentname;
    cout << "Enter m names and m grades \n";
    cin>>m;
    for (int i = 0; i < m; i++) {
        cout << "Enter students name " << i + 1 << ":" << endl;
        cin>>studentname;
    names.push_back(studentname);
        cout << "Enter students grade " << i + 1 << ":" << endl;
        cin>>grade;
    grades.push_back(grade);
    }
   list<string>::iterator tn; //iterator tn to read a list of names
   list<int>::iterator tg;  //iterator tg to read a list of grades
   float highest;
   string name;
   tn = names.begin(); //to point to the first name
   tg = grades.begin(); //to point to the first grade
   highest = *tg; //suppose that the highest grade is the first grade
   name = *tn;   //suppose that the first student has the highest grade
   tg++;  //to move to the next grade
   tn++;  //to move to the next name
   for (tg; tg != grades.end(); tg++) {
      if (highest<*tg) {
          highest=*tg;
          grades.pop_back();
      }
   tn++; //to read in the list of students’ names
   }
   cout << "----------\n";
   cout << "Highest grade: " << highest << " for: " << name;
   cout << "\n----------\n";
   return 0;
}

Upvotes: 0

Views: 447

Answers (2)

Richard Hodges
Richard Hodges

Reputation: 69864

This answer is for your interest only.

While I would not suggest that you submit this code to your tutor (but by all means discuss it with him/her after class), this is how I would approach this task "in the real world".

Synopsis:

  • All algorithmic logic expressed in terms of std algorithms.

  • Lists replaced with vectors on grounds of efficiency.

  • In-built test harness allows the program to be run with the --test option in order to check logic, and to that end...

  • Logic decoupled from IO streams.

  • Error handling in the case of incomplete or invalid input.

  • Use of a template function to acquire input, obviating the need for duplicating logic.

This code requires c++11, which ought to be the very minimum that you are learning.

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

template<class...Ts>
std::istream& acquire(std::istream& stream, const char* prompt, Ts&...ts)
{
    if (std::addressof(stream) == std::addressof(static_cast<std::iostream&>(std::cin))) {
        std::cout << prompt;
        std::cout.flush();
    }
    using expand = int[];
    void(expand { 0, ((stream >> ts),0)... });
    return stream;
}

bool args_have_test(const std::vector<std::string>& args)
{
    auto ifind = std::find(std::begin(args), std::end(args), "--test");
    return ifind != std::end(args);
}

[[noreturn]]
bool input_error(const char* context)
{
    std::cerr << "input error while " << context << std::endl;
    exit(2);
}


int main(int argc, const char* const *argv)
{
    std::istringstream stest {
        "5\n"
        "bob 5\n"
        "bill 2\n"
        "bernie 9\n"
        "bert 7\n"
        "bart 8\n"
    };
    auto args = std::vector<std::string> { argv + 1, argv + argc };
    auto& stream = args_have_test(args) ? static_cast<std::istream&>(stest)
    : static_cast<std::istream&>(std::cin);
    int count = 0;
    acquire(stream, "enter number of students: ", count)
    or input_error("entering number of students");

    std::vector<std::string> names;
    names.reserve(count);
    std::vector<int> grades;
    grades.reserve(count);
    std::string name;
    int grade;
    while (count--) {
        acquire(stream, "enter name and grade followed by enter: ", name, grade)
        or input_error("entering name and grade");

        names.push_back(name);
        grades.push_back(grade);
    }
    auto imax = std::max_element(std::begin(grades), std::end(grades));
    if (imax == std::end(grades)) {
        std::cerr << "empty list\n";
        exit(1);
    }
    auto iname = std::next(std::begin(names), std::distance(std::begin(grades),
                                                            imax));
    std::cout << "highest grade was " << *imax << " acheived by " << *iname << std::endl;
    return 0;
}

Upvotes: 0

user31264
user31264

Reputation: 6727

You set the name as name = *tn; before the loop, and never change it later. What do you expect?

Upvotes: 2

Related Questions