Week 7 C++
Week 7 C++
#include <iostream>
#include <fstream>
using namespace std;
class Student {
private:
char name[10];
int age;
float height;
public:
void getdata() {
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
cout << "Enter height: ";
cin >> height;
ofstream f1;
f1.open("data.txt", ios::app);
f1 << name << " " << age << " " << height << endl;
f1.close(); }
void showdata() {
ifstream f2;
f2.open("data.txt");
while (f2 >> name >> age >> height) {
cout << "Name: " << name << ", Age: " << age << ", Height: " << height <<
endl; }
f2.close();
}};
int main() {
Student students[3];
for (int i = 0; i < 3; i++) {
cout << "Enter details for student " << i + 1 << ":" << endl;
students[i].getdata(); }
cout << "\nDisplaying student details" << endl;
students[0].showdata();
return 0;}
OUTPUT:
Enter details for student 1:
Enter name: sita
Enter age: 18
Enter height: 123.32
Enter details for student 2:
Enter name: ram
Enter age: 19
Enter height: 125.30
Enter details for student 3:
Enter name: riya
Enter age: 20
Enter height: 145.28