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
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)
hi, the random numbers (x_value, y_value) are generated within the while loop. So opening the file before the while loop doesn't give me the numbers i need. do you know what i need to do? The for loop doesnt work just like you said.
hi, the random numbers (x_value, y_value) are generated within the while loop. So opening the file before the while loop doesn't give me the numbers i need.
hi, the random numbers (x_value, y_value) are generated within the while loop. So opening the file before the while loop doesn't give me the numbers i need. do you know what i need to do? The for loop doesnt work just like you said.
And why would it? It variable it outputs is never modified in the loop. If you want different output, you either need to output a different variable, or modify the one you're outputting with each iteration of the loop.
i want it written 1000 times. 1000 different x_values and 1000 different y_values. And that's why i thought the for loop will do it. But it prints out only the first x_value and y_value
i want it written 1000 times. 1000 different x_values and 1000 different y_values. And that's why i thought the for loop will do it. But it prints out only the first x_value and y_value
Code:
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";
}
The first two lines set x_value and y_value to a random number.
The for loop outputs those two numbers 100 times. Do you understand why?
i think its because this line tells it to do so
file<<x_value<<","<<y_value<<"\n";
and it does it again and again when it goes to the beginning of the loop
As I said, it's because x_value and y_value never change in the loop. You need to store your values in an array then iterate over the array, or assign them new values in the loop.
Bookmarks