// This program reads the cost of items purchased and adds them up. // The program will stop reading when -1 is entered. The price of // each item and the total cost are printed to the file out.dat #include <iostream.h> #include <iomanip.h> #include <fstream.h> int main() { const int SENTINEL = -1; float price; float total = 0; ofstream outfile; // open the output file and print heading outfile.open("out.dat"); outfile << setiosflags(ios::fixed); outfile << setiosflags(ios::showpoint); outfile << setprecision(2); outfile << "Items Purchased:\n"; // read in the price of each item, add to the total, and print // price in output file cout << "Enter the price of the first item (enter -1 to end): "; cin >> price; while (price != SENTINEL) { total = total + price; outfile << " $" << price << endl; cout << "Enter the price of the next item (enter -1 to end): "; cin >> price; } // print the total cost in output file outfile << "Total Cost:\n $" << total << endl; }
Enter the price of the first item (enter -1 to end): 2.5 Enter the price of the next item (enter -1 to end): 4.75 Enter the price of the next item (enter -1 to end): 9.95 Enter the price of the next item (enter -1 to end): 1.5 Enter the price of the next item (enter -1 to end): -1
Items Purchased: $2.50 $4.75 $9.95 $1.50 Total Cost: $18.70
Email Me |
Office Hours |
My Home Page |
Department Home |
MCC Home Page
© Copyright Emmi Schatz 2002