I'm beginner of c++, no knowledge on c++. I'm writing a code for student and module. The requirement is need to displayed the module and the student with final score, also the duplication. When I write the case 5 and case 6, after compile it show the error below.
error C2360: initialization of 'code' is skipped by 'case' label see declaration of 'code' error C2361: initialization of 'code' is skipped by 'default' label see declaration of 'code'
error C2039: 'getId' : is not a member of 'std::vector<_Ty>' with[_Ty=Student]
error C2039: 'getModule' : is not a member of 'std::vector<_Ty>' with[_Ty=Student]
Main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include "Student.h"
#include "Module.h"
using namespace std;
void printMenu();
vector<Student> readFile();
void liststudentRecord(vector<Student>);
string trim(string);
bool isValidClasscode(string s);
bool isvalidStudentid(string s);
bool test1Ascending(Student s1, Student s2);
bool test1Desscending(Student s1, Student s2);
bool test1finalScoreAscending(Student s1, Student s2);
vector<Module> readFile2();
void listmoduleRecord(vector<Module>);
bool moduleName (Module m1, Module m2);
vector<Student> getStudentByModule(vector<Student>,string);
bool isDuplicateStudentRecord(vector<Student>, Student);
//gloval variable
vector<Student> duplicates;
int main() {
int choice;
vector<Student> students;//list of student
vector<Module> modules;
vector<Student> list;
do {
printMenu();
cin >> choice;
switch(choice) {
case 1:
cout << "Read Student file ... " << endl;
students =readFile();
cout<<"Number of records read: " <<students.size() <<endl;
break;
case 2:
liststudentRecord(students);
break;
case 3:
cout << "Read Module file ... " << endl;
modules =readFile2();
cout<<"Number of records read: " <<modules.size() <<endl;
break;
case 4:
listmoduleRecord(modules);
break;
case 5:
liststudentRecord(duplicates);
break;
case 6:
cout << "Enter Module Code: ";
string code;
cin >> code;
list = getStudentByModule(students, code);
for (int i=0; i < list.size(); i++ ){
Student s = list.at(i);
cout << s.toString() << endl;
}
cout<<"List Student by Module Code"<<endl;
break;
case 7:
//http://www.cplusplus.com/reference/algorithm/sort/
//sort(students.begin(), students.end(), test1finalScoreAscending);
sort(students.begin(), students.end(),test1finalScoreAscending); //finalScoreAcending
break;
case 8:
break;
case 9: cout << "Exiting program... " << endl; break;
default:
cout << "Invalid option specified. Please try again" << endl;
}
} while(choice != 9);
return 0;
}
void printMenu() {
cout << "Menu" << endl;
cout << "----" << endl;
cout << "[1] Read student file" << endl;
cout << "[2] List student records" << endl;
cout << "[3] Read module file" << endl;
cout << "[4] List module records" << endl;
cout << "[5] List Duplicate Record" << endl;
cout << "[6] Display Student by Module" << endl;
cout << "[7] Sort by Final Score" <<endl;
cout << "[8] Write to file" << endl;
cout << "[9] Exit" << endl;
cout << "Choice: ";
}
vector<Student>readFile(){
vector<Student>students;
string filename;
ifstream inputFile;
cout << "Enter filename: ";
cin >> filename;
inputFile.open(filename, ios::in);//open file, read only
if(inputFile.is_open()){
while (inputFile.good()){
string line;
getline(inputFile, line);
Student s; //create a new student
//extract id
int i = line.find(",");
string id = trim(line.substr(0, i));// extract id
s.setId(id); // set student id
line = line.substr(i+1);// remove id form line
/*if(isvalidStudentid(id)){
string s = id.substr(0,1);
const char* s = id.substr(1,7);
}*/
//extract name
i = line.find(",");
string name = trim(line.substr(0, i)); // extract name
s.setName(name); //set student name
line = line.substr(i+1); //remove name from line
//extract classcode
i = line.find(",");
string classcode = trim(line.substr(0, i));
s.setClasscode(classcode);
line = line.substr(i+1);
if(isValidClasscode(classcode)){
string module = classcode.substr(0,6);
s.setModule(module);
string status = classcode.substr(7,9);
char *ft ="FT";
if(status.compare(ft)==0){
s.setFulltime(true);
}else{
s.setFulltime(false);
}
string classnumber = classcode.substr(9,11);
s.setClassnumber(classnumber);
}
//to use a for-loop for attendance
for (int j =0; j < 10 ; j++){
i = line.find(",");
string attendance = trim(line.substr(0, i));
s.setAttendance(j, attendance);
line = line.substr(i+1);
}
//to extreact test1
i = line.find(",");
int test1 = stoi(line.substr(0, i));
s.setTest1(test1);
line = line.substr(i+1);
//to extreact test2
i = line.find(",");
int test2 = stoi(line.substr(0, i));
s.setTest2(test2);
line = line.substr(i+1);
//to extreact tutorial
i = line.find(",");
int tutorial = stoi(line.substr(0, i));
s.setTutorial(tutorial);
line = line.substr(i+1);
//to extreact exam
i = line.find(",");
int exam = stoi(line.substr(0, i));
s.setExam(exam);
line = line.substr(i+1);
//need to do validationfirst
//to only add the valid student
if(isDuplicateStudentRecord(students, s) == false){
students.push_back(s); //add student to list
} else {
duplicates.push_back(s);
}
//cout<< line << endl;
}
inputFile.close();
}else{
cout << "Invalid file" << endl;
}
return students;
}
void liststudentRecord(vector<Student> list){
int numberStudents = list.size();
if (numberStudents > 0){
for (int i = 0; i < numberStudents; i++){
Student s = list.at(i);
cout << s.toString() << endl;
}
}else{
cout << "Empty list" <<endl;
}
}
bool test1Ascending(Student s1, Student s2){
return s1.getTest1() < s2.getTest1();
}
bool test1Descending(Student s1, Student s2){
return s1.getTest1() < s2.getTest1();
}
bool finalScoreAscending(Student s1, Student s2){
return s1.getFinalScore() < s2.getFinalScore();
}
bool isvalidStudentid(string s){
int i;
if(s.length() !=8){
return false;} //first check th length is 8
if (s.at(i) != 'S') {
return false;} //start with s
for (i =1 ; i<8; i++){ //7 digit
if(isdigit(s.at(i))==0){
return false;
}
}
return false;
}
bool isValidClasscode(string s){
int i;
if(s.length() !=11){
return false;} //first check th length is 8
for (i =0 ; i<3; i++){ // first 3 char are letters
if(isalpha(s.at(i))==0){
return false;}
}
for (i =3 ; i<6; i++){ //next 3 char are number
if(isdigit(s.at(i)) == 0){
return false;}
}
if (s.at(6) != '-') {
return false;} //has a hyphen
if (s.at(7) != 'F' && s.at(7) != 'P') {
return false;}
if (s.at(8) != 'T' ) {
return false;} // T
if (isdigit(s.at(9)) == 0 || isdigit(s.at(10)) == 0){
return false;}
return false;
}
vector<Module> readFile2(){
vector<Module>modules;
string filename;
ifstream inputFile;
cout << "Enter filename: ";
cin >> filename;
inputFile.open(filename, ios::in);//open file, read only
if(inputFile.is_open()){
while (inputFile.good()){
string line;
getline(inputFile, line);
Module m; //create a new student
//extract name
int i = line.find(",");
string modulecode = trim(line.substr(0, i));// extract id
m.setModulecode(modulecode); // set student id
//cout << id<< endl;
line = line.substr(i+1);// remove id form line
//cout << line << endl;
//extract id
i = line.find(",");
string modulename = trim(line.substr(0, i)); // extract name
m.setModulename(modulename); //set student name
line = line.substr(i+1); //remove name from line
//to extreact test1
i = line.find(",");
int test1 = stoi(line.substr(0, i));
m.setTest1(test1);
line = line.substr(i+1);
//to extreact test2
i = line.find(",");
int test2 = stoi(line.substr(0, i));
m.setTest2(test2);
line = line.substr(i+1);
//to extreact tutorial
i = line.find(",");
int tutorial = stoi(line.substr(0, i));
m.setTutorial(tutorial);
line = line.substr(i+1);
//to extreact exam
i = line.find(",");
int exam = stoi(line.substr(0, i));
m.setExam(exam);
line = line.substr(i+1);
modules.push_back(m); //add student to list
//cout<< line << endl;
}
inputFile.close();
}else{
cout << "Invalid file" << endl;
}
return modules;
}
void listmoduleRecord(vector<Module> list){
int numberModules = list.size();
if (numberModules > 0){
for (int i = 0; i < list.size(); i++){
Module m = list.at(i);
cout << m.toString() << endl;
}
}else{
cout << "Empty list" <<endl;
}
}
vector<Student> getStudentByModule(vector<Student> students,string code){
vector<Student> list;
for (int i=0; i< students.size(); i++){
Student s = students.at(i);
if (code.compare(current_module) == 0){
list.push_back(s);
}
}
return list;
}
bool isDuplicateStudentRecord(vector<Student> student, Student s){
string id = student.getId();
string name = student.getName();
string module = student.getModule();
for(int i=0; i< student.size(); i++){
Student s = student.at(i);
string current_id = s.getId();
string current_name = s.getName();
string current_module = s.getModule();
if(id.compare)(current_id) == 0 &&
name.compare(current_name) == 0 &&
(module.compare(current_module) == 0);{
return true;
}
}
return false;
}
//http://stackoverflow.com/a/6500499/3839235
string trim(string s){
s.erase(0, s.find_first_not_of(' ')); //prefixing spaces
s.erase(s.find_last_not_of(' ')+1); //surfixing spaces
return s;
}
Module.cpp
#include "Module.h"
#include <sstream>
using namespace std;
Module::Module(void){
}
string Module:: getModulecode(){
return modulecode;
}
void Module::setModulecode(string modulecode){
this->modulecode = modulecode;
}
string Module:: getModulename(){
return modulename;
}
void Module:: setModulename(string modulename){
this->modulename = modulename;
}
int Module:: getTest1(){
return test1;
}
void Module:: setTest1(int value1){
test1 = value1;
}
int Module:: getTest2(){
return test2;
}
void Module:: setTest2(int value2){
test2 = value2;
}
int Module:: getTutorial(){
return tutorial;
}
void Module:: setTutorial(int value3){
tutorial = value3;
}
int Module:: getExam(){
return exam;
}
void Module:: setExam(int value4){
exam = value4;
}
string Module::toString(){
stringstream ss;
ss<<"Module Code: " << modulecode<< "-"<< modulename <<endl;
ss << "Test 1: " << getTest1Percentage() << "%" << endl;
ss << "Test 2: " << getTest2Percentage() << "%" << endl;
ss << "Tutorial: " << getTutorialPercentage() << "%" << endl;
ss << "Exam: " << getExamPercentage() << "%" << endl;
//ss<< "Score percentage: " << getScorePercentage() << "%"<< endl;
ss<< "Final score: " << getFinalScore() << "%" <<endl;
return ss.str();
}
int Module::getTest1Percentage(){
int result;
result = (getTest1() * 100) / 100;
return result;
}
int Module::getTest2Percentage(){
int result;
result = (getTest2() * 100) / 100;
return result;
}
int Module::getTutorialPercentage(){
int result;
result = (getTutorial() * 100) / 100;
return result;
}
int Module::getExamPercentage(){
int result;
result = (getExam() * 100) / 100;
return result;
}
int Module::getFinalScore(){
int final = getTest1Percentage()+getTest2Percentage()+getTutorialPercentage()+getExamPercentage();
return final;
}
/*int Module::getFinalScore(){
int final = test1+test2+tutorial+exam;
return final;
}*/
Student.cpp
#include "Student.h"
#include <sstream>
using namespace std;
Student::Student(void){
}
string Student:: getId(){
return id;
}
void Student:: setId(string newid){
id = newid;
}
string Student:: getName(){
return name;
}
void Student:: setName(string name){
this->name = name;
}
string Student:: getClasscode(){
return classcode;
}
void Student::setClasscode(string classcode){
this->classcode = classcode;
}
string Student:: getAttendance(int i){
return attendance[i];
}
void Student:: setAttendance(int i, string value){
attendance[i] = value;
}
int Student:: getTest1(){
return test1;
}
void Student:: setTest1(int value1){
test1 = value1;
}
int Student:: getTest2(){
return test2;
}
void Student:: setTest2(int value2){
test2 = value2;
}
int Student:: getTutorial(){
return tutorial;
}
void Student:: setTutorial(int value3){
tutorial = value3;
}
int Student:: getExam(){
return exam;
}
void Student:: setExam(int value4){
exam = value4;
}
string Student::toString(){
stringstream ss;
ss << name << "(" <<id << ")" << endl;
ss << classcode << endl;
for(int i=0; i<10; i++){
ss << getAttendance(i) << " ";
}
ss << endl;
ss << "Test 1: " << test1 << endl;
ss << "Test 2: " << test2 << endl;
ss << "Tutorial: " << tutorial << endl;
ss << "Exam: " << exam << endl;
ss<< "Attendance percentage: " << getAttendancePercentage() << "%"<< endl;
ss<< "Final score: " << getFinalScore() <<endl;
return ss.str();
}
double Student::getAttendancePercentage(){
int n =0 ; // n will be the total number of "yes"
for (int i=1; i<10; i++){
string s = getAttendance(i);
char*yes = "yes";
if(s.compare(yes)== 0){
n++;
}
}
double percentage = n/10.0 * 100; //avoud interger division
return percentage;
}
int Student::getFinalScore(){
int final = test1+test2+tutorial+exam;
return final;
}
/*string Student::getModule() { return module; }
void Student::setModule(string m) { module = m;}
bool Student::getFulltime() { return fulltime; }
void Student::setFulltime(bool v) { fulltime = v;}
string Student::getClassnumber() { return classnumber; }
void Student::setClassnumber(string v) { classnumber = v;} */
string Student:: getModule(){
return module;
}
void Student:: setModule(string m){
module =m;
}
bool Student:: getFulltime(){
return fulltime;
}
void Student:: setFulltime(bool v){
fulltime = v;
}
string Student:: getClassnumber(){
return classnumber;
}
void Student:: setClassnumber(string c){
classnumber = c;
}
Module.h
#ifndef MODULE_H
#define MODULE_H
#include <string>
using namespace std;
class Module {
public:
Module();
string getModulecode();
void setModulecode(string);
string getModulename();
void setModulename(string);
int getTest1();
void setTest1(int);
int getTest2();
void setTest2(int);
int getTutorial();
void setTutorial(int);
int getExam();
void setExam(int);
string toString(); //to displayed info
int getTest1Percentage();
int getTest2Percentage();
int getTutorialPercentage();
int getExamPercentage();
int getFinalScore();
private:
string modulecode;
string modulename;
int test1;
int test2;
int tutorial;
int exam;
};
#endif //MODULE_H
Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;
class Student {
//methods
public:
Student();
string getId();
string getName();
string getClasscode();
string getAttendance(int);
int getTest1();
int getTest2();
int getTutorial();
int getExam();
void setId(string);
void setName(string);
void setClasscode(string);
void setAttendance(int, string);
void setTest1(int);
void setTest2(int);
void setTutorial(int);
void setExam(int);
string toString(); //to displayed info
double getAttendancePercentage();
int getFinalScore();
string getModule();
void setModule(string);
bool getFulltime();
void setFulltime(bool);
string getClassnumber();
void setClassnumber(string);
//to create the get and set methods for all the attributes
//attribute
private:
string id;
string name;
string classcode;
string module;
bool fulltime;
string classnumber;
string attendance[10];
int test1;
int test2;
int tutorial;
int exam;
};
#endif //STUDENT_Hs