Thuta Learning
ရှာဖွေရန်
ProjectsProgrammingbeginner

Mini Project: Student Manager - Part 3

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Mini Project: Student Manager - Part 3 ကို လက်တွေ့ project ထဲမှာ အသုံးချတတ်မယ်
  • ကိုယ်တိုင် code ရေးပြီး run ကြည့်တတ်မယ်
  • Project တစ်ခုလုံးကို အဆင့်လိုက် ဆောက်တတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

Part 3 က project ရဲ့ နောက်ဆုံးအဆင့်ဖြစ်ပြီး, program ကို production-ish quality နီးပါးဖြစ်အောင် polish လုပ်ပါမယ်။ ပထမဆုံး, marks ဂဏန်းမဟုတ်တဲ့ input ရိုက်ထည့်လိုက်ရင် crash မဖြစ်အောင် try/catch နဲ့ exception handling ထည့်ပါမယ်။ ဒုတိယ, algorithm library ရဲ့ sort() ကို သုံးပြီး student list ကို marks အလိုက် စီပါမယ်။ နောက်ဆုံး, fstream သုံးပြီး student data ကို students.txt file ထဲကို save လုပ်ပြီး, program ပြန်ဖွင့်တဲ့အခါ file ထဲက data ကို ပြန်ဖတ်တင်နိုင်အောင် (persistence) စီစဉ်ပါမယ်။ ဒီနေရာက file handling topic ကို practical project ထဲမှာ တကယ်အသုံးချတဲ့ ကဏ္ဍဖြစ်ပါတယ်။

လက်တွေ့ ဆောက်ကြည့်မယ်

addStudent() ထဲမှာ marks ကို cin >> marks; ဖတ်ပြီးနောက် cin.fail() ဖြစ်ရင် exception throw လုပ်ပြီး try/catch block နဲ့ error message ပြပါ။ sortByMarks(vector<Student>& students) function တစ်ခု ဖန်တီးပြီး std::sort() ကို lambda comparator [](const Student& a, const Student& b){ return a.getMarks() > b.getMarks(); } နဲ့ သုံးပါ။ saveToFile(const vector<Student>& students) function မှာ ofstream ဖွင့်ပြီး student data တစ်ယောက်ချင်းစီကို line တစ်ကြောင်းစီ text file ထဲရေးပါ။ loadFromFile(vector<Student>& students) function မှာ ifstream ဖွင့်ပြီး file ရှိရင် line by line ဖတ်ပြီး vector ထဲ ပြန်ဖြည့်ပါ - program စလိုက်တာနဲ့ main() အစမှာ ဒီ function ကို ခေါ်ပါ။ Menu ထဲမှာ Sort by Marks နဲ့ Save & Exit option တွေ ထပ်ထည့်ပါ။

Code နမူနာ

cpp
#include <fstream>
#include <algorithm>
#include <stdexcept>

void sortByMarks(vector<Student>& students) {
    sort(students.begin(), students.end(),
        [](const Student& a, const Student& b) {
            return a.getMarks() > b.getMarks();
        });
    cout << "Sorted by marks (highest first).\n";
}

void saveToFile(const vector<Student>& students) {
    ofstream fout("students.txt");
    for (const Student& s : students) {
        fout << s.getRoll() << " " << s.getName() << " " << s.getMarks() << "\n";
    }
    fout.close();
    cout << "Saved to students.txt\n";
}

void loadFromFile(vector<Student>& students) {
    ifstream fin("students.txt");
    if (!fin) return;

    int roll;
    string name;
    double marks;
    while (fin >> roll >> name >> marks) {
        students.push_back(Student(name, roll, marks));
    }
    fin.close();
}

void addStudentSafe(vector<Student>& students) {
    try {
        string name;
        int roll;
        double marks;
        cout << "Enter name: ";
        cin >> name;
        cout << "Enter roll: ";
        cin >> roll;
        cout << "Enter marks: ";
        cin >> marks;

        if (cin.fail()) {
            throw runtime_error("Invalid marks input!");
        }
        students.push_back(Student(name, roll, marks));
        cout << "Student added!\n";
    } catch (const runtime_error& e) {
        cout << "Error: " << e.what() << "\n";
        cin.clear();
    }
}
You should see
Program ကို run ပြီး Save & Exit ရွေးလိုက်ရင် students.txt file ဖန်တီးပြီး data ရေးထည့်ကာ, program ကို ပြန်ဖွင့်လိုက်ရင် အရင် save ထားတဲ့ student list တွေ ပြန်ပေါ်လာမှာဖြစ်ပါတယ်။

၅ မိနစ် စမ်းကြည့်

Program ကို run ကြည့်ပြီး student 3 ယောက် ထည့်ပါ - Sort by Marks နဲ့ စမ်းစီပြီး, Save & Exit ရွေးပြီး program ကို ပြန်ဖွင့်ကာ data ပြန်ပေါ်လာမလားစစ်ဆေးကြည့်ပါ။

သတိလေးတစ်ချက်

cin.fail() ဖြစ်ပြီးရင် cin.clear() ကိုသာမက cin.ignore() ကိုပါ သုံးပြီး invalid input တွေကို buffer ထဲက ရှင်းပေးဖို့ လိုပါတယ် - မရှင်းရင် infinite loop ဖြစ်တတ်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • cin.fail() ဖြစ်တဲ့အခါ cin.clear() နဲ့ stream state ကို reset မလုပ်တတ်ပြီး, ပြီးတဲ့ input operation တွေအားလုံး fail ဆက်ဖြစ်နေတာ
  • file save function ကို program ရဲ့ menu loop အထဲမှာပဲ ခေါ်ပြီး, program crash ဖြစ်ရင် (သို့) user Ctrl+C နဲ့ ပိတ်ရင် data လုံးဝ မ save ဘဲကျန်ခဲ့တာ

အခု ကိုယ်တိုင် စမ်းကြည့်

Program ကို run ကြည့်ပြီး student 3 ယောက် ထည့်ပါ - Sort by Marks နဲ့ စမ်းစီပြီး, Save & Exit ရွေးပြီး program ကို ပြန်ဖွင့်ကာ data ပြန်ပေါ်လာမလားစစ်ဆေးကြည့်ပါ။

You'll know it worked when: Program ကို run ပြီး Save & Exit ရွေးလိုက်ရင် students.txt file ဖန်တီးပြီး data ရေးထည့်ကာ, program ကို ပြန်ဖွင့်လိုက်ရင် အရင် save ထားတဲ့ student list တွေ ပြန်ပေါ်လာမှာဖြစ်ပါတယ်။

Mini Project: Student Manager - Part 3 | Thuta Learning