fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. // ตัวแปรสำหรับเก็บข้อมูล
  9. string name, studentID, faculty, email, phone;
  10. int age;
  11.  
  12. // รับข้อมูลจากผู้ใช้
  13. cout << "กรุณากรอกข้อมูลของคุณ\n";
  14. cout << "ชื่อ-นามสกุล: ";
  15. getline(cin, name);
  16.  
  17. cout << "อายุ: ";
  18. cin >> age;
  19. cin.ignore(); // ล้าง buffer หลังจากรับค่า int
  20.  
  21. cout << "รหัสนักศึกษา: ";
  22. getline(cin, studentID);
  23.  
  24. cout << "คณะ/สาขา: ";
  25. getline(cin, faculty);
  26.  
  27. cout << "อีเมล: ";
  28. getline(cin, email);
  29.  
  30. cout << "โทรศัพท์: ";
  31. getline(cin, phone);
  32.  
  33. // เปิดไฟล์เพื่อบันทึกข้อมูล
  34. ofstream file("profile.txt");
  35.  
  36. if (file.is_open()) {
  37. file << "------ ประวัติส่วนตัว ------\n";
  38. file << "ชื่อ-นามสกุล: " << name << "\n";
  39. file << "อายุ: " << age << "\n";
  40. file << "รหัสนักศึกษา: " << studentID << "\n";
  41. file << "คณะ/สาขา: " << faculty << "\n";
  42. file << "อีเมล: " << email << "\n";
  43. file << "โทรศัพท์: " << phone << "\n";
  44. file.close();
  45. cout << "บันทึกข้อมูลเรียบร้อย! (profile.txt)\n";
  46. } else {
  47. cout << "ไม่สามารถเปิดไฟล์ได้!\n";
  48. }
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0.03s 25864KB
stdin
Standard input is empty
stdout
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    // ตัวแปรสำหรับเก็บข้อมูล
    string name, studentID, faculty, email, phone;
    int age;

    // รับข้อมูลจากผู้ใช้
    cout << "กรุณากรอกข้อมูลของคุณ\n";
    cout << "ชื่อ-นามสกุล: ";
    getline(cin, name);
    
    cout << "อายุ: ";
    cin >> age;
    cin.ignore();  // ล้าง buffer หลังจากรับค่า int

    cout << "รหัสนักศึกษา: ";
    getline(cin, studentID);

    cout << "คณะ/สาขา: ";
    getline(cin, faculty);

    cout << "อีเมล: ";
    getline(cin, email);

    cout << "โทรศัพท์: ";
    getline(cin, phone);

    // เปิดไฟล์เพื่อบันทึกข้อมูล
    ofstream file("profile.txt");

    if (file.is_open()) {
        file << "------ ประวัติส่วนตัว ------\n";
        file << "ชื่อ-นามสกุล: " << name << "\n";
        file << "อายุ: " << age << "\n";
        file << "รหัสนักศึกษา: " << studentID << "\n";
        file << "คณะ/สาขา: " << faculty << "\n";
        file << "อีเมล: " << email << "\n";
        file << "โทรศัพท์: " << phone << "\n";
        file.close();
        cout << "บันทึกข้อมูลเรียบร้อย! (profile.txt)\n";
    } else {
        cout << "ไม่สามารถเปิดไฟล์ได้!\n";
    }

    return 0;
}