Transferring output to txt file
Im trying to get the output transferred to a txt file. Each time i run it the way it is, it only shows a repetition of the first generated random number. I want all the random x_values and y_values to be shown in the text file. Can someone please tell me what i'm doing wrong? I've been doing this for about 3hrs, and I havent been able to correct my mistake. Whenever I use the cout function (cout << " x = " <<x_value<< " y = " <<y_value<< endl;), it prints out all the random numbers. But i want it to be in a text file. Thanks
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <cstdlib>
using namespace std;
void main(){
double x_value, y_value;
long int hit,n_darts=1;
//generating the seed value for the random numbers
srand(time(0));
while(n_darts!=0){
cout<<"\nThis program estimates Pi by the Monte Carlo method.\n";
cout<<"How many darts do you want to throw? (0 to quit) ";
cin>>n_darts;
if (n_darts!=0)
{
//reset the hit variable
hit=0;
for(int i=0;i<n_darts;i++)
{
//generates a random value of x and y between 0
//and 1 with the precision of a double variable
x_value = static_cast<double>(rand())/RAND_MAX;
y_value = static_cast<double>(rand())/RAND_MAX;
ofstream.file;
file.open("monster.txt");
//for loop to write the values to the file
for(int k = 0; k<100;k ++){
file<<x_value<<","<<y_value<<"\n";
}
cout<<"Printed numbers to file monster.txt\n";
//if the x and y values are inside a circle of
//unit 1 then we increase the hit counter
if (((0.25*x_value*x_value)+(0.25*y_value*y_value))<=0.25) hit++;
}
//using the setprecision and setioflags functions to
//set the precision to 10 digits and to show
//trailing zeros we estimated the value of pi
//by multiplying the number of hits by 4 and dividing
//by the numbers of thrown darts.
cout<<"There were "<<hit<<" hits in the circle \n";
cout<<"The estimated value of pi is: "
<<setiosflags(ios::fixed|ios::showpoint)
<<setprecision(10)
<<(hit*4)/static_cast<double>(n_darts)<<"\n";
std::freopen("output.txt", "w", stdout);
}//if (darts!=0)
}//end of while(n_darts!=0)
}//end of main()
Re: Transferring output to txt file
Hi,
Try running it in a debugger, and see what happens when you loop over the 100 writes to the file. You should see the same values being written 100 times, since there's nothing else happening in that loop.
Also, have a look at how to use CODE tags to display source code in any posts, it's much easier to read.
Alan
Re: Transferring output to txt file
Code:
for(int k = 0; k<100;k ++)
{
file<<x_value<<","<<y_value<<"\n";
}
Why would you expect that to do anything but write the same number 100 times?
Re: Transferring output to txt file
Quote:
Originally Posted by
GCDEF
Code:
for(int k = 0; k<100;k ++)
{
file<<x_value<<","<<y_value<<"\n";
}
Why would you expect that to do anything but write the same number 100 times?
i generated random numbers for x_value and y_value like a few lines above that code, so i thought it would print out all the random numbers to the text file