C++ Tutorial

C++ Project: Student Manager

Store students in a vector of structs, print the roster, and find the top score.

What you will build

A student manager keeps a roster in memory: each person has a name and a score. You print every row, then walk the same list once more to find the highest score and the student who earned it. The container is avector. The row type is a struct.

Names and scores are hardcoded so the program runs without cin. Open/cpp/try with Try it in C++. That is the C++ editor.

A Student struct

A struct groups fields that belong together. name is a string. score is an int. After the closing brace of the struct you need a semicolon. One object is one student. The roster will hold several objects.

Example

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

struct Student {
  string name;
  int score;
};

int main() {
  Student a;
  a.name = "Ada";
  a.score = 91;
  cout << a.name << " " << a.score << endl;
  return 0;
}

Output is Ada 91. The two values travel as one record instead of two parallel variables.

A vector of students

Include <vector>. The type is vector<Student>.push_back adds one student at the end. size() is how many rows you have. A range-for visits each object in order.

Example

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Student {
  string name;
  int score;
};

int main() {
  vector<Student> roster;
  roster.push_back({"Ada", 91});
  roster.push_back({"Lin", 84});
  roster.push_back({"Omar", 97});

  cout << "Count: " << roster.size() << endl;
  for (const Student& s : roster) {
    cout << s.name << " " << s.score << endl;
  }
  return 0;
}

Run this at /cpp/try with Try it in C++. Add a fourth student withpush_back and compile again. The loop picks up the new row without a new index variable.

Print a roster

A dedicated print function keeps main short. Pass the vector by const reference so the function can read every field without copying the whole list. Print a header first so the columns are obvious.

Example

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Student {
  string name;
  int score;
};

void printRoster(const vector<Student>& roster) {
  cout << "Name   Score" << endl;
  for (const Student& s : roster) {
    cout << s.name << "   " << s.score << endl;
  }
}

int main() {
  vector<Student> roster;
  roster.push_back({"Ada", 91});
  roster.push_back({"Lin", 84});
  roster.push_back({"Omar", 97});
  printRoster(roster);
  return 0;
}

Complete program: top score

Start from the first student as the current best. Walk the rest of the vector. When a score is higher, keep that student instead. If the roster is empty, print a message and skip the search. Ties keep the first name that reached that score.

Example

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Student {
  string name;
  int score;
};

void printRoster(const vector<Student>& roster) {
  cout << "Name   Score" << endl;
  for (const Student& s : roster) {
    cout << s.name << "   " << s.score << endl;
  }
}

void printTop(const vector<Student>& roster) {
  if (roster.empty()) {
    cout << "No students" << endl;
    return;
  }
  Student top = roster[0];
  for (size_t i = 1; i < roster.size(); i++) {
    if (roster[i].score > top.score) {
      top = roster[i];
    }
  }
  cout << "Top: " << top.name << " with " << top.score << endl;
}

int main() {
  vector<Student> roster;
  roster.push_back({"Ada", 91});
  roster.push_back({"Lin", 84});
  roster.push_back({"Omar", 97});
  roster.push_back({"Nia", 88});
  printRoster(roster);
  printTop(roster);
  return 0;
}

Omar has 97, the highest value in this demo. Change Lin to 99 and the top line should name Lin. The roster print stays in the same order you pushed.

Common mistakes

  • Parallel vectors, one of names and one of scores. A struct keeps the pair aligned. If you delete a name from one list and forget the matching score, the rows no longer match.
  • Passing the roster by value into print. That copies every student. Useconst vector<Student>&.
  • Calling roster[0] when the vector is empty. Check empty() first.
  • Using >= in the top-score loop when you want the first winner of a tie. >keeps the earlier name.

Practice

  1. Print the lowest score as well, with the student name.
  2. Compute the class average as a double and print it after the roster.
  3. Count how many students scored at least 90 and print that count.

Next project: an Account class that refuses a withdrawal when the balance would go negative.

FAQ: C++ Project: Student Manager

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ students project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ students project in this C++ C++ lesson (C++ Project: Student Manager).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.