Created
January 5, 2014 22:45
-
-
Save Experiment5X/8275076 to your computer and use it in GitHub Desktop.
Just a simple project I made to demonstrate some concepts in C++. Here's the video where I teach this project: http://www.youtube.com/watch?v=NIoEVxe-rpk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// StudentInformationAnalyzer | |
// | |
// Created by Adam Spindler on 1/4/14. | |
// Copyright (c) 2014 Adam Spindler. All rights reserved. | |
// | |
#include <iostream> | |
#include <exception> | |
#include <string> | |
#include "Student.h" | |
#include "StudentList.h" | |
using namespace std; | |
void printStudent(Student student) | |
{ | |
cout << "Name: " << student.getName() << endl; | |
cout << "ID: " << student.getId() << endl; | |
cout << "GPA: " << student.getGPA() << endl; | |
cout << endl; | |
} | |
int main(int argc, const char * argv[]) | |
{ | |
try | |
{ | |
if (argc != 2) | |
{ | |
cout << "Usage: StudentInformationAnalyzer PATH_TO_STUDENT_FILE" << endl; | |
return -1; | |
} | |
string path(argv[1]); | |
StudentList list(path); | |
cout << "Valedictorian: " << endl << endl; | |
printStudent(list.getValedictorian()); | |
cout << endl << "Honor Role Students: " << endl << endl; | |
for (Student s : list.getHonorRollStudents()) | |
printStudent(s); | |
cout << endl << "Failing Students: " << endl << endl; | |
for (Student s : list.getFailingStudents()) | |
printStudent(s); | |
} | |
catch (exception &e) | |
{ | |
cout << "An exception was thrown." << endl; | |
cout << "\t" << e.what() << endl; | |
return -1; | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Student.cpp | |
// StudentInformationAnalyzer | |
// | |
// Created by Adam Spindler on 1/4/14. | |
// Copyright (c) 2014 Adam Spindler. All rights reserved. | |
// | |
#include "Student.h" | |
using namespace std; | |
Student::Student(string studentInformation) | |
{ | |
stringstream studentStream(studentInformation); | |
studentStream >> firstName; | |
studentStream >> lastName; | |
studentStream >> id; | |
studentStream >> gpa; | |
} | |
string Student::getFirstName() | |
{ | |
return firstName; | |
} | |
string Student::getLastName() | |
{ | |
return lastName; | |
} | |
string Student::getName() | |
{ | |
return firstName + " " + lastName; | |
} | |
int Student::getId() | |
{ | |
return id; | |
} | |
double Student::getGPA() | |
{ | |
return gpa; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Student.h | |
// StudentInformationAnalyzer | |
// | |
// Created by Adam Spindler on 1/4/14. | |
// Copyright (c) 2014 Adam Spindler. All rights reserved. | |
// | |
#ifndef __StudentInformationAnalyzer__Student__ | |
#define __StudentInformationAnalyzer__Student__ | |
#include <string> | |
#include <sstream> | |
class Student | |
{ | |
public: | |
Student(std::string studentInformation); | |
std::string getFirstName(); | |
std::string getLastName(); | |
std::string getName(); | |
int getId(); | |
double getGPA(); | |
private: | |
std::string firstName; | |
std::string lastName; | |
int id; | |
double gpa; | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// StudentList.cpp | |
// StudentInformationAnalyzer | |
// | |
// Created by Adam Spindler on 1/4/14. | |
// Copyright (c) 2014 Adam Spindler. All rights reserved. | |
// | |
#include "StudentList.h" | |
#define FAILING_GRADE 65 | |
#define HONORROLE_GRADE 90 | |
using namespace std; | |
StudentList::StudentList(string filePath) | |
{ | |
ifstream studentFile(filePath); | |
string lineContents; | |
while (!studentFile.eof()) | |
{ | |
getline(studentFile, lineContents); | |
Student s(lineContents); | |
students.push_back(s); | |
} | |
studentFile.close(); | |
sort(students.begin(), students.end(), [](Student &s1, Student &s2) | |
{ | |
return s1.getGPA() > s2.getGPA(); | |
}); | |
} | |
Student StudentList::getValedictorian() | |
{ | |
if (students.size() == 0 || students.at(0).getGPA() < FAILING_GRADE) | |
throw NoValedictorianException(); | |
return students.at(0); | |
} | |
vector<Student> StudentList::getHonorRollStudents() | |
{ | |
vector<Student> toReturn; | |
for (Student s : students) | |
{ | |
if (s.getGPA() < HONORROLE_GRADE) | |
break; | |
else | |
toReturn.push_back(s); | |
} | |
return toReturn; | |
} | |
vector<Student> StudentList::getFailingStudents() | |
{ | |
vector<Student> toReturn; | |
for (vector<Student>::reverse_iterator iter = students.rbegin(); iter < students.rend(); iter++) | |
{ | |
if (iter->getGPA() >= FAILING_GRADE) | |
break; | |
else | |
toReturn.push_back(*iter); | |
} | |
return toReturn; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// StudentList.h | |
// StudentInformationAnalyzer | |
// | |
// Created by Adam Spindler on 1/4/14. | |
// Copyright (c) 2014 Adam Spindler. All rights reserved. | |
// | |
#ifndef __StudentInformationAnalyzer__StudentList__ | |
#define __StudentInformationAnalyzer__StudentList__ | |
#include <string> | |
#include <vector> | |
#include <fstream> | |
#include <algorithm> | |
#include <exception> | |
#include "Student.h" | |
class StudentList | |
{ | |
public: | |
StudentList(std::string filePath); | |
Student getValedictorian(); | |
std::vector<Student> getHonorRollStudents(); | |
std::vector<Student> getFailingStudents(); | |
private: | |
std::vector<Student> students; | |
}; | |
class NoValedictorianException : public std::exception | |
{ | |
public: | |
const char *what() const throw() | |
{ | |
return "StudentList: No Valedictorian"; | |
} | |
}; | |
#endif |
I think you should check while reading file that the file is empty or not.
This will not create garbage.
Because when i give input an empty file it pushes garbage value into vector and which in turn increases size by 1. So, i think ifixed it.
here is the code
StudentList.cpp
#include "StudentList.h"
#define FAILING_GRADE 65
#define HONORROLE_GRADE 90
#include<iostream>
using namespace std;
StudentList::StudentList(string filePath)
{
ifstream studentFile(filePath);
string lineContents;
if(studentFile.peek() == '\0'){
throw NoValedictorianException();
}else{
while (!studentFile.eof())
{
getline(studentFile, lineContents);
Student s(lineContents);
students.push_back(s);
}
}
studentFile.close();
sort(students.begin(), students.end(), [](Student &s1, Student &s2)
{
return s1.getGPA() > s2.getGPA();
});
}
Student StudentList::getValedictorian()
{
if (students.at(0).getGPA() < FAILING_GRADE){
throw NoValedictorianException();
}
return students.at(0);
}
vector<Student> StudentList::getHonorRollStudents()
{
vector<Student> toReturn;
for (Student s : students)
{
if (s.getGPA() < HONORROLE_GRADE)
break;
else
toReturn.push_back(s);
}
return toReturn;
}
vector<Student> StudentList::getFailingStudents()
{
vector<Student> toReturn;
for (vector<Student>::reverse_iterator iter = students.rbegin(); iter < students.rend(); iter++)
{
if (iter->getGPA() >= FAILING_GRADE)
break;
else
toReturn.push_back(*iter);
}
return toReturn;
}
Also you should remove #include "Student.h"
from main.cpp file.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Adam, first of all thanks for your tutorial, I found it on youtube and it really helped me. Here is my question, I have programmed the first part (until we print all the students data by console). I have copied your code but there is something that doesn´t work in the same way. When I print the students data by console, my program creates 2 member for each student, one with the data and another one empty at the end.For example if I just have one line in my txt file it shows me this:
Name: Michael Scott
ID: 5423
GPA: 76.9
Name:
ID
GPA
Do you know why I could be happening?? Thank from Spain!