Ans)
Step1:First create a class elements to store values of each of elements
Step2:include string stream to get values from each line from text file of respective element
Step3:declare array of Elements type of size 10 to store 10 records
Step4:Open file Elements.txt using ifstream. get line by line in stringstream
Step5: read every ement from string stream
Step6:display every element and its respective information
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
class Elements
{
public:
string atomicnum;
string symbol;
string name;
float atoweight;
float density;
int year;
};
int main() { // main function
Elements elements[10];
string filename="Elements.txt";
// cout << "Enter File name:";
// cin >> filename;
int j = 0;
ifstream file;
file.open((filename.c_str())); //if file does not exist
if (!file)
{
cout << "Cannot Open file";
return 0;
}
int count = 0;
string out,temp;
out = "";
while (!file.eof()) //reading all elements from file
{
getline(file,temp);
if(j<=9){
stringstream ss(temp);
ss>>elements[j].atomicnum;
ss>>elements[j].symbol;
ss>>elements[j].name;
ss>>elements[j].atoweight;
ss>>elements[j].density;
ss>>elements[j].year;
ss.clear();
ss.seekg(0,ios::beg);
j++;
}
}
file.close();
for (int j = 0; j < 10; j++)
{
cout << elements[j].atomicnum << " " << elements[j].symbol << " "
<< elements[j].name << " " << elements[j].atoweight << " "
<< elements[j].density << " "
<< elements[j].year<<"\n";
}
return 0;
}
You will need to copy/paste the files Elements.txt and Diamonds.txt from Module 13 folder into your...