K
Kush
Hello All,
I am having problem writing some data to file and I do not understand
my mistake / misconception.
I am trying to write some simple text to file but the text is rather
output to screen!
Below is my entire code from my problem, but I just want to focus on
the method "write_percentage_per_side_to_file(ofstream &os)". From my
understanding I am passing an ofstream object which I declared in main(
) as "ofstream os = ("C:\\output.txt", ios:
ut);" into this method,
and since I declared this as an output stream object, that using:
os << "Text"
Would write this to the filename I specified in my declaration of "os".
Any help is appreciated!
Cheers,
Kush
----------------------------------------------------------------------------------------------------------------------------------------
#include <iostream.h>
#include <math.h>
#include <fstream.h>
//********************************************************Class
definitions
class coordinate
{
private:
int x, y;
public:
void initialize(int a, int b);
int provide_x_coordinate(void);
int provide_y_coordinate(void);
};
class side
{
private:
coordinate left_side, right_side;
public:
void initialize(int ax, int ay, int bx, int by);
double length(void);
};
class property
{
private:
side side1, side2, side3, side4;
double perimeter(void);
public:
void initialize(void);
void write_percentage_per_side_to_file(ofstream &os);
};
//********************************************************coordinate
void coordinate::initialize(int a, int b)
{
x = a;
y = b;
}
int coordinate:
rovide_x_coordinate(void)
{
return x;
}
int coordinate:
rovide_y_coordinate(void)
{
return y;
}
//********************************************************side
void side::initialize(int ax, int ay, int bx, int by)
{
left_side.initialize(ax, ay);
right_side.initialize(bx, by);
}
double side::length(void)
{
int left_x, left_y, right_x, right_y;
left_x = left_side.provide_x_coordinate();
left_y = left_side.provide_y_coordinate();
right_x = right_side.provide_x_coordinate();
right_y = right_side.provide_y_coordinate();
return ( sqrt( ((right_x - left_x) * (right_x - left_x)) + ((right_y -
left_y) * (right_y - left_y)) ) );
}
//********************************************************property
void property::initialize(void)
{
side1.initialize(1, 6, 10, 10);
side2.initialize(10, 10, 36, 4);
side3.initialize(36, 4, 0, 0);
side4.initialize(0, 0, 1, 6);
}
double property:
erimeter(void)
{
return (side1.length() + side2.length() + side3.length() +
side4.length() );
}
void property::write_percentage_per_side_to_file(ofstream &os)
{
os << "The percentage length of side1 relative to the perimeter is "
<< (side1.length() / perimeter() ) * 100 << endl
<< "The percentage length of side2 relative to the perimeter is " <<
(side2.length() / perimeter() ) * 100 << endl
<< "The percentage length of side3 relative to the perimeter is " <<
(side3.length() / perimeter() ) * 100 << endl
<< "The percentage length of side4 relative to the perimeter is " <<
(side4.length() / perimeter() ) * 100 << endl;
os.close();
}
//********************************************************main()
void main(void)
{
property lot;
lot.initialize();
ofstream os = ("C:\\output.txt", ios:
ut);
lot.write_percentage_per_side_to_file(os);
}
//********************************************************end of program
I am having problem writing some data to file and I do not understand
my mistake / misconception.
I am trying to write some simple text to file but the text is rather
output to screen!
Below is my entire code from my problem, but I just want to focus on
the method "write_percentage_per_side_to_file(ofstream &os)". From my
understanding I am passing an ofstream object which I declared in main(
) as "ofstream os = ("C:\\output.txt", ios:
and since I declared this as an output stream object, that using:
os << "Text"
Would write this to the filename I specified in my declaration of "os".
Any help is appreciated!
Cheers,
Kush
----------------------------------------------------------------------------------------------------------------------------------------
#include <iostream.h>
#include <math.h>
#include <fstream.h>
//********************************************************Class
definitions
class coordinate
{
private:
int x, y;
public:
void initialize(int a, int b);
int provide_x_coordinate(void);
int provide_y_coordinate(void);
};
class side
{
private:
coordinate left_side, right_side;
public:
void initialize(int ax, int ay, int bx, int by);
double length(void);
};
class property
{
private:
side side1, side2, side3, side4;
double perimeter(void);
public:
void initialize(void);
void write_percentage_per_side_to_file(ofstream &os);
};
//********************************************************coordinate
void coordinate::initialize(int a, int b)
{
x = a;
y = b;
}
int coordinate:
{
return x;
}
int coordinate:
{
return y;
}
//********************************************************side
void side::initialize(int ax, int ay, int bx, int by)
{
left_side.initialize(ax, ay);
right_side.initialize(bx, by);
}
double side::length(void)
{
int left_x, left_y, right_x, right_y;
left_x = left_side.provide_x_coordinate();
left_y = left_side.provide_y_coordinate();
right_x = right_side.provide_x_coordinate();
right_y = right_side.provide_y_coordinate();
return ( sqrt( ((right_x - left_x) * (right_x - left_x)) + ((right_y -
left_y) * (right_y - left_y)) ) );
}
//********************************************************property
void property::initialize(void)
{
side1.initialize(1, 6, 10, 10);
side2.initialize(10, 10, 36, 4);
side3.initialize(36, 4, 0, 0);
side4.initialize(0, 0, 1, 6);
}
double property:
{
return (side1.length() + side2.length() + side3.length() +
side4.length() );
}
void property::write_percentage_per_side_to_file(ofstream &os)
{
os << "The percentage length of side1 relative to the perimeter is "
<< (side1.length() / perimeter() ) * 100 << endl
<< "The percentage length of side2 relative to the perimeter is " <<
(side2.length() / perimeter() ) * 100 << endl
<< "The percentage length of side3 relative to the perimeter is " <<
(side3.length() / perimeter() ) * 100 << endl
<< "The percentage length of side4 relative to the perimeter is " <<
(side4.length() / perimeter() ) * 100 << endl;
os.close();
}
//********************************************************main()
void main(void)
{
property lot;
lot.initialize();
ofstream os = ("C:\\output.txt", ios:
lot.write_percentage_per_side_to_file(os);
}
//********************************************************end of program