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).
Edit :
My sequence checker looks like this now:
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" << "\n" << "\n";
i++;
}
and my counter looks like this:
// Count the number of Cs and Gs
count_c = 0;
count_g = 0;
for (i = 0; i < st.size() ; i++) {
if ((st[i] == 'c') || (st[i] == 'C'))
count_c++;
else if ((st[i] == 'g')|| (st[i] == 'G'));
count_g++;
}
cout << "Number of instances of C = " << count_c;
cout << "Number of instances of G = " << count_g;
You should remove “;” after that if operator:
Now it’s not doing anything.
You should use “<” instead of “<=” to avoid wrong indexing of string array (string of size “size” means that indexes are from 0 to size – 1)
If you already checked that symbol is not one of c,C,g,G,t,T,a,A, you don’t need to check it again, so if (st[i] == c,C,G,t,T,a,A) is useless.
But even with theese corrections your code is logically wrong.