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. Use
const vector<Student>&. - Calling
roster[0]when the vector is empty. Checkempty()first. - Using
>=in the top-score loop when you want the first winner of a tie.>keeps the earlier name.
Practice
- Print the lowest score as well, with the student name.
- Compute the class average as a
doubleand print it after the roster. - 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.