Output File issue!

Hello all, In my count function i want it to output the 1st and last index on to the same file that is being used on my main function, every time i run this program the cout from the count function does not go through any ideas?

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
  #include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
// This function transforms the DNA
char replace(char c)
{
    if (c == 'A') return 'T';
    if (c == 'T') return 'A';
    if (c == 'G') return 'C';
    if (c == 'C') return 'G';
    return c;
}
//This function counts how many times the string "TTTT" appears
int Count( const string & str, 
           const string & obj ) {
    int n = 0;
    ofstream outfile;
    string ::size_type pos = 0;
    while( (pos = obj.find( str, pos )) 
                 != string::npos ) {
        outfile<<pos<<endl;//This should output 1st index it on the same file 
    	n++;
    	pos += str.size();
    	outfile<<pos<<endl;//This should output last index on the same file
    }
    return n;

}
int main()
{
  
    ifstream infile("dna.txt");
    
     if (infile.fail())
     { cout<<"Error File cannot be open!"<<endl;
       exit(1);
      }
    ofstream outfile("dna2.txt");//Output file on main function
    
     if (outfile.fail())
     { cout<<"Error File cannot be open!"<<endl;
       exit(1);
      }
   
    string dna;
    size_t nPos = dna.find( "TTTT", 0 );
    while (infile >> dna) { 
        float gc;
        float content;
        float gccontent;
        transform(dna.begin(), dna.end(), dna.begin(), ::replace);
        dna=string(dna.rbegin(), dna.rend());
        outfile << dna << endl;
        int n = Count( "TTTT", dna );//Where the function is declared
        outfile << n << endl;//Outputs how many times the string "TTTT" appears
        size_t g=count(dna.begin(), dna.end(),  'G');
        size_t c=count(dna.begin(), dna.end(),  'C');
        size_t t=count(dna.begin(), dna.end(),  'T');
        size_t a=count(dna.begin(), dna.end(),  'A');
        gc=c+g;
        content=gc/dna.length();
        gccontent=content*100;
        outfile<<"The GC content for this strand is at "<<gccontent<<"%"<<endl;
}
    system ("pause");
}
Show example of input.

Also, do you need count before replacement or after? Currently it access modified string.
Topic archived. No new replies allowed.