//--------------------------------------------------------- // SetwDemo.cpp // // Demonstrate the setw manipulator in conjunction with the // left and right manipulators. // // D. Searls // Asbury College // May 2006 //--------------------------------------------------------- #include <iostream> #include <iomanip> using namespace std; int main() { double dvdCost = 12.95; double vhsCost = 9.95; double hdtvCost = 1999.9; cout << fixed << setprecision(2); cout << "Using setw alone, output is right-justified by default:\n\n"; cout << setw(10) << "DVD" << setw(8) << dvdCost << endl; cout << setw(10) << "VHS" << setw(8) << vhsCost << endl; cout << setw(10) << "HDTV" << setw(8) << hdtvCost << endl << endl; cout << "Now make output left-justified:\n\n"; cout << left; cout << setw(10) << "DVD" << setw(8) << dvdCost << endl; cout << setw(10) << "VHS" << setw(8) << vhsCost << endl; cout << setw(10) << "HDTV" << setw(8) << hdtvCost << endl << endl; cout << "Finally, make text left-justified and numbers right-justified:\n\n"; cout << setw(10) << left << "DVD" << setw(8) << right << dvdCost << endl; cout << setw(10) << left << "VHS" << setw(8) << right << vhsCost << endl; cout << setw(10) << left << "HDTV" << setw(8) << right << hdtvCost << endl << endl; return 0; }
//*********************************************** // FileDemo.cpp // // Demonstrate basic file input and output. // // D. Searls // Asbury College // May 2006 //*********************************************** #include <iostream> #include <string> #include <fstream> using namespace std; int main() { ifstream infile; // The input file ofstream outfile; // The output file string filename; string firstName; char middleInitial; string lastName; string streetAddress; string city; string state; string zipcode; filename = "FileDemoData.dat"; infile.open(filename.c_str()); filename = "Output.txt"; outfile.open(filename.c_str()); infile >> firstName >> middleInitial >> lastName; infile.ignore(); getline(infile, streetAddress); infile >> city >> state >> zipcode; infile.close(); outfile << firstName << ' '; outfile << middleInitial << ". "; outfile << lastName << endl; outfile << streetAddress << endl; outfile << city << ", " << state << " " << zipcode << endl; outfile.close(); cout << "The output was sent to the file 'Output.txt'.\n"; return 0; }