//Self-Write.cpp - Creates Simple programs from IPO instructions //Written and edited by Gilad "Speaker For The Dead" Barlev //Final Edit on 11/06/2002 (DD/MM/YYYY) //Re-Edited to Compile with g++ on 05/04/2007 (DD/MM/YYYY) //Submitted as a Final Project for Mr. Cockrum's 2nd Period Foundations of Programming 2 Class #include #include #include #include #include using namespace std; void get(string prompt, string &input) //get(prompt, variable); {//get - prompts and gets a value cout << "Enter " << prompt << ": "; getline(cin, input); if (cin.fail()) { cin.clear(); cin.ignore(1000, '\n'); } }//end of get function void get(string prompt, int &input) //get(prompt, variable); {//get - prompts anbd gets a value cout << "Enter " << prompt << ": "; cin >> input; if (cin.fail()) { cin.clear(); cin.ignore(1000, '\n'); } }//end of get function //function prototypes void addInitialize(string &); void addInput(string &, string); void addProcessing(string &, string[100], int &); void addOutput(string &, string[100], int); int main() {//main - main function //declare fstream commands ofstream fout; ifstream fin; //intro cout << "Program Writer\n\n"; //declare main function variables string filename; //name of file char x = 'n'; //multi-use variable (character) int y; //multi-use variable (integer) string initialize[100]; //initialization commands string input[100]; //input commands string processing[100]; //processsing commands string output[100]; //output commands int length[4] = {0,0,0,0}; //numbers of commands {initialize, input, processing, output} while (tolower(x) != 'y') {//create file get("Filename (not including extension)",filename); filename = filename + ".cpp"; fin.open(filename.c_str()); if (fin.is_open() ) {//if file already exists cout << "Erase existing file? (y/n)"; cin >> x; }//end if file already exists else x = 'y';//if file DOES NOT exist }//end create file fin.close(); //write beginning of file fout.open(filename.c_str()); fout << "//Created using Program Writer\n" << "#include \n" << "#include \n" << "#include \n" << "using namespace std;\n" << "int main()\n" << "{\n"; main: //main menu system("cls"); cout << "\n\n***MAIN MENU***\n" << "(1)Add Input\n" << "(2)Add Calculation\n" << "(3)Add Output\n" << "(4)Done\n\n"; get("selection",y); switch(y) {//add commands branch case 1: //add input addInitialize(initialize[length[0]]); addInput(input[length[1]], initialize[length[0]]); length[0]++; length[1]++; cout << "Input added"; getch(); goto main; case 2: //add calculation (processing) addProcessing(processing[length[2]], initialize, length[0]); length[2]++; cout << "Calculation added"; getch(); goto main; case 3: //add output addOutput(output[length[3]], initialize, length[0]); length[3]++; cout << "Output added"; getch(); goto main; }//end add commands branch //write commands to file for (y = 0; y < length[0]; y++)//write initializations fout << "\t" << initialize[y] << endl; for (y = 0; y < length[1]; y++)//write input fout << "\t" << input[y] << endl; for (y = 0; y < length[2]; y++)//write processing fout << "\t" << processing[y] << endl; for (y = 0; y < length[3]; y++)//write output fout << "\t" << output[y] << endl; //write end of file fout << "\tsystem(\"PAUSE\");\n" << "\treturn 0;\n" << "}\n"; fout.close(); cout << "\nProgram completed and ready for compliling...\n"; system("PAUSE"); return 0; }//end of main function void addInitialize(string &initialize) //addInitialize(initialize command); {//addInitialize - add initialization command //declare addInitialize variables int x = 0; //multi-use variable (integer) string y; //multi-use variable (string) //select variable type cout << "\nVARIABLE TYPES\n" << "(1)Integer\n" << "(2)Real Number\n" << "(3)Character\n" << "(4)String\n\n"; while (x < 1 || x > 4) get ("selection", x); switch(x) {//select variable type branch case 1: //integer y = "int "; break; case 2: //real number y = "float "; break; case 3: //character y = "char "; break; case 4: //string y = "string "; }//end select variable type branch initialize = y;//add variable type to command getline(cin, y);//debug //get name of variable get("name of variable", y); initialize = initialize + y;//add variable name to command //get initial value get ("initial value", y); //add initial value to command if (x == 1 || x == 2) initialize = initialize + " = " + y; else if (x == 3) initialize = initialize + " = " + "'" + y + "'"; else initialize = initialize + " = " + "\"" + y + "\""; initialize = initialize + ";";//end command line }//end of addInitialize function void addInput(string &input, string variable) //addInput(input command, variable); {//addInput - add input command //declare addInput variables string x; //multi-use variable (string) int y = variable.find(" = ",0); //multi-use variable (integer) variable.assign(variable, 7, y - 7); //get variable prompt get("Variable Prompt", x); //create input command input = "cout << \"Enter " + x + ": \";\n\t"; if (variable.compare(0,6,"string") == 0) x = "getline(cin," + variable + ");"; else x = "cin >> " + variable + ";"; input = input + x; }//end of addInput function void addProcessing(string &processing, string variables[100], int &len) //addProcessing(processing command, variable list, length of variable list); {//addProcessing - add processing command (calculation) //display list of variables cout << "AVAILABLE VARIABLES\n"; for (int i = 0; i < len; i++) cout << "(" << i+1 << ") " << variables[i] << endl; cout << "(" << len+1 << ") New Variable\n"; //select final variable int x = 0; //multi-use variable (integer) while (x < 1 || x > len+1) get("Final Variable", x); if (x == len+1) {//create new variable addInitialize(variables[len]); len++; }//end create new variable x--; int y = variables[x].find(" = ",0); //multi-use variable (integer) //write beginning of processing command processing.assign(variables[x], 7, y - 7); processing = processing + " = "; //get final variable string formula; //formula to get final variable cout << "Ex. (variable1 + variable2) * variable 3\n"; //getline(cin, formula);//debug get("formula to get final (include names of variables)",formula); //write end of processing command processing = processing + formula + ";"; }//end of addProcessing function void addOutput(string &output, string variables[100], int length)//addOutput(output command, variable list, length of variable list); {//addOutput - add output command //display list of variables cout << "AVAILABLE VARIABLES\n"; for (int i = 0; i < length; i++) cout << "(" << i+1 << ") " << variables[i] << endl; cout << "(" << length+1 << ") No Variable\n"; //select variable int x = 0; //multi-use variable (integer) while (x < 1 || x > length+1) get("Output Variable", x); x--; //get comment string y; //multi-use variable (string) getline(cin, y);//debug get("Comment",y); //write output command if (x != length) {//write output command WITH variable int z = variables[x].find(" = ",0); //multi-use variable (integer) output.assign(variables[x], 7, z-7); output = "cout << \"" + y + ":\" << " + output + "<< endl;"; }//end write output command WITH variable else//write output command WITHOUT variable output = "cout << \"" + y + "\\n\";"; }//end of addOutput command //This program was intended for private, education and entertainment uses only //This program was written to be compiled using Microsoft Visual C++ v6.0 Minor editing may be needed for your compiler //To contact Gilad "Speaker For The Dead" Barlev, send email to //Gilad "Speaker For The Dead" Barlev assumes no responsibility for undesired effects this program may have on your machine.