0

There are numbers ranging from 1-4 in a file called "data.txt" (ex. 2 1 2 4 1 3...). I need to read these numbers and create strings of 100 numbers each. After which I need to count how many 1s, 2s 3s, and 4s there are in each string. This is what I have so far:

//structure
struct DNAnt
{
    int a, c, g, t;
    string strand;
};
void data2DNA();

//main
int _tmain(int argc, _TCHAR* argv[])
{
    data2DNA();

    cin.get();
    return 0;
}

//function 
void data2DNA()
{
    DNAnt DNAstrand[100];
    ifstream inFile;
    int numbers;
    inFile.open("data.txt");
    if(inFile.is_open())
    {
        while(!inFile.eof())
        {
            inFile >> numbers;  
        }
    }
    else
        cout << "ERROR!";

    inFile.close();
}

All this program does at the moment is read all the data from the file. I don't know how to continue from here.

Tarun
  • 19
  • 1
  • The while loop should be where you get the strings of 100 digits, but it's not doing anything right now. As for the rest, depends on what you intend to do with the counts and the 100-digit strings. – Nuclearman Dec 09 '15 at 00:25
  • The `numbers` variable looks more like how you want your "storage" to behave. Try changing the type of `numbers` into a container (like `std::vector`) and moving from there. – Brian Rodriguez Dec 09 '15 at 00:26
  • `while(!inFile.eof())` almost never works. [Read more here](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). In this case it is wrong and will cause you to read past the end of the file and place some unknown value in numbers. – user4581301 Dec 09 '15 at 01:57

2 Answers2

0

Very often in C++ you need to use a loop to repeat an action some number of times. In your case I believe you could benefit from a for loop. Here is an example of a loop that will read 100 integers and store each one to a location in the array that you have near the start of your main:

int i;
for(i = 0; inFile && i < 100; ++i) { //i will range from 0 to 99
    inFile >> DNAstrand[i];
}
//now use DNAstrand to do stuff

The reason I say inFile && is to make sure that inFile is not at eof.

Kevin Tindall
  • 330
  • 5
  • 16
0

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 inFiles 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.

user4581301
  • 29,019
  • 5
  • 26
  • 45