Need Help with DNA String Validation

Write your question here.

I must verify that a string of any length contain only valid characters:G,A,T,C. If any characters do not contain G or A or T or C keep asking the user for input until it is valid.

Once the DNA string is valid input it as the strand string for a variable of class dna:

This is the code in my main function where the user inputs the string that goes into the "dna" class and the ID of the DNA:

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;i<SIZE;i++){
   cout<<" Please insert the ID of DNA sample: "<<i<<endl;
   cin>>eyeD;
   STRAND[i].seteyeD(eyeD);

   cout<<" Please insert the strand for DNA sample "<<i<<endl;
   cin>>sample;
   do{
    cout<<" Please REINSERT the strand for DNA sample "<<i<<endl;
    cout<<"Must be G or A or T or C\n";
    cin>>sample;
   }
   while(checkDNA(sample)==false);
   STRAND[i].setsample(sample);


   cout<<"STRAND "<<i<<" length:"<<STRAND[i].getsample().length()<<endl;
   cout<<"STRAND "<<i<<" sample:"<<STRAND[i].getsample()<<endl;
   dnaout <<STRAND[i].geteyeD()<<endl;
   dnaout <<STRAND[i].getsample()<<endl;
   }  


This is the function that verifies that the initial string inputted by the user is valid. ie. It contain either G or A or T or C.
1
2
3
4
5
6
7
8
9
10
11
bool checkDNA(string strand){
    bool check=true;
    int SIZED=strand.length();
    for (int i=0;i<SIZED-1;i++){
        if ((strand.at(i) != 'A') && (strand.at(i) != 'T') && (strand.at(i) != 'G') && (strand.at(i) != 'C')){
            check = false;
            break;
        }
    }
    return check;
}



I cannot get it to move on to displaying the ID and the strand of the DNA sample. It just declared the ID and Strand for all DNAs until the program closes on it's own.

Thanks to anyone who can help. I have tried everything I can to fix it and have tried many different ways recommended online for this sort of problem.

If anyone can fix with piece of code for me it would be greatly appreciated. This is the final project for my class and I have the functions for everything to work, I just need to ensure that the DNA strands are actually valid.

THANK YOU.
I don't know about other users on this forum, but for me, there's not enough info to help you.
I'll start with some questions:

1.) When you say:
I cannot get it to move on to displaying the ID and the strand of the DNA sample.

You mean, you're unable to exit the do-while loop? Or, do you mean it doesn't print? What exactly is dnaout?

2.) When you say:
It just declared the ID and Strand for all DNAs until the program closes on it's own.

What do you mean?

3.) When you say:
I have the functions for everything to work, I just need to ensure that the DNA strands are actually valid.

You mean, you need to print these things to ensure their validity, as opposed to, for instance, using your checkDNA function?
Last edited on
I'm sorry for the confusion.

For now my only problem is getting the program to continue asking the user to input the string "strand" until it is valid. ie. It only contains G A T C and no other letter. For example: GGAAATTTCCCCATGTG is valid and GGAATTYU is not because Y and U are not valid.

I made a do while loop in my main function that calls on my checkDNA function.

Something is wrong with my implementation. I don't know what. It all looks fine to me, but I'm only a beginner.


Here is the loop I am trying to get to work in my main.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Array of variable of class dna
dna STRAND[SIZE];
int eyeD;
string sample;
   for(int i=0;i<SIZE;i++){
   cout<<" Please insert the ID of DNA sample: "<<i<<endl;
   cin>>eyeD;
   STRAND[i].seteyeD(eyeD);

   cout<<" Please insert the strand for DNA sample "<<i<<endl;
   cin>>sample;
   do{
    cout<<" Please REINSERT the strand for DNA sample "<<i<<endl;
    cout<<"Must be G or A or T or C\n";
    cin>>sample;
   }
   while(checkDNA(sample)==false);
   STRAND[i].setsample(sample);


And the verification function that I am trying to get to work properly:

1
2
3
4
5
6
7
8
9
10
11
bool checkDNA(string strand){
    bool check=true;
    int SIZED=strand.length();
    for (int i=0;i<SIZED-1;i++){
        if ((strand.at(i) != 'A') && (strand.at(i) != 'T') && (strand.at(i) != 'G') && (strand.at(i) != 'C')){
            check = false;
            break;
        }
    }
    return check;
}


Here is the entire main.cpp if this helps clarify things better.

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
#include<iostream>
#include<string>
#include "dna.h"
#include<cstring>
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;


int main()
{
const int SIZE=10;
ofstream dnaout;
ifstream dnain;
dnaout.open("GeneOut.txt");
dnain.open("GeneoOut.txt");
dna STRAND[SIZE];
string checker;
int eyeD;
string sample;
   for(int i=0;i<SIZE;i++){
   cout<<" Please insert the ID of DNA sample: "<<i<<endl;
   cin>>eyeD;
   STRAND[i].seteyeD(eyeD);

   cout<<" Please insert the strand for DNA sample "<<i<<endl;
   cin>>sample;
   do{
    cout<<" Please REINSERT the strand for DNA sample "<<i<<endl;
    cout<<"Must be G or A or T or C\n";
    cin>>sample;
   }
   while(checkDNA(sample)==false);
   STRAND[i].setsample(sample);


   cout<<"STRAND "<<i<<" length:"<<STRAND[i].getsample().length()<<endl;
   cout<<"STRAND "<<i<<" sample:"<<STRAND[i].getsample()<<endl;
   dnaout <<STRAND[i].geteyeD()<<endl;
   dnaout <<STRAND[i].getsample()<<endl;
   }

dnaout.close();

    return 0;
}

Ok. Thanks for clearing things up a bit. However, you still haven't said exactly what the problem is. All you've said is that something is wrong, but what is it that makes you think that?

Anyways, I've re-written the checkDNA() function to be a bit more straight-forward (I think). It's pretty much the same.
Note, this doesn't account for lower case letters. I'm not a fan of hardcoding these kinds of constants, but whatever.

1
2
3
4
5
6
7
8
9
bool checkDNA(const std::string& strand) {
	unsigned short size = strand.size();
	for(unsigned short i=0; i<size; ++i) {
		if(strand[i]!='A' && strand[i]!='T' && strand[i]!='G' && strand[i]!='C') {
			return false;
		}
	}
	return true;
}
Topic archived. No new replies allowed.