lab 10: init

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-12-26 18:38:55 +05:30
parent 42902fa122
commit 9e7cffdc32
9 changed files with 298 additions and 2 deletions

67
lab10/30.cpp Normal file
View File

@@ -0,0 +1,67 @@
#include <fstream>
#include <iostream>
#include <ostream>
using namespace std;
const string FILE_PATH = "./30.out";
class StudentInfo {
char name[30];
uint age;
char sex;
uint height;
public:
void read();
void write();
};
void StudentInfo::read() {
cout << "Name: ";
cin >> name;
cout << "Age: ";
cin >> age;
cout << "Sex (m/f): ";
cin >> sex;
cout << "Height: ";
cin >> height;
cout << "[LOG] Data read\n";
}
void StudentInfo::write() {
ofstream file(FILE_PATH);
file.close();
file.open(FILE_PATH, ios_base::app);
file << "Name: " << name << endl
<< "Age: " << age << endl
<< "Sex: " << (sex == 'm' ? "Male" : "Female") << endl
<< "Height: " << height << "cm";
cout << "[LOG] Data written\n";
file.close();
}
int main() {
StudentInfo foo;
string line;
foo.read();
foo.write();
ifstream file(FILE_PATH);
if (!file.is_open())
exit(1);
cout << "File contents: \n";
while (getline(file, line)) {
cout << line;
if (file.peek() != EOF) {
cout << endl;
}
}
return 0;
}