/* PPP 11 ex 01
Write a program that reads a text file and 
converts its input to all lower case, 
producing a new file.
*/

#include "std_lib_facilities.h"

void doThis(ifstream& ifs);
void doThat(ifstream& ifs);

int main() {
	cout << "Enter the name of a file to convert to lower case: ";
	string fname;
	cin >> fname;
	ifstream fs(fname);
	doThis(fs);
	fs.clear();                 // clear fail and eof bits
	fs.seekg(0, std::ios::beg); // back to the start!
	doThat(fs);
}

void doThat(ifstream& ifs) {
	cout << "Enter output file name: ";
	string output;
	cin >> output;
	ofstream ofs(output.c_str());
	if (!ofs) error("can't open output file ", output);

	char ch;
	while (ifs.get(ch)) {
		if (isalpha(ch)) ch = tolower(ch);
		ofs << ch;
	}
}

void doThis(ifstream& ifs) {
	char c{ 0 };
	ifs >> c;
	char* buffer = &c;
	ifs.unget();
	ofstream ofs("outputFile.txt");
	char chin{ 0 };
	while (ifs.read(buffer, 1)) {
		//cout << chin;
		//if(int(chin)>64 && int(chin)<90)
		if (tolower(*buffer) == 32) {
			cout << ' ';
			ofs << ' ';
		}
		cout << char(tolower(*buffer));
		ofs << char(tolower(*buffer));
	}
}