The aim of the program which i am writing is to convert strings of DNA. The DNA is in a text file and represented by the letters A, T, C, and G. Reading in the file and outputting the converted DNA is easy. However i want to save the converted data in another array and then to a text file. Here is my code

#include <iostream>
#include <fstream>
#include "dna.h"
using namespace std;

int main()
{
	fstream ins;
	char name[128];
	char savefile[128];

	cout << "Enter file name to read: " << endl;
	cin >> name;

	ins.open(name, ios::in);

	if (ins.good())
	{
		reverse(ins);
	}
	else
	{
		cout << "error" << endl;
	}

	ins.close();

	cout << "Enter file to save to: " << endl;
	cin >> savefile;

	ins.open(savefile, ios::in);

	if (ins.good())
	{
		write(ins);
	}
	else
	{
		cout << "error" << endl;
	}

	ins.close();
	
	return 0;
}
#include <iostream>
#include <fstream>
#include "dna.h"
using namespace std;

ecoli sequence;
ecoli save;
int reverse(fstream& ins)
{
	int i = 0;

	while ((ins.good()) && (i < MAX))
	{
		ins.ignore(0,'\n');
		ins >> sequence.dna[i];
		
		if (ins.good())
		{
			i++;
		}
	}

	for (int i = MAX - 1; i >= 0 ; i--)
	{
		if (sequence.dna[i] == 'A')
		{
			cout << 'T';
			save.dna[i] = "T"; //i though this mwould work but it dosnt.
		}
		else if (sequence.dna[i] == 'T')
		{
			cout << 'A' ;
			save.dna[i] = "A";
		}
		else if (sequence.dna[i] == 'C')
		{
			cout << 'G';
			save.dna[i] = "G";
		}
		else if (sequence.dna[i] == 'G')
		{
			cout << 'C';
			save.dna[i] = "C";
		}
	}
	cout << endl;
	
	return 0;

}

int write(fstream& ins)
{

}
#include <fstream>
using namespace std;

const int MAX = 3000;

struct ecoli
{
        char dna[MAX];
};

int reverse(fstream&);
int write(fstream&);

how can i insert the output into a new array so i can save it to another file?

The reason the line you commented isn't working is because save.dna[i] is of type char, but "T" is of type char* (I believe, maybe it depends on the compiler?). Try changing it to 'T'

Thanx, i will try that.

First thing I noticed when you opened the save file was

ins.open(savefile, ios::in);

What does ios::in mean? [the question is a hint]

yeh i noticed that to when i got up to that part lol. I changed it to ios::out, but the problem i have now is that after it saves to the file, i cannot read the file with a text editor. Here is the save function.

int write(fstream& ins)
{
		
		for (int i = MAX - 1; i >= 0 ; i--)
		{
		            ins<< save.dna[i];
		}	
	
	return 0;
}

I'm assuming the third file posted is dna.h, the middle file posted is dna.cpp and the first file posted is the driver file.

Now withing dna.h you declare a user defined type called ecoli. I suspect you want ecoli to have 1 data member, called dna, and 2 functions to manipulate the data, one called reverse() and one called write(). If that's so then move the declaration of reverse() and write into the body of the declaration of ecoli:

struct ecoli
{
   const int MAX = 3000;
   char originalDNAsequence[MAX];
   char reversedDNAsequence[MAX];
   int reverse(fstream &);
   int write(fstream &);
};

Then within dna.cpp preface the definition of reverse() and write() with the scope operator like this:

int ecoli::write(fstream & ins)

Remove declaration of the ecoli objects sequence an save from dna.cpp. In reverse, read the file contents into originalDNAsequence and then reverse it into reversedDNAsequence.

In main() declare a single ecoli object called bug and read the file into bug by calling the reverse() method like so:

bug.reverse()

and write the reversed dna sequence of bug to a file by calling bug like so:

bug.write();

where write() writes reversedDNAsequence to the desired file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.