有人可以告诉我为什么这段代码什么都不打印? [英] Can someone please tell me why this code prints nothing?

查看:57
本文介绍了有人可以告诉我为什么这段代码什么都不打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个比较三个文本文件的代码,我不知道为什么我不会打印任何东西:



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;
	

}



它编译正确,并没有出现任何错误,但是当我把它作为:

./DNA mouseDNA.txt humanDNA.txt unknownDNA.txt

它仍然无效。

我感谢任何帮助。谢谢!

推荐答案

从调试器开始:在主函数的第一行放置一个断点,然后运行程序。当它到达断点时,单步执行应用程序,查看每行中涉及的所有变量,并在执行之前准确计算出指令要执行的操作。然后检查以确保它做到了。在某些时候,这两个会有所不同,这应该让你开始寻找原因。


调试是一项技能:你只能通过练习和使用它来开发它!

我们不能为您做到这一点:我们无法访问您的数据文件!
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!


在您离开之前的一堆快速建议调试器:



- 摆脱多余的线条,只需在创建ifstream对象时打开文件



- 检查错误,特别是检查流状态。您不知道文件是否正确打开或者是否实际从中读取了任何内容



- 您是否了解for循环和if语句的复合块的规则?从快速阅读看起来你的第二个for循环永远不会被执行。这不是python,缩进对编译器没有任何意义。



- 学习如何使用< algorithm>。 std :: mismatch可以做你需要的东西而不用所有搞乱的显式循环



- 调查input_iterators以避免一举将整个文件读入RAM。如果你真的想要比较DNA字符串,你需要大量的内存,以便在<之后可能会失败。 10K碱基对。
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.


删除两个 return 语句。您的程序将计算所有偏差,而不是在第一个停止,然后退出程序而不打印任何内容。



然后也正确距离计算:

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:
double similarity_scoreH = (len - humanDistance) / len;



由于len和humanDistance都是int类型,因此除法将以整数运算完成,最有可能产生0的结果。 br />


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:

double similarity_scoreH = 1.0;
if (len > 0)
    similarity_scoreH = ((double) len - humanDistance) / (double) len;



similarity_scoreM 做同样的事情。



我只能同意其他两个解决方案中的要点:习惯于在调试器中运行程序。如果在调试器中运行它,您将在几秒钟内检测到问题。并做更多检查输入数据。例如,如果 unknownfile 的长度短于其他两个文件的长度怎么办?



As对于比较算法:逐个比较可能不是比较DNA序列的最佳方法。如果流中只缺少一个元素,则所有剩余元素将报告为偏差。有意义的比较比你在这里做的要复杂得多。


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.


这篇关于有人可以告诉我为什么这段代码什么都不打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆