A program where user enters a string and the program counts the instances of the letters

Posted by user1865183 on Stack Overflow See other posts from Stack Overflow or by user1865183
Published on 2012-11-30T04:35:46Z Indexed on 2012/11/30 5:03 UTC
Read the original article Hit count: 195

Filed under:
|
|
|
|

This is the first C++ program I have ever written and I'm having trouble understanding the order in which operands must be put in. This is for a class, but it looks like I'm not supposed to use the homework tag. Sorry if I'm doing this wrong.

This is my input

// Get DNA string

string st;
cout << "Enter the DNA sequence to be analysed: ";
cin >> st;

This seems to work ok, but I thought I would include it incase this is what I'm doing wrong.

This is what I have so far to check that the input is exclusively C,T,A, or G. It runs through the program and simply prints "Please enter a valid sequnce1, please enter a valid sequence2, ... ect. I'm sure I'm doing something very stupid, I just can't figure it out.

// Check that the sequence is all C, T, A, G

while (i <= st.size()){
if (st[i] != 'c' && st[i] != 'C' && st[i] != 'g' && st[i] != 'G' && st[i] != 't' && st[i] != 'T' && st[i] != 'a' && st[i] != 'A');
    cout << "Please enter a valid sequence" <<
    i++;
else if (st[i] == c,C,G,t,T,a,A)
    i++;

The second half of my program is to count the number of Cs and Gs in the sequence

for (i < st.size() ; i++ ;);
for (loop <= st.size() ; loop++;)
    if (st[loop] == 'c')
    {
    count_c++;
    }
    else if (st[loop] == C)
    {
    count_c++;
    }
    else if (st[loop] == g)
    {
    count_g++;
    }
    else if (st[loop] == G);
    {
    count_g++;
    }


cout << "Number of instances of C = " << count_c;
cout << "Number of instances of G = " << count_g;

It seems like it's not looping, it will count 1 of one of the letters. How do I make it loop? I can't seem to put in endl; anywhere without getting an error back, although I know I'll need it somewhere.

Any help or tips to point me in the right direction would be greatly appreciated - I've been working on this code for two days (this is embarrassing to admit).

© Stack Overflow or respective owner

Related posts about c++

Related posts about string