I have just started Operator overloading (stream insertion and stream extraction) in C++, and got stuck at point. The program should work like If user enters the value 10 then program must store 10 five times in a text file in different lines. but the program is not storing any of the value in text file.
#include<iostream>
#include<fstream>
using namespace std;
class cntr
{
private:
int cnt;
public:
cntr();
friend istream& operator>>(istream& in, cntr& c);
friend ostream& operator<<(ostream& out, cntr& c);
};
cntr::cntr()
{
cnt = 0;
}
istream& operator>>(istream& in, cntr& c)
{
in >> c.cnt;
return in;
ofstream file;
file.open("lab5.txt", ios::app);
if (file.is_open())
{
for (int i = 0; i <= 5; i++)
{
if (c.cnt == 10)
{
in >> c.cnt;
return in;
cout << endl;
}
}
}
else
{
cout << "Error!" << endl;
}
}
ostream& operator<<(ostream& out, cntr& c)
{
out << c.cnt;
return out;
}
int main()
{
cntr r;
cin >> r;
cout << "\n" << r << endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire