Transferring output to txt file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Transferring output to txt file

  1. #1
    Join Date
    Sep 2009
    Posts
    8

    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()

  2. #2
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057

    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?

  4. #4
    Join Date
    Sep 2009
    Posts
    8

    Re: Transferring output to txt file

    Quote Originally Posted by GCDEF View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center