Skip to content

Commit adb9553

Browse files
committed
feat : corrections made
1 parent f68f31f commit adb9553

File tree

2 files changed

+63
-27
lines changed

2 files changed

+63
-27
lines changed

18.IO Streams/4_student_exercise.cpp

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,74 @@
22
#include<fstream>
33
using namespace std;
44

5-
class Product{
6-
public:
5+
class Item{
6+
private:
77
string name;
88
float price;
9-
int quantity;
10-
11-
//constructor
12-
Product(string name = " ",float price = 0, int quantity = 0){
13-
this->name = name;
14-
this->price = price;
15-
this->quantity = quantity;
16-
}
9+
int qty;
10+
public:
11+
Item(){}
12+
Item(string n,float p,int q);
13+
friend ifstream & operator>>(ifstream &ifs,Item &i);
14+
friend ofstream & operator<<(ofstream &ofs,Item &i);
15+
friend ostream & operator<<(ostream &out,Item &i);
1716

18-
friend ifstream & operator>>(ifstream &ifs, Product &p);
19-
friend ofstream & operator<<(ofstream &ofs, Product &p);
2017
};
2118

22-
ifstream & operator>>(ifstream &ifs, Product &p){
23-
ifs>>p.name>>p.price>>p.quantity;
24-
return ifs;
19+
Item::Item(string n,float p,int q){
20+
name=n;
21+
price=p;
22+
qty=q;
2523
}
2624

27-
ofstream & operator<<(ofstream &ofs, Product &p){
28-
cout<<"Name : "<<p.name<<endl;
29-
cout<<"Price : $"<<p.price<<endl;
30-
cout<<"Quantity : "<<p.quantity<<endl;
25+
ofstream & operator<<(ofstream &ofs,Item &i){
26+
ofs<<i.name<<endl<<i.price<<endl<<i.qty<<endl;
3127
return ofs;
3228
}
3329

34-
int main(){
35-
Product p1("Hennessey Cognac",399.99,60);
36-
ifstream ifs("product.txt");
37-
ifs>>p1;
38-
ifs.close();
30+
ifstream & operator>>(ifstream &ifs,Item &i){
31+
ifs>>i.name>>i.price>>i.qty;
32+
return ifs;
33+
}
3934

40-
ofstream ofs("product.txt");
41-
ofs<<p1;
42-
ofs.close();
35+
ostream & operator<<(ostream &out,Item &i){
36+
out<<i.name<<endl<<i.price<<endl<<i.qty<<endl;
37+
return out;
38+
}
4339

40+
41+
int main()
42+
{
43+
int n;
44+
string name;
45+
float price;
46+
int qty;
47+
48+
cout<<"Enter number of Item:";
49+
cin>>n;
50+
51+
Item *list[n];
52+
53+
cout<<"Enter All Item "<<endl;
54+
for(int i=0;i<n;i++){
55+
cout<<"Enter "<<i+1<<" Item Name , Price and Quantity"<<endl;
56+
cin>>name;
57+
cin>>price;
58+
cin>>qty;
59+
60+
list[i]=new Item(name,price,qty);
61+
}
62+
63+
ofstream fos("product.txt");
64+
for(int i=0;i<n;i++){
65+
fos<<*list[i];
66+
}
67+
68+
Item item;
69+
ifstream ifs("product.txt");
70+
for(int i=0;i<n;i++){
71+
ifs>>item;
72+
cout<<"Item "<<i<<endl<<item<<endl;
73+
}
4474
return 0;
4575
}

18.IO Streams/product.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
adobe
2+
9.99
3+
64
4+
spotify
5+
1.99
6+
66

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy