Hands-on 4b - Hints

Program readIsotopes

Step 2 - Read data in local variables

Hints:

  • Code to open and test file:
  // Open a file, test if it was open sucessfully
  // and handle the test result
  string filename = "isotopes.txt";
  ifstream myFile (filename);
  if (! myFile.is_open ()) {
    cerr << "Cannot open file " << filename << endl;
    return 1;
  }

  // Add printing a message that myFile was open successfully
  cout << "File was open successfully." << endl;
  • Code to read the first line with comments
  // Read the line with comments (use getline)
  string commentLine;
  getline(myFile, commentLine);
  • Code to define local variables to hold read data and fill them from the file
     // Define local variables to hold read data
     // and fill them from the file

     // define variables to hold data
     string name;      
     int Z;         
     int A;         
     double halflife;  
     double abundance; 

     // Read data of one isotope
     myFile >> name >> Z >> A >> halflife >> abundance;
  • Code to print read values on the screen
     // Print read values on the screen
     // ...
    cout << "Read: " 
      << name << ", " << Z <<", " << A << ", " << halflife << ", " << abundance << endl;