#include <iostream>
#include <fstream>
usingnamespace std;
struct Fraction
{
int nu, de;
};
void Enter(Fraction *&f, int &n)
{
cout << "Enter the number of elements: ";
cin >> n;
for(int i = 0; i < n; i++)
{
cout << "Enter fraction number " << i + 1 << ":\n";
cin >> f[i].nu;
cin >> f[i].de;
}
}
void Output(Fraction *&f, int &n)
{
cout << "Elements:\n";
for (int i = 0; i < n; i++)
{
cout << f[i].nu << "/" << f[i].de << endl;
}
}
void Write(Fraction *&f, int n)
{
ofstream fout("output.bin", ofstream::binary);
if (!fout.is_open())
{
cout << "Can not create output.bin" << endl;
}
fout.write((char*)&n, sizeof(int));
for (int i = 0; i < n; i++)
{
fout.write((char*)&f[i], sizeof(Fraction));
}
fout.close();
cout << "Completely" << endl;
}
int main()
{
Fraction *a;
int n;
Enter(a, n);
Output(a, n);
}
This will let the user enter some fractions and then save them into a file name "output.bin". However, at the line cin >> f[i].nu;, the compiler told me: Unhandled exception at 0x0F75591C (msvcp140d.dll) in ex03.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC. occurred (ex03.exe is the executed file from my solution).
What happened?