Create a structure Student containing fields for Roll No., Name, Class, Year and Total Marks. Create 10 students and store them in a file.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Student {
int rollNo;
string name;
string cls;
int year;
int totMarks;
};
int main() {
Student students[10] { {12345, "John Dow", "CSE123", 2020, 234},
{12355, "Adam Smith", "EC222", 2020, 233},
{22334, "Caty Sark", "ENG124", 2021, 240},
{99881, "Alice Guibson", "PHY32", 2020, 255},
{89765, "Den Brown", "CSE123", 2019, 123},
{89123, "Bob Gray", "ENG124", 2020, 223},
{12233, "Mary Cray", "BIO222", 2020, 250},
{12244, "John Snow", "ECO123", 2021, 211},
{11223, "Henry Ford", "ENG124", 2019, 256},
{11334, "Bill Gates", "CSE123", 2918, 211} };
ofstream ofs("students.txt");
for (int i=0; i<10; i++) {
ofs << students[i].rollNo << endl;
ofs << students[i].name << endl;
ofs << students[i].cls << endl;
ofs << students[i].cls << endl;
ofs << students[i].year << endl;
ofs << students[i].totMarks << endl;
}
ofs.close();
}
Comments
Leave a comment