Sending all output to a file
Mar 21, 2017 at 7:58pm UTC
I am trying to get all of my answers/numbers to be outputted to my .txt file, but I am only getting the last value in the set for each of the values, where did I go wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
//A program to compute a pair of estimates of π
//Information is sent to a file instead of console
#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
#include <iomanip>
using namespace std;
int main(void )
{
const char filepath[] = "C:\\CoutputFiles\\File2.txt" ;
const char ErrorMessage[] = "Can't open file " ;
fstream OutStream(filepath, ios::out);
if (OutStream.fail())
{
cerr << ErrorMessage << filepath;
exit(-1);
}
int i;
int hex = 6;
int sides;
double tolerance, fx, gx, radius, inscribed_perimeter, circumscribed_perimeter;
char zzz;
cout << "Sides " << " Inscribed Perimeter" << " Circumscribed Perimeter" << endl;
for (i = 0, sides = hex, radius = 0.5; i < 30; i++)
{
inscribed_perimeter = radius*sides;
fx = sqrt(0.25 - (radius*radius) / 4);
gx = radius / (2 * fx);
circumscribed_perimeter = gx*sides;
cout << sides << " " ;
cout << std::setprecision(15) << inscribed_perimeter << " " ;
cout << std::setprecision(15) << circumscribed_perimeter << endl;
//printf ("%d \t%.14f\t%.14f\n", sides, inscribed_perimeter, circumscribed_perimeter);
tolerance = (circumscribed_perimeter - inscribed_perimeter);
if (tolerance < 0.000000000000001)
{
break ;
}
radius = sqrt((radius*radius) / 4 + ((0.5 - fx)*(0.5 - fx)));
sides *= 2;
}
OutStream << "Sides = " << sides;
OutStream << " Inscribed Perimeter = " << std::setprecision(15) << inscribed_perimeter;
OutStream << " Circumscribed Perimeter = " << std::setprecision(15) << circumscribed_perimeter;
return 0;
}
Mar 21, 2017 at 8:35pm UTC
You are only writing to the file on lines 46-48. If you want to output thosethe same variables for each iteration of the loop you could move those lines inside the loop.
Last edited on Mar 21, 2017 at 8:37pm UTC
Topic archived. No new replies allowed.