// This program lets the user enter a filename. // and reads in a list of purchases and finds the total and the average purchase // This is program 5-24 modified by CYC #include #include // in order to accept a file name from the user #include // in order to process files #include using namespace std; int main() { ifstream inputFile; //input file name string filename; double number; //grocery item price double average; //average purchase price for one visit double total=0; //total purchases for one visit int count =0; //number purchases at one visit to store // Get the filename from the user. cout << "Enter the filename: "; cin >> filename; //I am testing the program using a file called datalist.txt // Open the file. inputFile.open(filename); // If the file successfully opens, process it else write out error message and quit if (inputFile) { // Read the numbers from the file and // display them in order to track if file is being read correctly inputFile >> number; //input the first number in the data file cout << setprecision(2) << fixed; while ( number > 0) { count++; total +=number; cout << number << endl; inputFile >> number; //input the next number } average = total/count; cout << "\nFor a total of " << count << " items " << "your average purchase price is " << average << endl; // Close the file. inputFile.close(); } else { // Display an error message. cout << "Error opening the file.\n"; } return 0; }