Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3495540
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:08:20+00:00 2026-05-18T12:08:20+00:00

Hi the title says everything really. I have a method which reads in a

  • 0

Hi the title says everything really. I have a method which reads in a file holding data on the composition of materials (like polythene, or the natural composition of lead) each of these materials is held in a different object and each has a different composition. The make method is called sequentially, as the once the materials which make up the physical system are obtained from another file it is the natural next step. What is written into the list is o/p to a file, then what is actually in the list is op to the end of that file. How do I avoid spillage?

void material::make(char* filename)   
{           
    ifstream fin(filename) ;    
ofstream filenameOUT ;
string outfile = (string)filename + "OUT.txt" ;
filenameOUT.open(outfile.c_str()) ;
    string ZStr, propStr, isotope ;
    vector<float> component(2,0) ;

    getline(fin, propStr, '\n') ;   //store first entry in file as a str (prop is used to save mem allocation)
    lamda = atof(propStr.c_str()) ;   //then convert to float so calcs can be done
filenameOUT<<"lamda: "<<lamda<<endl;

    while(!fin.eof())
    {
        getline(fin, isotope, ' ') ;  //get the element name
        getline(fin, ZStr, '\t') ;   //get the Z's and abunancies from the file.
        getline(fin, propStr) ;
        component[0] = atof(ZStr.c_str()) ;
        component[1] = atof(propStr.c_str()) ;
filenameOUT<<"isotope: "<<isotope<<" isotope Z: "<<component[0]<<" proportional amount: "<<component[1]<<endl;
        composition.push_back(component) ;
        elements.push_back(isotope) ;
    }
filenameOUT<<filename<<" is loaded"<<endl;

    for(c=composition.begin();c!=composition.end();c++)
    {
filenameOUT<<(*c)[0]<<" : "<<(*c)[1]<<" is loaded"<<endl;
    }
}

for example, the input file for polyethylene:

.335657
carbon 12 .33333
hydrogen 1 .66667

produces this (which contains isotopes of lead, copper, boron, hydrogen three times and carbon once):

lamda: 0.335657
isotope: carbon isotope Z: 12 proportional amount: 0.33333
isotope: hydrogen isotope Z: 1 proportional amount: 0.66667
poly.txt is loaded
11 : 0.04 is loaded
10 : 0.01 is loaded
12 : 0.31778 is loaded
1 : 0.63332 is loaded
1 : 0.63332 is loaded
204 : 0.014 is loaded
206 : 0.241 is loaded
207 : 0.221 is loaded
208 : 0.524 is loaded
208 : 0.524 is loaded
106 : 0.0125 is loaded
108 : 0.0089 is loaded
110 : 0.1249 is loaded
111 : 0.128 is loaded
112 : 0.2413 is loaded
113 : 0.1222 is loaded
114 : 0.2873 is loaded
116 : 0.0749 is loaded
12 : 0.33333 is loaded
1 : 0.66667 is loaded

any advice much appreciated! (and, yes, it says ‘void’ as the return. I could wait for a return from each method call before I run the next, but I don’t know how to do that.)

I MAY NOT HAVE FOUND THE WAY TO DO THIS ON TINTERWEBS BECAUSE I DON’T REALLY KNOW WHAT THE TERMINOLOGY IS, IF THIS IS THE CASE LINKS TO FIXED PROBLEMS ARE GREAT!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:08:21+00:00Added an answer on May 18, 2026 at 12:08 pm

    In what way is my question incomprehensible? (Seriously, other than my lack of knowledge of technical programming terms, I wrote the code so it could be understood. What does the question lack?)

    The method above is called six times from the main method.

    Each time it is called it takes input from a file (such as the shortest one, which I have included to demonstrate how the code should behave, others are longer).

    Following the code through you should notice that the first line of the input file is held in a variable called lamda which is not used again other than being output to the outpuf file.

    Sequential lines of the inout file are then read and placed into a vector of two entries, corresponding to the ‘Z’ of the isotope and relative abundance of that particular ‘Z’. This vector is then put into a list. The values which are held in the vector are clearly supposed to be the ones which are written to the output file.

    As you can see, there are more entries in the list than there are lines in the input file.

    These extraneous lines arise from the same method being run, in a different object.

    The ‘spillage’ to which I refer is the spillage of items in a list, which is in a seperate object, into another. Due to the number of methods running, there is a lot of ‘spillage’.

    Due to its similarity of memory adresses being shared by things in c, I called it the same thing, as I thought it was, essentially.

    What I require is advice on how to avoid this occurring, there must be many programs which open the same type of object and run their methods (as it seems in this case) in parallel. I cannot clear the lists from memory as each is required so that the material they describe behaves as it physically should.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As title says, I have a yaml file with values like development: email:dev@abc.com test:
Like the title says Where and How (i.e. if encrypted, with what method) does
Pretty much what the title says really. We have some code that is .NET
Title says it mostly. I want to add a simple extension method to the
Question title says it all really - a lot slower than VS2005, with a
As title says, how do I call a java class method from a jsp,
Like the title says, how do you create custom code snippets in Visual Studio
The title says everything. I am talking about C/C++ specifically, because both consider this
The title says everything. I'm using Visual Studio Team System 2008 Database Edition and
The title says most of what I'd like to accomplish. I'd like to render

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.