Add and Remove a student vectors

okay here my new code that boosted my grade, however I am still haveing a hard time on Add and remove the vectors of students and i have checked throughly over and over and still is not satisfy with it. here is my other code of it.
[code]
Put the code you need help with here.
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int main() {
// arrays for names and grades
vector<string> names;
vector<double> grades;

//get the students info for add and remove
int numStudents {};
double grade {};
string firstName;
string lastName;
string fullName;
string StudentInfo;
char selection {};



cout << "Welcome to the student roster! " ;
cout << "How many students are in your class?: " ;
cin >> numStudents;
//Read in Student Info
for (int i = 0; i < numStudents; ++i){
cout << "Please enter student (First Last Grade) info: ";
cin >> firstName >> lastName >> grade;
string fullName = firstName + " " + lastName;
names.push_back(firstName + " " + lastName);
grades.push_back(grade);
}

//Print Menu and Thanks
cout << "Thank you for entering your students information! " << endl;
cout << "Please choose one of the following options: " << endl
<< "a: add a Student" << endl
<< "r: remove a Student" << endl
<< "p: print the class summary" << endl
<< "m: print menu" << endl
<< "q: quit program" << endl;


cout << "Selection: " ;
cin >> selection;
//Read in Initial Selection for loop
while(selection != 'q') {
// add a Student
if(selection == 'a') {
cout << StudentInfo << fullName;
cout << "Please enter student (First Last Grade) info: ";
cin >> firstName >> lastName >> grade;
string fullName = (firstName + " " + lastName);
names.push_back(firstName + " " + lastName);
grades.push_back(grade);
}
//remove Student
if(selection == 'r') {
cout << "\nPlease enter student (First Last ) info: " ;
cin >> firstName >> lastName >> grade;
string fullName = firstName + " " + lastName;
// ask the user which student name to be remove
for (size_t i {}; i < names.size(); i++) {
if (names.at(i) == fullName){
names.erase(names.begin() +i);
grades.erase(grades.begin() + i);
cout << "Removing:" << fullName;
}
}
}


// print Class Summary
if(selection =='p') {
double sum {};
cout << "Class Summary\n";
cout << "------------------------\n";
cout << "Name" "Grade\n";
cout << "---------" "--------\n";
for (size_t i {}; i < names.size(); ++i) {
cout << left << setw(20) << names[i];
cout << right << setw(10) << fixed << setprecision(2) << grades[i] << '\n';
//cout << names[i] << setw(20) << grades[i] << '\n';
sum += grades[i];
}
cout << "Number of Students:\n";
cout << "-------------------\n";
cout << names.size() << '\n';

cout << "Average Grade:\n";
cout << "--------------\n";
cout << fixed << setprecision(20) << sum / names.size() << '\n';
}
// print menu display
if(selection == 'm') {
cout << "Please choose one of the following options:\n" ;
cout << "a: add a Student\n" ;
cout << "r: remove a Student\n" ;
cout << "p: print the class summary\n" ;
cout << "m: print menu\n" ;
cout << "q: quit program\n";
}

while(selection != 'q') {
cout << endl;
cin >> selection;
cout << "Selection: " ;
cout << endl;

cout << "Not a valid selection. " ;
}
}
return 0;
}
code]
A for effort, but you need to fix the closing code tag to get it to format properly.
trust me I did even in the code tag, and it still did not submit my original formatting.
Take a look at your last tag. You forgot something :/

I think that your code could be better if you create a structure for each student with all parameters :
https://cplusplus.com/reference/vector/vector/

A clever example here using a structure :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
#include <vector>

struct student {
    std::string name;
    int age;
    std::string book;
};

int main() {

    std::vector<student> allStudents;

    allStudents.push_back(student({ "Joe", 17, "Lord Byron poems" }));
    allStudents.push_back(student({ "Nina", 19, "The Last of Mohicans" }));
    allStudents.push_back(student({ "Fred", 18, "Animal Farm" }));

    for (const auto& s : allStudents) {
        std::cout << "Name: " << s.name << std::endl
                  << "Age: " << s.age << std::endl
                  << "Book: " << s.book << std::endl << std::endl;
    }

    std::cout << "Give me a valid name : ";
    std::string who;
    std::cin >> who;
    // naive approach - but it works :)
    for (int i = 0; i < allStudents.size(); i++)
    {
        if (allStudents[i].name == who)
        {
            std::cout << who << " reads this book : " << allStudents[i].book << std::endl;
            break;
        }
    }

    return 0;
}


According to the reference that I posted above, you can do everything on this vector of Student structure. You can sort them, add another one, erase an entry, find an index and more more more...
Really you can simplify your life creating a structure for each student. I hope that I helped you ++
Last edited on
Without using a struct, possibly something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <vector>
#include <iomanip>

void add(std::vector<std::string>& names, std::vector<double>& grades) {
	std::string firstName, lastName;
	double grade {};

	std::cout << "Please enter student (First Last Grade) info: ";
	std::cin >> firstName >> lastName >> grade;

	names.push_back(firstName + " " + lastName);
	grades.push_back(grade);
}

void remove(std::vector<std::string>& names, std::vector<double>& grades) {
	std::string firstName, lastName;

	std::cout << "\nPlease enter student (First Last ) info: ";
	std::cin >> firstName >> lastName;

	const std::string fullName { firstName + " " + lastName };

	for (size_t i {}; i < names.size(); ++i)
		if (names[i] == fullName) {
			names.erase(names.begin() + i);
			grades.erase(grades.begin() + i);
			std::cout << "Removing:" << fullName << '\n';
			return;
		}

	std::cout << "Name not found!\n";
}

void display(const std::vector<std::string>& names, const std::vector<double>& grades) {
	double sum {};

	std::cout << "Class Summary\n";
	std::cout << "------------------------------\n";
	std::cout << std::left << std::setw(20) << "Name" << std::right << std::setw(10) << "Grade" << '\n';
	std::cout << std::left << std::setw(20) << "---------" << std::right << std::setw(10) << "------" << '\n';

	for (size_t i {}; i < names.size(); ++i) {
		std::cout << std::left << std::setw(20) << names[i];
		std::cout << std::right << std::setw(10) << std::fixed << std::setprecision(2) << grades[i] << '\n';
		sum += grades[i];
	}

	std::cout << "\nNumber of Students:\n";
	std::cout << "-------------------\n";
	std::cout << names.size() << '\n';

	std::cout << "\nAverage Grade:\n";
	std::cout << "--------------\n";
	std::cout << std::fixed << std::setprecision(2) << sum / names.size() << '\n';
}

void menu() {
	std::cout << "\nPlease choose one of the following options:\n"
		<< "a: add a Student\n"
		<< "r: remove a Student\n"
		<< "p: print the class summary\n"
		<< "m: print menu\n"
		<< "q: quit program\n";
}

int main() {
	std::vector<std::string> names;
	std::vector<double> grades;
	size_t numStudents {};

	std::cout << "Welcome to the student roster!\n";
	std::cout << "How many students are in your class?: ";
	std::cin >> numStudents;

	for (size_t i {}; i < numStudents; ++i)
		add(names, grades);

	std::cout << "Thank you for entering your students information!\n";
	menu();

	for (char selection {}; selection != 'q'; ) {
		std::cout << "\nSelection: ";
		std::cin >> selection;

		switch (selection) {
			case 'a':
				add(names, grades);
				break;

			case 'r':
				remove(names, grades);
				break;

			case 'p':
				display(names, grades);
				break;

			case 'm':
				menu();
				break;

			case 'q':
				break;

			default:
				std::cout << "Not a valid selection. ";
				break;
		}
	}
}

> I think that your code could be better if you create a structure

Yes. Keep closely related information together, as a unit: use a struct

And for remove, use the erase-remove idiom.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <vector>

struct student {

    std::string name ;
    double grade = 0 ;
    // ...
};

bool remove( std::vector<student>& students, const std::string& name ) { // return true on success

    // erase remove idiom: https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom
    // https://en.cppreference.com/w/cpp/container/vector/erase2
    return std::erase_if( students, [&]( const student& s ) { return s.name == name ; } ) ;
}

http://coliru.stacked-crooked.com/a/3f3da21408586148
Another example with another way to remove a student from the list by his name.
Almost the same that JLBorges has posted above - just another semantic :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <vector>

struct student {
    std::string name;
    int age;
    std::string book;
};

void removeByName(std::vector<student>& s, std::string name) {
    s.erase(
        std::remove_if(s.begin(), s.end(), [&](student const& ss) {
            return ss.name == name;
            }),
    s.end());
}

int main() {

    std::vector<student> allStudents;

    allStudents.push_back(student({ "Joe", 17, "Lord Byron poems" }));
    allStudents.push_back(student({ "Nina", 19, "The Last of Mohicans" }));
    allStudents.push_back(student({ "Fred", 18, "Animal Farm" }));

    for (const auto& s : allStudents) {
        std::cout << "Name: " << s.name << std::endl
                  << "Age: " << s.age << std::endl
                  << "Book: " << s.book << std::endl << std::endl;
    }

    std::cout << "Student who returned his book : ";
    std::string who;
    std::cin >> who;

    removeByName(allStudents, who);
    std::cout << "This is now the new list with students and books : " << std::endl << std::endl;

    for (const auto& s : allStudents) {
        std::cout << "Name: " << s.name << std::endl
                  << "Age: " << s.age << std::endl
                  << "Book: " << s.book << std::endl << std::endl;
    }

    return 0;
}
Last edited on
Using a struct, then possibly as C++20:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <vector>
#include <iomanip>

struct Student {
	std::string name;
	double grade {};

	Student(const std::string& first, const std::string& last, double g) : name(first + ' ' + last), grade(g) {}

	bool operator== (const std::string& s) const { return s == name; }
};

using Data = std::vector<Student>;

void add(Data& stds) {
	std::string firstName, lastName;
	double grade {};

	std::cout << "Please enter student (First Last Grade) info: ";
	std::cin >> firstName >> lastName >> grade;

	stds.emplace_back(std::move(firstName), std::move(lastName), grade);
}

void remove(Data& stds) {
	std::string firstName, lastName;

	std::cout << "\nPlease enter student (First Last ) info: ";
	std::cin >> firstName >> lastName;

	if (!std::erase(stds, firstName + ' ' + lastName))
		std::cout << "Name not found!\n";
}

void display(const Data& stds) {
	double sum {};

	std::cout << "Class Summary\n";
	std::cout << "------------------------------\n";
	std::cout << std::left << std::setw(20) << "Name" << std::right << std::setw(10) << "Grade" << '\n';
	std::cout << std::left << std::setw(20) << "---------" << std::right << std::setw(10) << "------" << '\n';

	for (const auto& [nam, grad] : stds) {
		std::cout << std::left << std::setw(20) << nam;
		std::cout << std::right << std::setw(10) << std::fixed << std::setprecision(2) << grad << '\n';
		sum += grad;
	}

	std::cout << "\nNumber of Students:\n";
	std::cout << "-------------------\n";
	std::cout << stds.size() << '\n';

	std::cout << "\nAverage Grade:\n";
	std::cout << "--------------\n";
	std::cout << std::fixed << std::setprecision(2) << sum / stds.size() << '\n';
}

void menu() {
	std::cout << "\nPlease choose one of the following options:\n"
		<< "a: add a Student\n"
		<< "r: remove a Student\n"
		<< "p: print the class summary\n"
		<< "m: print menu\n"
		<< "q: quit program\n";
}

int main() {
	Data stds;
	size_t numStudents {};

	std::cout << "Welcome to the student roster!\n";
	std::cout << "How many students are in your class?: ";
	std::cin >> numStudents;

	for (size_t i {}; i < numStudents; ++i)
		add(stds);

	std::cout << "Thank you for entering your students information!\n";
	menu();

	for (char selection {}; selection != 'q'; ) {
		std::cout << "\nSelection: ";
		std::cin >> selection;

		switch (selection) {
			case 'a':
				add(stds);
				break;

			case 'r':
				remove(stds);
				break;

			case 'p':
				display(stds);
				break;

			case 'm':
				menu();
				break;

			case 'q':
				break;

			default:
				std::cout << "Not a valid selection. ";
				break;
		}
	}
}

Topic archived. No new replies allowed.