 
//
// This is example code from Chapter 11.4 "String streams" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <fstream>

using namespace std;
//------------------------------------------------------------------------------

double str_to_double(string s) {
// if possible, convert characters in s to floating-point value
	istringstream is(s);    // make a stream so that we can read from s
	double d;
	is >> d; // If s doesn't contained characters formatted for a double, like "1.2e-3", or "-0.00123", then is will have it's fail flag set.
	if (!is) cerr << "double format error: " << s;
	return d;
}

//------------------------------------------------------------------------------


void test() {
	double d1 = str_to_double("12.4");               // testing
	double d2 = str_to_double("1.34e-3");
	double d3 = str_to_double("twelve point three"); // will call error()
}

//------------------------------------------------------------------------------

struct Temperature   {
	double temp;
	string unit;
};

//------------------------------------------------------------------------------

void my_code(string label, Temperature temp) {
	// ...
	ostringstream os;    // stream for composing a message
	os << setw(8) << label << ": "
		<< fixed << setprecision(5) << temp.temp << temp.unit;
	//someobject.display(Point(100,100), os.str().c_str());
	// ...
}

//------------------------------------------------------------------------------

int get_next_number() // get the number of a log file
{
	static int n = -1;  // static here means that 
	++n;
	return n;
}

//------------------------------------------------------------------------------

int main() {
	string input;
	int noFiles;
	istringstream is;
	test();
	cout << "How many files do you want to write to?: \n";
	cin >> noFiles;
	cin.ignore('\n',10);
	cin.clear();
	for (int i = 0; i < noFiles; ++i) {
		int seq_no = get_next_number();       // get the number of a log file
		cout << seq_no << " ";
		ostringstream name;  // make a stream, name, that we can write to from the program using a concatenation of strings and numbers
		name << "myfile" << seq_no << ".txt";           // e.g., myfile17
		cout << "Enter text to write into " << name.str() << ": \n";
		getline(cin, input);
		ofstream logfile(name.str()); // e.g., open myfile17
		logfile << input;
	}
}

//------------------------------------------------------------------------------
