Question

You will need to copy/paste the files Elements.txt and Diamonds.txt from Module 13 folder into your CodeBlocks project folder
1. 2. 3. 4. 5. н He Li Ве B 7. 8. N O 10. Ne 11. Mg Al 12. 13. 14. 15. 16. 17. 18. P Hydrogen 1.0079 0.09 1776 Helium 4.0026
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;

}

Add a comment
Know the answer?
Add Answer to:
You will need to copy/paste the files Elements.txt and Diamonds.txt from Module 13 folder into your...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT