#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <list>
struct Pat
{
std::string name;
int age = 0;
//constructor
Pat(std::string pname, int page): name(pname), age(page)
{
}
};
int main()
{
std::ifstream inputFile("data.txt");
std::string line;
std::list<Pat> patList;
//check if file opened correctly
if(inputFile)
{
//create variable for holding name
std::string tempName;
//create variable for holding age
int tempAge;
//read line by line from the file
while(std::getline(inputFile, line))
{
std::istringstream ss(line);
//read the name
ss >> tempName;
//read the age
ss >> tempAge;
patList.emplace_back(tempName, tempAge);
}
}
else
{
std::cout << "Input file cannot be opened" << std::endl;
}
//lets check if our patList contains all the objects correctly
for(const Pat &elem: patList)
{
std::cout<<"Name: "<<elem.name << " Age: "<< elem.age<<std::endl;
}
return 0;
}
Anoop 23
Rana 43
Richard 35
Singh 65