Write a program to read a list containing item name, item code, and cost interactively and produce a three column output as shown below.
Name | Code | Cost |
Turbo C++ | 1001 | 250.95 |
C Primer | 905 | 95.70 |
… … … … | … … … … | … … … … |
Note that the name and code are left-justified and the cost is right justified with a precision of two digits. Trailing zeros are shown.
#include<iostream> #include<iomanip> #include<string> using namespace std; class item { char name[40]; int code; float cost; public: void get_data(char *n,int c,float co) { strcpy(name,n); code=c; cost=co; } void display(); }; void item:: display() { cout.precision(2); cout.setf(ios::fixed,ios::floatfield); cout.setf(ios::showpoint); cout.setf(ios::left,ios::adjustfield); cout<<setw(40)<<name<<code; cout.setf(ios::right,ios::adjustfield); cout<<setw(15)<<cost<<endl; } int main() { item a[5]; a[0].get_data("Tarbo C++",1001,250.95); a[1].get_data("C primer",905,95.7); a[2].get_data("algorithm",1111,120.5); a[3].get_data("principle of electronics",2220,150.85); a[4].get_data("solution of balagurusamy",6666,145.00); cout<<setw(10)<<"name"<<setw(34)<<"code"<<setw(15)<<"cost"<<endl; for(int i=0;i<60;i++) cout<<"-"; cout<<endl; for(i=0;i<5;i++) a[i].display(); return 0; }
OUTPUT
name code cost
——————————————————————————
Tarbo C++ 1001 250.95
C Primer 905 95.70
algorithm 1111 120.50
Principle of electronics 2220 150.85
Solution of balaguruswamy 6666 145.00
Modify the above program to fill the unused spaces with hyphens.
#include<iostream> #include<iomanip> #include<string> using namespace std; class item { char name[40]; int code; float cost; public: void get_data(char *n,int c,float co) { strcpy(name,n); code=c; cost=co; } void display(); }; void item:: display() { cout.precision(2); cout.fill('-'); cout.setf(ios::fixed,ios::floatfield); cout.setf(ios::showpoint); cout.setf(ios::left,ios::adjustfield); cout<<setw(40)<<name<<code; cout.setf(ios::right,ios::adjustfield); cout<<setw(15)<<cost<<endl; } int main() { item a[5]; a[0].get_data("Tarbo C++",1001,250.95); a[1].get_data("C primer",905,95.7); a[2].get_data("algorithm",1111,120.5); a[3].get_data("principle of electronics",2220,150.85); a[4].get_data("solution of balagurusamy",6666,145.00); cout<<setw(10)<<"name"<<setw(34)<<"code"<<setw(15)<<"cost"<<endl; for(int i=0;i<60;i++) cout<<"-"; cout<<endl; for(i=0;i<5;i++) a[i].display(); return 0; }
OUTPUT
name code cost
—————————————————————————————-
Tarbo C++ ——————– 1001———–250.95
C Primer ———————-905————-95.70
algorithm———————-1111————120.50
Principle of electronics——2220———–150.85
Solution of balaguruswamy–6666——— 145.00
Write a program which reads a text from the keyboard and displays the following information on the screen in two columns:
- (a) Number of lines
- (b) Number of words
- (c) Number of characters
Strings should be left-justified and numbers should be right-justified in a suitable field width.
#include<iostream> #include<iomanip> #include<string> #include<stdio> using namespace std; int main() { char line[1000]; char ch; int c; int word,lines,chr; word=0; lines=0; chr=0; int end=0; cout<<" Enter text : \n"; while(end==0) { c=0; while((ch=getchar())!='\n') line1=ch; line1='\0'; if(line[0]=='\0') break; else { word++; for(int i=0;line[i]!='\0';i++) if(line[i]==' ' || line[i]=='\t' || line[i]=='\n') word++; } lines++; chr+=strlen(line); } cout.setf(ios::left,ios::adjustfield); cout<<setw(25)<<"Number of lines"<<setw(25) <<"Number of words "<<"Number of characters "<<endl; cout.setf(ios::right,ios::adjustfield); cout<<setw(10)<<lines<<setw(24)<<word<<setw(25)<<chr<<endl<<endl; return 0; }
OUTPUT
Enter text :
santo reads in class five.
He always speak the truth.
He respects his teachers.
He feels shy when I admire him.
I like his morality.
Number of lines Number of words Number of characters
5 25 128
Note: If you press the Enter button two times, the program will terminate.
Next Previous