Hi,
I am currently trying to do an assignment where I read in from a file, three variables from each line, add them together and then at the end basically add each column up.
We have to use classes.
Reading works, adding each line works, and writing all that to a file works.
However, adding each individual column gets me some really crazy and crappy stuff.
I have tried doing it with temp variables in my class, temp variables in my loop and out of my loop.
I either get odd results like “-9.25596e+061” or, results that appear to have worked correct but completely bad math.
Right now my code might look a little messy, because well I have tried 15 different things, and having to change stuff so much I did not want to waste the time of typing lots of nice comments and descriptive variable names. And right now my output it kinda messy, but it is pointless cleaning that up unless I can get it to add correct!
Here is my code:
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
class SalesFigures
{
public:
SalesFigures(double jan, double feb, double march_);
SalesFigures();
void write_out();
void write_out_total();
void math(double in, double in2, double in3);
private:
double january;
double febuary;
double march;
double jan_total;
double feb_total;
double march_total;
};
int main()
{
double in_1,in_2,in_3;
SalesFigures one;
// double temp1, temp2, temp3;
ifstream in_stream;
in_stream.open("source.txt");
while (! in_stream.eof())
{
in_stream >> in_1 >> in_2 >> in_3;
one =SalesFigures(in_1,in_2,in_3) ;
one.write_out();
one.math(in_1,in_2,in_3);
// temp1 = in_1 + temp1;
// temp2 = in_2 + temp2;
// temp3 = in_3 + temp3;
}
one.write_out_total();
// ofstream out_f;
// out_f.open("output.txt",ios::app);
// out_f << temp1 << " " << temp2 << " " << temp3;
in_stream.close();
system("pause");
return 0;
}
SalesFigures::SalesFigures(double jan, double feb, double march_)
{
january = jan;
febuary = feb;
march = march_;
}
SalesFigures::SalesFigures()
{
january = 0;
febuary = 0;
march = 0;
jan_total = 0;
feb_total = 0;
march_total = 0;
}
void SalesFigures::write_out()
{
ofstream out_f;
out_f.open("output.txt",ios::app);
out_f << january << " " << febuary << " " << march << " " << january + febuary + march << endl ;
out_f.close();
}
void SalesFigures::math(double in, double in2, double in3)
{
jan_total = (in + january);
feb_total = (in2 + febuary);
march_total = (in3 + march);
}
void SalesFigures::write_out_total()
{
ofstream out_f;
out_f.open("output.txt",ios::app);
out_f << jan_total << " " << feb_total << " " << march_total << " " << jan_total + feb_total + march_total << endl ;
out_f.close();
}
As you will see some things commeted out that I tried.
The source file is currently hardcoded to source.txt, here is an example of it:
Code:
105.1 298.3 37998.4
644.9 5654.8 6654.2
65457.1 86465.1 96454.1
Any help would be much appericated!