Please send questions to
st10@humboldt.edu .
#include <fstream> // DOES fstream include iostream? NO;
#include <iostream> // (comment this out, to see this!)
#include <cstdlib> // (so can use exit function)
#include <string>
using namespace std;
// can we include the DATE/TIME with the appended total?
// consider: what is DIFFERENT between this opening
// comment block, and that for io2.cpp?
// What is the SAME?
// Contract: void -> int
// Purpose: interactively find out how many rats there
// are, get their weights, and determine the total
// of all of their weights. Append this total weight
// in a message to the end of file
// "allRatWeights.txt" in the directory from
// where this program is run (or create this file
// with this as its first line, if it didn't
// already exist).
//
// Examples: if, when prompted, the user enters 4,
// then the user should be prompted to enter
// 4 rat weights; if the 4 weights entered
// are 13.3, 5, 27.1, and 15.5, then it
// should append to the file "allRatWeights.txt"
// in the current directory:
// The total weight is 60.9
// If the user then runs this program again,
// and this time enters 2 when prompted for
// number of rats, and weights 500 and 1000,
// the following will become the NEXT line in
// "allRatWeights.txt", after the line above:
// The total weight is 1500
//
// by: Sharon M. Tuttle
// last modified: 10-28-03
int main ( )
{
// local declarations
double weight, total = 0;
int num_rats;
// we declare an ofstream variable, an output file
// stream variable, giving it the name output_stream
ofstream output_stream, date_out;
ifstream date_in;
// an output file stream needs to be OPENED using
// using the appropriate constant in
// order to append to it --- this open function
// expects two parameters, a string, representing
// the name of the file where you want the output
// to go, and the constant indicating appending.
// (why open it so soon? because why get the info
// from the user, if will not be able to append
// to output file?)
output_stream.open("allRatWeights.txt", ios::app);
if (output_stream.fail())
{
cout << "Could not open allRatWeights.txt "
<< "for appending..." << endl;
exit(1);
}
// how many rats are there?
cout << "how many rats are being entered? ";
cin >> num_rats;
// get each rat's weight, accumulate sum of weights
for (int i=0; i < num_rats; i++)
{
cout << "Enter the next rat weight: ";
cin >> weight;
total += weight;
}
// can I get the current date/time info into
// a file named temp_io6 in the current dir?
system("date > temp_io6");
// read from that file?
date_in.open("temp_io6");
if (date_in.fail())
{
cout << "arggh! temp_io6 wouldn't open!!"
<< endl;
exit(1);
}
string myDate;
getline(date_in, myDate);
// append the sum of all the rats' weights, but to
// the output file stream instead of to
// cout! (Notice how, once you set it up, you treat
// it like cout...)
output_stream << myDate << endl
<< "The total weight is " << total
<< endl;
// then, when you are done with it, you should
// close your output file stream...
output_stream.close();
// ...and remove our temporary date file...
system("rm temp_io6");
return 0;
}