/////////////////////////////////////////////////////////////////////////////// // // Exam Score Histogram // // Opens a file typed in by user (assumed well formed file with 1 score per line and scores // in range 0 through to 100 // A historgram then plotted of the scores. An "X" per range separed into0-98, 10-19...90-99 // Scores placed into the groupd based on the first digit. // // Where file name is wrong, error chck ion palce to reprompt // // File loaded into a vector (that's array in Java speak) // // // Paul Yeatman // 20170911 // // Version Notes: // Initial version (v1.0) // /////////////////////////////////////////////////////////////////////////////// /* Two ways to do this: * 1. read the contents of the file into a vector, then examine the data in the vector * 2. read the contents of the file and assign values to relevant vector grid at the same time * NOTE, intialise the vectore so all cells = 0 before option 2 */ #include "console.h" #include "simpio.h" #include "strlib.h" #include #include using namespace std; /////////////////////////////////////////////////////////////////////////////// // // Constants should be declared here // /////////////////////////////////////////////////////////////////////////////// //const string STATS_FILE = "scores.txt"; /////////////////////////////////////////////////////////////////////////////// // // Variiables should be declared here // /////////////////////////////////////////////////////////////////////////////// /* !! The convention is supposedly adding an i at the start of ints, d for double, * s for string etc. That causes a lot of work if at some time we want to change * the into to a double and we have placed the variable name iNumber multiple times * in the code with and want to change it to a double. dNumber. We could cast it to * a double, but it would be easier to * not put the lower character i,d etc in front. * * If I want to make the code easier to read, the i, d, s etc is the way to go. */ string sFileName; string sScores; vector arrScores; // error is storage size is unknown (of course it is!!) /////////////////////////////////////////////////////////////////////////////// // // Typedef section. Create an alias name for another data type. // /////////////////////////////////////////////////////////////////////////////// // typedef vector Coordinates; // this gived vector the name of Coordinates. Used for collecting the mouse click points /////////////////////////////////////////////////////////////////////////////// // // Function Prototypes are declare here here, alternately, just whack all the // methods main calls before main! // // Practically, it's good to list them all at the top, so you do not have to work // out where to place it in the list of functions that are written in the program. // Alternately, put the FP's after Main. // /////////////////////////////////////////////////////////////////////////////// string FileOpenPrompt(); void OpenFile (string sFileName); /////////////////////////////////////////////////////////////////////////////// // // DataStructure struct ScoreStats () // // This contains the structure of the data read from he file containing scores // /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// // // Main Program // /////////////////////////////////////////////////////////////////////////////// int main() { // stuff happens FileOpenPrompt(); if (sFileName != "") { OpenFile(sFileName); } else { cout << "You pressed RETURN. Program has ended.\n"; // break; } // manipulate vector // output histogram return 0; } /////////////////////////////////////////////////////////////////////////////// // // FileOpenPrompt - asks for a file name and passes this back to main // /////////////////////////////////////////////////////////////////////////////// string FileOpenPrompt(){ cout << "Enter filename (RETURN to quit): "; sFileName = getLine(); return sFileName; } /////////////////////////////////////////////////////////////////////////////// // // OpenFile - takes a passed string and tried to open a file with the name of // the string. Imports each line into a vector // /////////////////////////////////////////////////////////////////////////////// void OpenFile (string sFileName){ cout << "OpenFile(sFileName) has started.\n"; //read file ifstream inScores; int x= 0; while (true) { inScores.open(sFileName.c_str()); // Standford library reader !! Requires C-string, hence the c_str // if (inScores.fail()) Error("The file could not be read"); // !! in one of the Stanford libs. Not working for some reason. if (!inScores.fail()) break;// !inScores is a if it does not fail...skip the error msg and go to getline { // error handling cerr << "Error: The file could not be read, try again.\n"; inScores.clear(); FileOpenPrompt(); } getline (inScores, sScores); cout << sScores << " is line " << x << " = line read as"; arrScores.push_back(sScores); if (inScores.fail()) { cout << "File read complete." << endl; break; // eof reached } // cout << sScores << "sScores"<< endl; // cout << arrScores.size() << "arr output" << endl; //getline (inScores, arrScores[iArrayRef]); // chucks an error // cout << sScores << endl; } // inScores.close(); // cout << "File read complete." << endl; for (int iArrayRef = 0; iArrayRef < arrScores.size(); iArrayRef++) { cout << arrScores[iArrayRef] << endl; cout << arrScores.size() << "arrScores size" << endl; cout << "arrScores[iArrayRef] test" << endl; } // cout << sScores << endl; //inScores.clear(); //getline(in, line); // cout << "Num lines = test" << endl; // just for kicks, add the numbers up. // while (true) { // string line; // getline(in, line); // cout << line << endl; // if (in.fail()) break; /* in >> iReadValue; // error check follows if (in.fail()) break; // update the stats // sort into the buckets here... // if position 0 = 1.. // if postion 0 = 2... if (iReadValue < iMinScore) iMinScore = iReadValue; if (iReadValue > iMaxScore) iMaxScore = iReadValue; iTotalScore += num; iFileLines ++; } */ // } }