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";
}
#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 (constauto& 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 ++
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 :