For Loop help

Apr 20, 2013 at 3:09am
I'm trying to to get the individual strings from ATG to TAA, ATG to TGA and ATG to TAG.

Input File:
XATGXXXXXTAAXYATGXXXXXTGAXZATGXXXXXTAGX

Goal:
ATGXXXXXTAA
ATGXXXXXTGA
ATGXXXXXTAG

EDIT ----
So I got the code to print each individual string, but I'm stuck on outputting each different string to a file (number1.txt, number2.txt etc...)
Can someone help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    
  for (int i=0;now.find("ATG",i)<now.size();i++) {
        cout << "Index: " << i << endl;
      
      if (TAA<TGA && TAA<TAG) {
          cout << "Found TAA: " << ATG_TAA << endl; 
         i = TAA;
         TAA = now.find ("TAA", TAA+1);
       }
      else if (TGA<TAA && TGA<TAG) {
          cout << "Found TGA: " << ATG_TGA << endl; 
          i = TGA;
          TGA = now.find ("TGA", TGA+1);

      }
      else if (TAG<TAA && TAG<TGA) {
          cout << "Found TAG: " << ATG_TAG << endl; 
          i = TAG;
          TAG = now.find ("TAG", TAG+1); 

      }
Last edited on Apr 20, 2013 at 7:18am
Apr 20, 2013 at 3:57am
You may have to use a while loop here, instead of a for loop... If I recall, you cannot manipulate the index beyond the increment that it undergoes after reach completion of the loop. If you use a while loop:

1
2
3
4
5
6
int i;
while(i<=number)
{
          //statements
          i++;
}

you can at least manipulate "i" from within the loop itself, which is what you are trying to do here.
Last edited on Apr 20, 2013 at 3:58am
Apr 20, 2013 at 6:01am
Of course you can modify the counter within for's body just like in while.
While is more logical though.

C11 has regex.
Last edited on Apr 20, 2013 at 6:09am
Apr 22, 2013 at 12:31pm
The revised version scares me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  for (int i=0;now.find("ATG",i)<now.size();i++) {
        cout << "Index: " << i << endl;
      
      if (TAA<TGA && TAA<TAG) {
          cout << "Found TAA: " << ATG_TAA << endl; 
         i = TAA;
         TAA = now.find ("TAA", TAA+1);
       }
      else if (TGA<TAA && TGA<TAG) {
          cout << "Found TGA: " << ATG_TGA << endl; 
          i = TGA;
          TGA = now.find ("TGA", TGA+1);

      }
      else if (TAG<TAA && TAG<TGA) {
          cout << "Found TAG: " << ATG_TAG << endl; 
          i = TAG;
          TAG = now.find ("TAG", TAG+1); 

      }

We have no idea what the values of TAA, TAG, TGA, and ATG_T?? are when the line 4 is evaluated for the first time.

If the intention is to read the string and then write each found substring into different, sequentially numbered file, then one could make a function that takes a counter and string, opens file, writes string, and closes file.
Topic archived. No new replies allowed.