#include <iostream> // basic i/o
#include <string>
#include <fstream> // file i/o
#define LIST_MAX 10
using namespace std;

struct Character{
	string fname;
	string lname;
};

int main(){
	ofstream outfile;
	string filename = "simpsons.csv";

	Character myList[LIST_MAX];

	myList[0].fname = "Bart";
	myList[0].lname = "Simpson";
	myList[1].fname = "Marge";
	myList[1].lname = "Simpson";
	myList[2].fname = "Homer";
	myList[2].lname = "Simpson";	
	myList[3].fname = "Lisa";
	myList[3].lname = "Simpson";	
	myList[4].fname = "Maggie";
	myList[4].lname = "Simpson";	
	myList[5].fname = "Ned";
	myList[5].lname = "Flanders";
	myList[6].fname = "Ralph";
	myList[6].lname = "Wiggum";	
	myList[7].fname = "Julius";
	myList[7].lname = "Hibbert";	
	myList[8].fname = "Kent";
	myList[8].lname = "Brockman";	
	myList[9].fname = "Nelson";
	myList[9].lname = "Muntz";	

	// When you open a file for writing, you can choose a mode as the 2nd parameter:
	// ofstream::app - append to the end of an existing file
	// ofstream::trunc - overwrite the existing contents of the file
 	// the default is trunc.

	outfile.open(filename);
	if (outfile.is_open()){
		for(int x = 0; x < LIST_MAX; x++){
			outfile << myList[x].lname << "," << myList[x].fname << endl;
		}
		outfile.close();
	}else{
		cout << "ERROR: Could not open the file for writing.";
	}

	return 0;
}

