Okay I'm gonna be honest with you... I have no idea what I'm doing... no not even a little. I'm in an online C++ class that I'm only taking because it was the last required course for me to graduate this semester... This is the last assignment due and I honestly haven't got a clue where to start... The last assignments I did seemed fairly simple with a little research but this one has completely thrown me off.

If anyone felt like helping me out... or getting me started that'd be great and I'd love you forever. =P I was helped here before and I realize that you're kinda supposed to at least have a basis program or something... I promise you it's not that I'm trying to get anyone to just do my homework for me... I've stared long and hard at this trying to figure out what I should do first... =[ Help?

Here's the assignment:

Write a program that will read student names and percentage grades from the keyboard and then sort them (highest grade first) and calculate average, lowest and highest grade as shown below:

Example :
>How many students do you want to process?
4
>Enter name for student #1 :
JONES
>Enter grade for student #1 :
60
>Enter name for student #2 :
SMITH
>Enter grade for student #2 :
80
>Enter name for student #3
FORSTER:
>Enter grade for student #3 :
55
>Enter name for student #4 :
ALAZAR
>Enter grade for student #4 :
85

>====================
>CLASS GRADE REPORT :
>____________________

>ALAZAR B
>SMITH B
>JONES D
>FORSTER F
>_____________________

>HIGHEST GRADE : 80.00
>LOWEST GRADE : 55.00
>AVERAGE : 70.00
>=====================

Dani AI

Generated

The framework from is a solid starting point. The main gaps to fill are robust input handling (mixing operator>> and getline), a correct descending comparator for sorting, input validation, and clean numeric formatting. The snippet below implements those pieces together and avoids common pitfalls such as division by zero or leftover-newline bugs.

Key implementation notes:

  • After cin >> n consume the rest of the line with ignore() before using getline.
  • Read grades as lines and parse them (reject non-numeric input or out-of-range values).
  • Store records in a vector<pair<string,double>> and use std::sort with a comparator that returns a.second > b.second for highest-first order. Use std::stable_sort if original input order should be preserved on ties.
  • Compute average with std::accumulate and find min/max with std::minmax_element.
  • Format numeric output with std::fixed and std::setprecision(2).
  • Prefer double over float for grade accumulation and guard against zero students.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

std::string letter(double g) {
    if (g >= 90) return "A";
    if (g >= 80) return "B";
    if (g >= 70) return "C";
    if (g >= 60) return "D";
    return "F";
}

int main() {
    int n = 0;
    std::cout << "How many students do you want to process?\n";
    if (!(std::cin >> n) || n <= 0) return 0;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    std::vector<std::pair<std::string,double>> rec;
    for (int i = 0; i < n; ++i) {
        std::string name, line;
        std::cout << "Enter name for student #" << (i+1) << " :\n";
        std::getline(std::cin, name);
        double grade = 0;
        while (true) {
            std::cout << "Enter grade for student #" << (i+1) << " :\n";
            std::getline(std::cin, line);
            std::stringstream ss(line);
            if (ss >> grade && grade >= 0 && grade <= 100) break;
            std::cout << "Invalid grade; enter a number 0-100.\n";
        }
        rec.emplace_back(name, grade);
    }

    std::sort(rec.begin(), rec.end(), [](const auto &a, const auto &b){ return a.second > b.second; });

    double sum = std::accumulate(rec.begin(), rec.end(), 0.0, [](double s, const auto& p){ return s + p.second; });
    auto mm = std::minmax_element(rec.begin(), rec.end(), [](const auto& a, const auto& b){ return a.second < b.second; });

    std::cout << "====================\nCLASS GRADE REPORT :\n____________________\n";
    for (auto &p : rec) std::cout << p.first << "\t" << letter(p.second) << "\n";
    std::cout << "_____________________\n";
    std::cout << "HIGHEST GRADE : " << std::fixed << std::setprecision(2) << mm.second->second << "\n";
    std::cout << "LOWEST GRADE : " << mm.first->second << "\n";
    std::cout << "AVERAGE : " << (sum / rec.size()) << "\n";
    std::cout << "=====================\n";
    return 0;
}

Final notes: this addresses the sort issue mentioned by and avoids system("PAUSE") (not portable). Test with edge cases (single student, identical grades, invalid input) and adapt the letter-grade boundaries to match course rules if they differ from the example.

Recommended Answers

All 3 Replies

I have finished it, but to don't violate the rules of this website --"Don't give away code" , I can not give you all my source code. The following is the framework of program. hope it can help you.

#include <iostream>
#include <vector>

using namespace std;

struct Student
{
  string name;
  float grade;
};

char convert(float grade)
{
  if (grade >= 90)
    return 'A';
  
  if ( grade >= 80)
    return 'B';
   .......
}

void sortStudents(vector<Student>& students)
{
   
} 

float getAverage(const vector<Student>& students)
{
  int size = students.size();
  float total = 0.0;
  
  for (int i=0; i<size; ++i)
  {
   ...............
  }
  
  return total / size;
}

void display(const vector<Student>& students)
{
  cout<<"===================="<<endl;
  cout<<"CLASS GRADE REPORT :"<<endl;
  cout<<"____________________"<<endl;
  
  int size = students.size();
  for (int i=0; i<size; ++i)
  {
    cout<<students[i].name<<"\t"<<convert(students[i].grade)<<endl;
  }
  
  cout<<"_____________________"<<endl;
  cout<<"HIGHEST GRADE : "<<students[0].grade<<endl;
  cout<<"LOWEST GRADE : "<<students[students.size() -1].grade<<endl;
  cout<<"AVERAGE : "<<getAverage(students)<<endl;
  cout<<"====================="<<endl;
} 

int main()
{	
  int number;
  vector<Student> students;
  cout<<"How many students do you want to process?"<<endl;	
  cin>>number;
  
  for (int i=0; i<number; ++i)
  {    
    Student temp;
    //deal with the input and then construct the struct 'temp'
    
    students.push_back(temp);
  }
  
  sortStudents(students);
  
  display(students);
  
  system("PAUSE"); 
  return 0;
}
commented: Helped me out greatly <3 +1

OMG I LOVE YOU! lol no really you're absolutely amazing! You pretty much saved me from not graduating this semester lol

<3333

kindly reminder, the sort function I posted don't work correctly, you should correct it by yourself.

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.