有人可以告诉我为什么这段代码没有打印任何东西?

时间:2014-10-24 05:33:18

标签: c++

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

#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

它仍然无能为力。 我感谢任何帮助。谢谢!

3 个答案:

答案 0 :(得分:3)

它不打印任何内容,因为它在打印指令(return mouseDistance;return humanDistance;)之前返回。通过在每个return语句之前打印进度消息,使您的函数更加详细。

答案 1 :(得分:0)

正如已经指出的那样,你太早回来了。我修改你的代码:

  1. 我在if块和block之间放置括号。
  2. 最后我有一份回复声明。
  3. 这是一个开始。您可能必须添加更多检查,例如,如果文件正确打开。

    #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++;
            }
        }
        for(int i=0; i<len; i++)
        {
            if(human_dna[i] != unknown_dna[i])
            {
                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;
    
    
        return 0;
    }
    

答案 2 :(得分:0)

你使用arg [1],arg [2]和arg [3]。您可能需要arg [0],arg [1]和arg [2]。