File writing

Hey forum! I have looked at numerous threads on here, but could not find any that would fix my problem so I thought I would make my own thread. I want to run a number through an algorithm then have the number and the count of how many times it ran through write out to a file so I can look for patterns. Here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
//  main.cpp
//  random
//
//  Created by Matthew Shoppas on 2/5/13.
//  Copyright (c) 2013 Matthew Shoppas. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>

class CollotzConjecture{
    
private:
    int number, count;
public:
    int compute(int x, int & count);
    
};


using namespace std;

int main()
{
    int x, count=0, y, i;
    ofstream file("collatz_write.txt", ios::app|ios::out);
    
    // Users/matthewshoppas/Documents/Math/collatz_write.txt
    
    for (i=0; i<100; i++) {
    
    file.open("collatz_write.txt"/*, ios::app|ios::out*/);
    
    CollotzConjecture first;
   
    srand((unsigned)time(0));
    
    x=rand() % 1000 + 5;
    y=x;
    
    while (x!=1)
    {
        
        x= first.compute(x, count);
        
    }
    
    cout<<"The number "<<y<<" goes through the Collatz Conjecture "<<count+1<<" times"<<endl;
    file<<y<<"----"<<count+1<<endl;
    
    file.close();
        
    }
    
    return 0;
}

int CollotzConjecture::compute(int x, int & count)
{
    if (x%2==0)
    {
        x=x/2;
    }
    
    else{
        x=3*x+1;
    }
    cout<<x<<endl;
    count++;
    
    return x;
    
}
Your code looks fine to me. What is not working about it?
I go to open the file and nothing is there.
Remove line 35 (it opens the file a second time):
file.open("collatz_write.txt"/*, ios::app|ios::out*/);
1
2
3
4
5
6
7
8
    // open the file.
   ofstream file("collatz_write.txt", ios::app|ios::out);
    
    for (i=0; i<100; i++) {
    // attempt to open the already open file.
    file.open("collatz_write.txt"/*, ios::app|ios::out*/); 

    // file is now in an error state. 


If you successfully open a file, any subsequent attempts to open the file without an intervening call to close will fail, putting the file stream into an error state. All subsequent use of the file stream will fail, since the error state is never cleared.

If you open a file for output with the default flags, the file will be truncated.

I would suggest opening the file once before the for loop is entered and using it to write data within the body of the for loop. You can then let the destructor close the file stream when it goes out of scope.


so this is what I just tried to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    ofstream file("collatz_write.txt", ios::app|ios::out);
    
    file<<"hey there";
    file.close();
    
    /*file.open("collatz_write.text", ios::app|ios::out);
    
    // /Users/matthewshoppas/Documents/Math/collatz_write.txt
    
    CollotzConjecture first;
   
    srand((unsigned)time(0));
    
    x=rand() % 1000 + 5;
    y=x;
    
    while (x!=1)
    {
        
        x= first.compute(x, count);
        
    }
    
    cout<<"The number "<<y<<" goes through the Collatz Conjecture "<<count+1<<" times"<<endl;
    file<<y<<"<--------->"<<count+1<<endl;
    
    file.close();*/
    
    return 0;
}


But there is still nothing in the file...I am using xcode on a Mac if that makes any difference. I also included the file in the xcode session.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    ofstream file("collatz_write.txt", ios::app|ios::out);
    
    file<<"hey there";
    file.close();
    
    /*file.open("collatz_write.text", ios::app|ios::out);*/
    
    // /Users/matthewshoppas/Documents/Math/collatz_write.txt
    
    CollotzConjecture first;
   
    srand((unsigned)time(0));
    
    x=rand() % 1000 + 5;
    y=x;
    
    while (x!=1)
    {
        
        x= first.compute(x, count);
        
    }
    
    cout<<"The number "<<y<<" goes through the Collatz Conjecture "<<count+1<<" times"<<endl;
    file<<y<<"<--------->"<<count+1<<endl;
    
    file.close();
    
    return 0;
Is that not exactly what I have? Haha and it was not working before.
Topic archived. No new replies allowed.