-
September 21st, 2009, 02:47 AM
#1
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()
-
September 21st, 2009, 06:02 AM
#2
Re: Transferring output to txt file
1) You open the file inside of the while loop ... this empties the file and then
writes writes the last pair of random numbers to it (100 times).
2) open the file before the while loop
3) what is the purpose of this for loop ?
Code:
for(int k = 0; k<100;k ++k)
{
file<<x_value<<","<<y_value<<"\n";
}
This just writes the same pair of numbers 100 times.
-
September 21st, 2009, 09:19 AM
#3
Re: Transferring output to txt file
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.
-
September 21st, 2009, 09:37 AM
#4
Re: Transferring output to txt file
Originally Posted by royalstatus
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.
What numbers do you want written to the file ?
I assumed that they were
Code:
x_value = static_cast<double>(rand())/RAND_MAX;
y_value = static_cast<double>(rand())/RAND_MAX;
-
September 21st, 2009, 09:38 AM
#5
Re: Transferring output to txt file
Originally Posted by royalstatus
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.
-
September 21st, 2009, 09:42 AM
#6
Re: Transferring output to txt file
Originally Posted by Philip Nicoletti
What numbers do you want written to the file ?
I assumed that they were
Code:
x_value = static_cast<double>(rand())/RAND_MAX;
y_value = static_cast<double>(rand())/RAND_MAX;
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
-
September 21st, 2009, 09:55 AM
#7
Re: Transferring output to txt file
Originally Posted by royalstatus
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?
-
September 21st, 2009, 10:05 AM
#8
Re: Transferring output to txt file
Originally Posted by GCDEF
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
-
September 21st, 2009, 10:13 AM
#9
Re: Transferring output to txt file
Originally Posted by royalstatus
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.
-
September 21st, 2009, 11:08 AM
#10
Re: Transferring output to txt file
and how do i create an array to make sure 1000 different x_values and y_values are listed?
-
September 21st, 2009, 11:21 AM
#11
Re: Transferring output to txt file
I still think the simplest solution is do
1) remove the for loop:
Code:
for(int k = 0; k<100;k ++)
(but keep the writing out of x_value and y_value)
2) move the following lines up in the code:
Code:
ofstream.file;
file.open("monster.txt");
Either before the while loop or before the "for(int i=0;i<n_darts;i++)" loop,
depending on what you want.
3) if you go with an array, you should still move those lines before the while
loop, and just print out the array once when you have all the values.
-
September 21st, 2009, 11:29 AM
#12
Re: Transferring output to txt file
A few times you mentioned: 1000 values
I don't see anything in your code that corresponds to that.
-
September 21st, 2009, 12:46 PM
#13
Re: Transferring output to txt file
Originally Posted by Philip Nicoletti
A few times you mentioned: 1000 values
I don't see anything in your code that corresponds to that.
ok. thanks a lot. i figured it out. thanks for your help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|