#include <iostream>
#include <string>
#include <fstream>
#define SignatureBlock "~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\nNicholas Whitmore\nnicholas.whitmore@maine.edu\nCIS 215, C++ Application Programming\n~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~ ~*~\n"
usingnamespace std;
int main()
{
string gene;
int i = 0;
int sum = 0;
cout << SignatureBlock << endl;//implement signature block
cout << "\nEnter the name of the file that contains the genome including path:\n" << endl;//prompt for file path
string filePath; //file path variable
getline(cin, filePath); //user input
ifstream myfile(filePath.c_str());//opens file at filePath
string genome;
if (myfile.is_open())//check file path
{
while (getline(myfile,genome))
{
cout<<"Data is: "<< genome << endl;//output the data within the file
}
myfile.close();//close the file
}
else cout << "Unable to open file" << endl;//if file path is incorrect
cout << "Genes are as follows: " <<endl;
//for the beginning of the string to the end
for(i = genome.find("ATG",0); i!=string::npos; i=genome.find("ATG",i))
{
sum++;
i++;
}//end for
cout<<i<<endl;
cout<<sum<<endl;
cin.get();
return 0;
}//end main
Enter the name of the file that contains the genome including path:
C:\Users\Whitmore\Desktop\UMA work\CIS 215\MODULE 4\Content\dna_sample.txt
Data is: ACAAGATGCCATTGTCCCCCGGCCTCCTGCTGCTGCTGCTCTCCGGGGCCACGGCCACCG
Data is: CTGCCCTGCCCCTGGAGGGTGGCCCCACCGGCCGAGACAGCGAGCATATGCAGGAAGCGG
Data is: CAGGAATAAGGAAAAGCAGCCTCCTGACTTTCCTCGCTTGGTGGTTTGAGTGGACCTCCC
Data is: AGGCCAGTGCCGGGCCCCTCATAGGAGAGGAAGCTCGGGAGGTGGCCAGGCGGCAGGAAG
Data is: GCGCACCCCCCCAGCAATCCGCGCGCCGGGACAGAATGCCCTGCAGGAACTTCTTCTGGA
Data is: AGACCTTCTCCTCCTGCAAATAAAACCTCACCCATGAATGCTCACGCAAGTTTAATTACA
Data is: GACCTGAA
Genes are as follows:
-1
0
First off, please ignore the
Genes are as follows:
part, my code if far from being complete. My question is, where is my error that is giving me 0instead of 5I feel that it is because of the \n white space within the .txt file. As you can see from the code, I am trying to find all instances of "ATG" within the dna_sample.txt file. Thanks in advance :)
UPDATE: If I remove all of the \n white space, I get the answer I need. unfortunately, I must use the file the way it is and not the way I edited it to be. This means that the white space is screwing things up. Help please!
-1 is the int value of std::string::npos. This is what std::string::find() will return if the string is not found.
That's because you write the first line to genome, then write the second line to genome (first line is lost), then write the third line to genome (second line is lost)... until you write the last line to genome (all other lines have been lost).
You might want to do it this way:
1 2 3 4 5 6
std::string line;
while (getline(myfile,line))
{
cout<<"Data is: "<< line << endl;//output the data within the file
genome += line; // append the new data to our genome.
}