Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Walkthrough Function Prototypes Whenever a function had enough parameters to mak

ID: 3762724 • Letter: W

Question

Walkthrough Function Prototypes Whenever a function had enough parameters to make it wrap, I wrote them in column format. void GetInputFileStream(std::ifstream * fin, std::string filename); void GetOutputFileStream(std::ofstream * fout, std::string filename); void SetGetPointer(std::istream & sin, int location); void SetPutPointer(std::ostream & sout, int location); void AnalyzeFile(std::istream & sin, int & numUsed, int & numNew, double & newTotalPrice, double & newTotalMileage, double & usedTotalPrice, double & usedTotalMileage); void PrintLine(std::ostream & sout, std::string s); void PrintNew(std::istream & sin, std::ostream & sout); void PrintUsed(std::istream & sin, std::ostream & sout); void PrintStatistics(std::ostream & sout, int numUsed, int numNew, double newTotalPrice, double newTotalMileage, double usedTotalPrice, double usedTotalMileage); std::string FormatCarInfo(std::string year, std::string make, std::string model, double price, double mileage); Sample Operation In this section, pay very close attention to the output. When specified, yours should match exactly. Also, this section will include hints on how to get the appropriate functionality to work. bool GetInputFileStream(std::ifstream * fin, std::string filename); o fin: A pointer to an input file stream object. o filename: The name of the file you want to open o Returns: true or false, depending on whether the file opened successfully or not. The caller will supply a pointer to an input file stream object. You will use this object to open an input file that has the name ‘filename.’ If the file doesn’t exist, alert the user by returning false. Sample Usage Sample Output COP3014 Assignment 6 Fall 2015 3 Sample Usage Sample Output std::ifstream fin; // 'f'ile in - fin std::string filename = "fake.txt"; bool isOpen = GetInputFileStream(&fin, filename); std::cout << filename << " open: "; std::cout << std::boolalpha << isOpen << std::endl; std::cout << std::endl; filename = "real.txt"; isOpen = GetInputFileStream(&fin, filename); std::cout << filename << " open: "; std::cout << std::boolalpha << isOpen << std::endl; std::cout << std::endl; fin.close(); Opening file fake.txt fake.txt open: false Opening file real.txt real.txt open: true void GetOutputFileStream(std::ofstream * fout, std::string filename); o fout: A pointer to an output file stream object o filename: The name of the file that you want to open o Returns: Nothing. The caller will supply a pointer to an output file stream object. You will use this object to open an output file that has the name ‘filename.’ For this assignment we will assume that output files always open successfully. Once the code below runs you should have a file with the given name and the text that you inserted into the stream. You can then open it using notepad (or whatever) and see the output as shown below on the right. Sample Usage Sample Output std::ofstream fout; // 'f'ile out - fout std::string filename = "newFile.txt"; GetOutputFileStream(&fout, filename); fout << "This is my new file! "; fout << "This is on a new line!"; fout.close(); void SetGetPointer(std::istream & sin, int location); o sin: An input stream object o location: A location, with respect to the BEGINNING of the stream, to which you want to move the pointer to. o Returns: nothing When you are reading from the stream, each time you read a character the pointer is incremented by one. That way you always get the next character. What if you want to read the same file twice? Well, you need to reset the ‘get’ pointer back to 0. In this example, pretend that the file numbers.txt exists and that it has inside it the numbers 1, 2, 3, 4, and 5 in order with a space in-between. You notice that the count resets back to 1 when the get pointer is put back at the 0 location of the file. Sample Usage Sample Output std::ifstream fin; // 'f'ile in - fin std::string filename = "numbers.txt"; bool isOpen = GetInputFileStream(&fin, filename); std::cout << filename << " open: "; std::cout << std::boolalpha << isOpen << std::endl; int number = 0; fin >> number; std::cout << "Read: " << number << std::endl; Opening file numbers.txt numbers.txt open: true Read: 1 Read: 2 Read: 1 Read: 2 Read: 3 Read: 4 COP3014 Assignment 6 Fall 2015 4 Sample Usage Sample Output fin >> number; std::cout << "Read: " << number << std::endl; SetGetPointer(fin, 0); while (fin >> number) { std::cout << "Read: " << number << std::endl; } fin.close(); Read: 5 void SetPutPointer(std::ostream & sout, int location); o sout: An output file stream object. o location: The location that you want to set the ‘put’ pointer to. o Returns: nothing When you are writing to a file you can move the ‘put’ pointer around. It would allow you to put text in a file, send the pointer back to 0, and overwrite whatever information was already there. In the example below, I put all of the alphabet in lowercase into the file. I then put the pointer at 0 and rewrite half the alphabet in uppercase. Sample Usage Sample Output std::ofstream fout; // 'f'ile out - fout std::string filename = "setPutExample.txt"; GetOutputFileStream(&fout, filename); fout << "abcdefghijklmnopqrstuvwxyz"; SetPutPointer(fout, 0); fout << "ABCDEFGHIJKLMN"; fout.close(); void PrintStatistics(std::ostream & sout, int numUsed, int numNew, double newTotalPrice, double newTotalMileage, double usedTotalPrice, double usedTotalMileage); o sout: An output file stream object o numUsed: the number of used cars o numNew: the number of new cars o newTotalPrice: the total price of all new cars o newTotalMileage: the total mileage of all new cars o usedTotalPrice: the total price of all used cars o usedTotalMileage: the total mileage of all used cars o returns: nothing In this assignment you will be analyzing the statistics of a file comprising a list of new and used cars shipped to Bob’s Car Lot. After you have analyzed the file you will need to print the results. Follow this output exactly. I highly recommend the use of setw (iomanip). Notice that each column is as wide as the header (Category, Number, Total Price, etc.) plus 1 (except the first column has no space at the beginning). The vertical line after each category is called a pipe. It is usually on the same key as the backslash right above the enter key. The beauty of this function is that it takes an ‘ostream’ as a parameter. What is an ostream? Well, it can be a console output stream (cout), a file output stream, or even a string stream (we’ll discuss later). Sample Usage Sample Output // These are example values // You should get the real ones from the file double newTotalPrice = 33333; double newTotalMileage = 44444; double usedTotalPrice = 22222; double usedTotalMileage = 99999; int numUsed = 2; int numNew = 3; std::ofstream fout; // 'f'ile out - fout // First it prints to the console COP3014 Assignment 6 Fall 2015 5 Sample Usage Sample Output std::string filename = "statistics.txt"; GetOutputFileStream(&fout, filename); // Print to screen PrintStatistics(std::cout, numUsed, numNew, newTotalPrice, newTotalMileage, usedTotalPrice, usedTotalMileage); // Print to file PrintStatistics(fout, numUsed, numNew, newTotalPrice, newTotalMileage, usedTotalPrice, usedTotalMileage); // Then it creates a file and prints the same // info. void AnalyzeFile(std::istream & sin,int & numUsed, int & numNew, double & newTotalPrice, double & newTotalMileage, double & usedTotalPrice, double & usedTotalMileage); o sin: An input file stream object o numUsed: the number of used cars in the file o numNew: the number of new cars in the file o newTotalPrice: the aggregate price of all new cars o newTotalMileage: the aggregate mileage of all new cars o usedTotalPrice: the aggregate price of all used cars o usedTotalMileage: the aggregate mileage of all used cars Here is a sample input text file: This function should read the entire file and compile the statistics. Since each of the variables are reference variables, if you make changes here they should affect the variables used to call the function. After reading in the entire file you should be able to print the statistics. Sample Usage Sample Output double newTotalPrice = 0; double newTotalMileage = 0; double usedTotalPrice = 0; double usedTotalMileage = 0; int numUsed = 0; int numNew = 0; std::ifstream fin; // 'f'ile in - fin std::string filename = "cars.txt"; bool isOpen = GetInputFileStream(&fin, filename); std::cout << filename << " open: "; std::cout << std::boolalpha << isOpen << std::endl; AnalyzeFile(fin, numUsed, Opening file cars.txt cars.txt open: true Category| Number| Total Price| Total Mileage| New| 1| 33000| 250| Used| 2| 7000| 254000| COP3014 Assignment 6 Fall 2015 6 Sample Usage Sample Output numNew, newTotalPrice, newTotalMileage, usedTotalPrice, usedTotalMileage); PrintStatistics(std::cout, numUsed, numNew,] newTotalPrice, newTotalMileage, usedTotalPrice, usedTotalMileage); void PrintLine(std::ostream & sout, std::string s); o sout: an output stream o s: a string to write to the stream o returns: nothing This function simply prints a line to whatever output stream is given. The output stream might be cout, an output file stream, or a string stream. Sample Usage Sample Output std::stringstream ss; std::ofstream fout; // 'f'ile out - fout std::string filename = "printLineExample.txt"; GetOutputFileStream(&fout, filename); std::string s = "My output string!"; PrintLine(std::cout, s); PrintLine(ss, s); std::cout << "SS: " << ss.str(); PrintLine(fout, s); fout.close(); My output string! SS: My output string! // This file will be created also void PrintNew(std::istream & sin, std::ostream & sout); o sin: an input stream o sout: an output stream o Returns: nothing This function scans the stream for all new and used cars and only prints the new cars to the sout stream. The sout stream might be cout, an output file stream, or a string stream. Sample Usage Sample Output std::ifstream fin; // 'f'ile in - fin std::string filename = "cars.txt"; bool isOpen = GetInputFileStream(&fin, filename); std::cout << filename << " open: "; std::cout << std::boolalpha << isOpen << std::endl; PrintNew(fin, std::cout); std::stringstream ssout; PrintNew(fin, ssout); // use ssout.str() to turn a stringstream // into a std::string PrintLine(std::cout, "SS: " + ssout.str()); Opening file cars.txt cars.txt open: true 2015 Jeep Wrangler 33000 250 SS: 2015 Jeep Wrangler 33000 250 void PrintUsed(std::istream & sin, std::ostream & sout); o sin: an input stream COP3014 Assignment 6 Fall 2015 7 o sout: an output stream o returns: nothing This function is the same as PrintNew except it only prints the used cars. Sample Usage Sample Output std::ifstream fin; // 'f'ile in - fin std::string filename = "cars.txt"; bool isOpen = GetInputFileStream(&fin, filename); std::cout << filename << " open: "; std::cout << std::boolalpha << isOpen << std::endl; PrintUsed(fin, std::cout); std::stringstream ssout; PrintUsed(fin, ssout); // use ssout.str() to turn a stringstream // into a std::string PrintLine(std::cout, "SS: " + ssout.str()); Opening file cars.txt cars.txt open: true 1999 Ford Ranger 3000 156000 2000 Mazda Miata 4000 98000 SS: 1999 Ford Ranger 3000 156000 2000 Mazda Miata 4000 98000 std::string FormatCarInfo(std::string year, std::string make, std::string model, double price, double mileage); o year: the year of the car o make: the make of the car o model: the model of the car o price: the price of the car o mileage:A the mileage of the car o returns: A string representation of the car’s information. As shown in the sample output, each of the blocks should be separated by one space. You’ve already read the file searching for new and used cars to print. Now, you just need to format EACH line of the file and return it as a string. The string will just be each value separated by a space. Sample Usage Sample Output std::string year; std::string make; std::string model; double price; double mileage; std::string category; std::ifstream fin; // 'f'ile in - fin std::string filename = "cars.txt"; bool isOpen = GetInputFileStream(&fin, filename); std::cout << filename << " open: "; std::cout << std::boolalpha << isOpen << std::endl; while (fin >> year && fin >> make && fin >> model && fin >> price && fin >> mileage && fin >> category) { std::string s = FormatCarInfo(year, make, model, price, mileage); PrintLine(std::cout, s); }

Explanation / Answer

1) firstly calling a file twice is inefficient.

int numOfMappings = 0;
ifstream numbersFile("numbers.txt");
string setting;
int number = 0; fin >> number; std::cout << "Read: " << number << std::endl;
while(getline(numbersFile, numbers))
{
    numOfMappings++;

}
char* mapping = new char[numOfMappings];
numbersFile.close();
cout << "numOfMappings: " << numOfMappings << endl;
numbersFile.open("numbers.txt");
while(getline(numbersFile, setting))
{
     std::cout << filename << " open: "; std::cout << std::boolalpha << isOpen << std::endl;
}

this is the basic idea in c++