Start with a very simple program that reads numbers from a file.
Just read them. Do not interpret them because there is no point to trying to do anything with these numbers until you can prove that they are being read correctly. All additional code will do is muddy the waters of ensuring you are reading correctly and hide the location of bugs from you.
vector<int> readFile()
{
vector<int> numbers;
Why vector? Because it is the easiest way to store an unknown number of entries. Don't play around with more complicated structures at this point and don't try to roll your own. If the instructor says, "No vector" oh well. But that's for what you hand in. What you use to prove the algorithm works is another story.
ifstream inFile("data.txt");
This also opens the file so you don't have to do it later
if(inFile.is_open())
{
int number;
while(inFile >> number)
This reads into an int. If it can't read an int for any reason, it stops and leaves the loop. If the file is poorly formatted and contains data that cannot be converted, we're outta here. If it hits the end of the file, we're outta here.
{
numbers.push_pack(number);
Otherwise, we put the number we read into the vector.
}
}
else
{
cout << "ERROR opening file";
)
Don't need to close the file. When we hit the end of the function inFile
s destructor will fire and do it for us.
return numbers;
And send the vector back to the caller so they can use it however they see fit. There is a whole tot of interesting stuff that can happen here to keep the vector from being copied as it is returned, so don't worry about it.
}
All together:
void data2DNA()
{
vector<int> numbers;
ifstream inFile("data.txt");
if(inFile.is_open())
{
int number;
while(inFile >> number)
{
numbers.push_pack(number);
}
}
else
{
cout << "ERROR opening file";
)
return numbers;
}
Now that you have the data in, you can get on with the real job of turning it into a string of nucleic acids.
For this part I'm going to recommend an array
const char tags[] = {'-', 'A', 'C', 'G', 'T'};
Indexes 1 though 4 are A, C, G, and T, so tags[2]
will provide 'C'
. This makes translating from number to tag really, really easy.
Once you have a tag, you can append it to the DNA string.