c++ - I need some help formatting a file in an organized way -
i'm writing program update, delete , list tools hardware store in file. i'm able update , list tools in file in unorganized format on screen. here code output. give me idea on how format headers in file line more evenly? thanks.
#include <iostream> #include <fstream> #include <string> #include "hardware.h" #include <iomanip> using namespace std; int main() { int ch = 0, count=0, rno, qty; string filename, h1, h2, h3, h4,hname; double c; ifstream infile; ofstream outfile; hardwaredata hwd[10]; cout<<endl<<endl<<"enter 1 opening data file."<<endl; cout<<"enter 2 list records."<<endl; cout<<"enter 3 add record."<<endl; cout << "enter 4 delete entry."<<endl; cout<<"enter 5 exit program."<<endl; cout<<"choice: "; cin>>ch; while(ch!=5) { switch(ch) { case 1: infile.open("hardware.dat"); if(!infile) break; case 2: { while(!infile.eof()) { infile>>h1>>h2>>h3>>h4; cout<<h1<<"\t"<<h2<< "\t"<<h3<<"\t"<<h4 <<endl; } } break;
this hardware.dat file
record_num tool_name quantity cost 3 electric sander 7 57.98 17 hammer 76 11.99 24 jig saw 21 11.00 39 lawn mower 3 79.50 56 power saw 18 99.99 68 screwdriver 106 6.99 77 sledge hammer 11 21.50 83 wrench 34 7.50
if use setw(n)
adjust space taken each field space them correctly, provided n
greater maximum number of characters in field.
i believe output auto aligned left, if not need add in std::left
(or left
since using namespace std
).
while(!infile.eof()) { infile>>h1>>h2>>h3>>h4; cout << setw(10) << h1; cout << setw(10) << h2; cout << setw(10) << h3; cout << setw(10) << h4; cout << endl; // cout <<h1<<"\t"<<h2<< "\t"<<h3<<"\t"<<h4 <<endl; }
Comments
Post a Comment