Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <map>
- using namespace std;
- class StudentInfo
- {
- private:
- string fileName;
- int const static size = 5;
- string lastname;
- double grades[size];
- map<string, double> students;
- double AvarageGrade(double const arr[]); // liczenie sredniej
- public:
- StudentInfo(string name);
- void DisplayResults(); // wyswietlanie wynikow
- map<string, double> LoadStudents(); // obsluga pliku, zapisywanie do kolekcji
- };
- StudentInfo::StudentInfo(string name) {
- fileName = name;
- LoadStudents();
- };
- map<string, double> StudentInfo::LoadStudents() {
- ifstream file;
- file.open(fileName + ".txt", ios::in);
- if(!file.good()) {
- string wyjatek = "Wystapil problem z otwarciem pliku.";
- throw wyjatek;
- }
- while( file >> lastname >> grades[0] >> grades[1] >> grades[2] >> grades[3] >> grades[4] )
- {
- double k = AvarageGrade(grades);
- students[lastname] = k;
- }
- return students;
- }
- void StudentInfo::DisplayResults() {
- for( const auto &student : students){
- cout<<student.first << " ma srednia " << student.second<<endl;
- }
- }
- double StudentInfo::AvarageGrade(double const grades[]) {
- double tmp;
- double avarage;
- string wyjatek = "Zla ocena, poza zakrezem 0 - 6.";
- for(int i = 0; i < size; i++){
- if(grades[i] > 6 || grades[i]<0)
- throw wyjatek;
- tmp += grades[i];
- }
- avarage = tmp/size;
- return avarage;
- }
- int main() {
- StudentInfo foo("studenci");
- try {
- foo.DisplayResults();
- }
- catch(string e) {
- cout<< e;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment