//file GroceryPurchasesOneTripFcns.cpp //This program lets the user enter a filename. //and reads in purchases info for a single trip to the store //Program is broken into functions which illustrate the use of different numbers/type //of parameters. #include #include #include //needed to use c_str() member function #include #include using namespace std; //function prototypes bool openFile (ifstream &fin ); void readProcessInfo(ifstream &fin, double &purchaseTotal, int &countItem); void printHeader(); void printTotals (double, int); const double TAXRATE = 7.50; int main() { ifstream inputFile; //the file variable double purchaseTotal = 0; //total bill with tax int countItem = 0; //how many different items purchased cout << setprecision(2) << fixed; openFile(inputFile); printHeader(); readProcessInfo(inputFile, purchaseTotal,countItem); printTotals(purchaseTotal, countItem); inputFile.close(); return 0; } //function asks user for the external file name and opens it. //returns true if file opened, else returns false bool openFile (ifstream &fin) { string filename; // Get the filename from the user. cout << "Enter the filename: "; cin >> filename; // Open the file, using c_str() to convert the string object to a C-String as required fin.open(filename.c_str()); if(fin) { return true; } else { cout << "File not found \n"; return false; } } //end openFile() void printHeader() { cout << setw(10) << right << "Amount" << " " << right << "Count" << '\t' << "Taxable?" << endl; } //writes summary info to std output screen void printTotals(double purchaseTotal, int countItem) { cout << "Your total bill is " << purchaseTotal << " for " << countItem << " Items" << endl; } //function reads purchase price, and number purchased and if it is taxable //It computes the total bill and count how many different types of items were purchased //and returns these values void readProcessInfo(ifstream &fin, double &purchaseTotal, int &countItem) { double purchase; //cost of one item int howMany; //number of that item purchased char taxable; //X if taxable of - if not (in file) countItem = 0; purchaseTotal = 0; fin >> purchase >> howMany >> taxable; //priming file input while (fin) //read file till End of File { purchaseTotal = purchase*howMany + purchaseTotal; countItem++; if (taxable == 'X') purchaseTotal = (1 + TAXRATE/100)*purchaseTotal; else taxable = ' '; cout << setw(10) << right << purchase << '\t' << howMany << '\t' << " " << taxable << endl; fin >> purchase >> howMany >> taxable; //get next line of data } //end while EOF loop } //end readProcessInfo