Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have to write a code that compares three text files and i cant for the life of me find out why this wont print anything:

#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
using namespace std;

int main (int argc, char *argv[])
{
	ifstream mousefile;
	mousefile.open(argv[1]);
	string mouse_dna;
	getline(mousefile, mouse_dna);
	
	ifstream humanfile;
	humanfile.open(argv[2]);
	string human_dna;
	getline(humanfile, human_dna);
	
	ifstream unknownfile;
	unknownfile.open(argv[3]);
	string unknown_dna;
	getline(unknownfile, unknown_dna);
	
	int len = mouse_dna.size();
	int mouseDistance = 0, humanDistance = 0;
		
	for(int i=0; i<len; i++)
		if(mouse_dna[i] != unknown_dna[i])
			mouseDistance++;
			return mouseDistance;
	for(int i=0; i<len; i++)
		if(human_dna[i] != unknown_dna[i])
			humanDistance++;
			return humanDistance;
		
	
	double similarity_scoreH = (len - humanDistance) / len; 
	double similarity_scoreM = (len - mouseDistance) / len; 
	cout << "MouseCompare = " << similarity_scoreM << endl;
	cout << "HumanCompare = " << similarity_scoreH << endl;
	
	if (similarity_scoreH == similarity_scoreM)
		cout << "identity cannot be determined" << endl;
	else if (similarity_scoreH > similarity_scoreM)
		cout << "human" << endl;
	else if (similarity_scoreM > similarity_scoreH)
		cout << "mouse" << endl;
	

}

It compiles properly, and doesn't give any errors, but when i rut it as:
./DNA mouseDNA.txt humanDNA.txt unknownDNA.txt
it still does nothing.
I appreciate any help. Thanks!
Posted

Start with the debugger: put a breakpoint on the first line of the main function, and run you program. When it hits the breakpoint, single step through the application, looking at all the variables involved in each line, and working out exactly what you expect the instruction to do before you execute it. Then check to make sure it did it. At some point, these two will differ, and that should start you towards finding out why.

Debugging is a skill: and you only develop it by practising and using it!
And we can't do that for you: we don't have access to your data files!
 
Share this answer
 
A bunch of quick suggestions before you wheel out the debugger:

- get rid of redundant lines, just open the files when you create your ifstream objects

- check for errors, especially check stream states. You have no idea if files are opened properly or if anything was actually read from them

- do you understand the rules for composite blocks for for loops and if statements? From a quick read it looks like your second for loop is never executed. This isn't python and indentation means nothing to the compiler.

- learn how to use <algorithm>. std::mismatch can probably do what you need without all the messing about with explicit loops

- investigate input_iterators to avoid reading entire files into RAM in one fell swoop. If you really want to compare DNA strings you'll need a lot of RAM for something that'll probably fail after < 10K base pairs.
 
Share this answer
 
Comments
CPallini 24-Oct-14 3:32am    
5.
Remove the two return statements. Your program is to count all deviations and not to stop at the first one and then exit the program without printing anything.

Then also correct the distance calculation:
C++
double similarity_scoreH = (len - humanDistance) / len;

As len and humanDistance are both of type int, the division will be done in integer arithmetic and most probably yield the result of 0. Use instead:
C++
double similarity_scoreH = 1.0;
if (len > 0)
    similarity_scoreH = ((double) len - humanDistance) / (double) len;

And do the same for similarity_scoreM.

I can only agree with the points made in the two other solutions: Get used to run your program in a debugger. You would have detected the problem within a few seconds had you run it in the debugger. And do a lot more checking of the input data. For example, what if the length of the unknownfile is shorter than that of the other two files?

As for the comparison algorithm: A one-by-one comparison is probably not the best way of comparing DNA sequences. If just one element is missing in a stream, all the remaining elements will be reported as deviations. A meaningful comparison is much more complex than what you do here.
 
Share this answer
 
1. Learn to use the Language:
As Aesclaell already stated, indentation means nothing to the compiler; use code blocks instead. As a rule of thumb, always use a code block after a control statement like if or for. It doesn't hurt and helps you prevent errors like these.

return is most probably not what you intended to use here: I suspect all you wanted to do is exit the loop. The correct command for that is break:
C++
for(int i=0; i<len;>   {
   if(mouse_dna[i] != unknown_dna[i])
   {
      mouseDistance++;
      break;
   }
}

2. Learn to use the compiler
In the standard setting, all compilers I know will issue warnings up to and including level 3. With that setting, the compiler should have warned you about unreachable code, referring to the issue discussed above. Learn to read and heed warnings - don't ignore them! Good programs will issue no warnings at all.

3. Learn to use the Debugger
Even ignoring the above two methods, you would have found the issue with the help of the debugger, and quite prossibly other issues on top of that

The Debugger is the best friend of a C/C++ developer: it will help you locate problems that can't be spotted by the compiler or prevented using proper syntax. it will even help you identify problems that are impossible to spot after looking at the code for a week, e. g. a file you try to open being access-protected, or an instable internet connection that intermittently fails to transport the data you're waiting for, or the mouse clicks you can't react to due to a defective USB connector or bad driver.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900