I've been trying to convert my code from using iostream to fstream but seems the output file is always empty..
this is my original code
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
struct RationalNo
{
int RatNo [20];
};
struct RationalInfo
{
RationalNo rational;
int quotient;
int remiander;
float value;
};
int main()
{
srand(time(NULL));
RationalNo intNum;
RationalInfo modNum;
cout << "Original Integers" << endl;
for (int i = 0; i < 20; i++)//Array Generation
{
intNum.RatNo [i] = rand() % 101 + 1;
cout << intNum.RatNo [i] << "\t";
}
cout << endl;
cout << "Quotients of integers" << endl;//Quotient
for (int n = 0; n < 20; n++)
{
modNum.quotient = intNum.RatNo [n] / 10;
cout << modNum.quotient << "\t";
}
cout << endl;
cout << "Remainders Of Integers" << endl;//Remainder
for (int m = 0; m < 10; m++)
{
modNum.remiander = intNum.RatNo [m] % 10;
cout << modNum.remiander << "\t";
}
cout << endl;
cout << "Actual Value of integer division" << endl; //value of division
for (int q = 0; q < 10; q++)
{
modNum.value = intNum.RatNo [q] / 10;
cout << setiosflags (ios::fixed);
cout << setiosflags (ios::showpoint);
cout << setprecision (2);
cout << modNum.value << "\t";
}
cout << endl;
}
and this is my fstream attempt
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <fstream>
using namespace std;
struct RationalNo
{
int RatNo [20];
};
struct RationalInfo
{
RationalNo rational;
int quotient;
int remiander;
float value;
};
int main()
{
srand(time(NULL));
ofstream outfile("Rational.txt");
ifstream infile("Rational.txt");
RationalNo intNum;
RationalInfo modNum;
outfile.open("Rational.txt");
outfile << "Original Integer" << endl;
outfile.close();
outfile.open("Rational.txt");
for (int i = 0; i < 100; i++)//Array Generation
{
intNum.RatNo [i] = rand() % 101 + 1;
outfile << intNum.RatNo [i] << "\t";
}
outfile << endl;
outfile.close();
outfile.open("Rational.txt");
outfile << "Quotients of integers" << endl;//Quotient
outfile.close();
outfile.open("Rational.txt");
for (int n = 0; n < 100; n++)
{
modNum.quotient = intNum.RatNo [n] / 10;
outfile << modNum.quotient << "\t";
}
outfile << endl;
outfile.close();
outfile.open("Rational.txt");
outfile << "Remainders Of Integers" << endl;//Remainder
outfile.close();
outfile.open("Rational.txt");
for (int m = 0; m < 100; m++)
{
modNum.remiander = intNum.RatNo [m] % 10;
outfile << modNum.remiander << "\t";
}
outfile << endl;
outfile.close();
outfile.open("Rational.txt");
outfile << "Actual Value of integer division" << endl; //value of division
outfile.close();
outfile.open("Rational.txt");
for (int q = 0; q < 100; q++)
{
modNum.value = intNum.RatNo [q] / 10;
outfile << setiosflags (ios::fixed);
outfile << setiosflags (ios::showpoint);
outfile << setprecision (2);
outfile << modNum.value << "\t";
}
outfile << endl;
outfile.close();
}