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

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 2000
    Location
    West Virginia
    Posts
    7,560

    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.

  3. #3
    Join Date
    Sep 2009
    Posts
    8

    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.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,560

    Re: Transferring output to txt file

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

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Transferring output to txt file

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

  6. #6
    Join Date
    Sep 2009
    Posts
    8

    Re: Transferring output to txt file

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

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Transferring output to txt file

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

  8. #8
    Join Date
    Sep 2009
    Posts
    8

    Re: Transferring output to txt file

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

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Transferring output to txt file

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

  10. #10
    Join Date
    Sep 2009
    Posts
    8

    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?

  11. #11
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,560

    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.

  12. #12
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,560

    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.

  13. #13
    Join Date
    Sep 2009
    Posts
    8

    Re: Transferring output to txt file

    Quote Originally Posted by Philip Nicoletti View Post
    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
  •  


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