参数阴影干扰读取文件的功能

问题描述 投票:0回答:1

我试图在我编写的这个程序中获取我的功能,以从文件中读取数据。我认为参数被隐藏了,但我自己对这个概念还很陌生。很想知道我在这里做错了什么,这样我就可以确保下次不会这样做。本质上,我试图让我的程序从我的文件中正确读取数据,并通过创建一个新文件来输出数据。我也应该通过引用使用 ostream& 但不确定如何合并它。

任何帮助将不胜感激。


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

int Adenine = 0;
int Thymine = 0;
int Cytosine = 0;
int Guanine = 0;
    
char numbOfNucs;
    
float totalNucleotides = 0;

int* nucleotideCounterFunc (int Adenine, int Thymine, int Cytosine, int Guanine, char numbOfNucs, float totalNucleotides){

    ifstream dna_file("dnaSequence.txt");
    
    if (dna_file){
        cout << "dnaSequence.txt can be read." << endl;
        cout << "Proceeding to analysis of the DNA Sequence..." << endl;
    } else {
        cout << "Error opening dnaSequence.txt: Please try again" << endl;
    }
    
    cout << " " << endl;
    
    while(dna_file >> numbOfNucs){
        if (numbOfNucs == 'A')
        {
            Adenine++;
        }
        
        else if (numbOfNucs == 'T')
        {
            Thymine++;
        }
        
        else if (numbOfNucs == 'C'){
            Cytosine++;
        }
        
        else if (numbOfNucs == 'G'){
            Guanine++;
        }
    }
 return 0;
}

void formatOutputFunc (int Adenine, int Thymine, int Cytosine, int Guanine, float totalNucleotides){
    totalNucleotides = Adenine + Thymine + Guanine + Cytosine;
    
    cout << "DNA Sequence Analysis: " << endl;
    cout << totalNucleotides << " nucleotides in the sequence" << endl;
    
    cout << " " << endl;
    
    cout << "Sequence breakdown: " << endl;
    cout << "Adenine: " << Adenine << "     " << (Adenine/totalNucleotides) * 100 << "%" << endl;
    cout << "Thymine: " << Thymine << "     " << (Thymine/totalNucleotides) * 100 << "%" << endl;
    cout << "Cytosine: " << Cytosine << "     " << (Cytosine/totalNucleotides) * 100 << "%" << endl;
    cout << "Guanine: " << Guanine << "     " << (Guanine/totalNucleotides) * 100 << "%" << endl;
    
}


void formatNewFileFunc(int Adenine, int Thymine, int Cytosine, int Guanine, float totalNucleotides){
      
    ofstream dna_file_finished("dnaSequence_Post_Analysis.txt");
    
    if (dna_file_finished) {
        cout << "DNA Sequence Analysis Complete" << endl;
    }


    dna_file_finished << "DNA Sequence Analysis: " << endl;
    dna_file_finished << totalNucleotides << " nucleotides in the sequence" << endl;
    
    dna_file_finished << " " << endl;
    
    dna_file_finished << "Sequence breakdown: " << endl;
    dna_file_finished << "Adenine: " << Adenine << "     " << (Adenine/totalNucleotides) * 100 << "%" << endl;
    dna_file_finished << "Thymine: " << Thymine << "     " << (Thymine/totalNucleotides) * 100 << "%" << endl;
    dna_file_finished << "Cytosine: " << Cytosine << "     " << (Cytosine/totalNucleotides) * 100 << "%" << endl;
    dna_file_finished << "Guanine: " << Guanine << "     " << (Guanine/totalNucleotides) * 100 << "%" << endl;


    dna_file_finished << " " << endl;
    
}

int main()
{
    nucleotideCounterFunc(Adenine, Thymine, Cytosine, Guanine, numbOfNucs, totalNucleotides);
    
    cout << " " << endl;
    
    formatOutputFunc(Adenine, Thymine, Cytosine, Guanine, totalNucleotides);

    cout << " " << endl;

    formatNewFileFunc(Adenine, Thymine, Cytosine, Guanine, totalNucleotides);

    return 0;
}

我的输出应该是: DNA序列分析: 序列中有 29782 个核苷酸 顺序分解:

腺嘌呤:8892 29.86%

胸腺嘧啶:9581 32.17%

胞嘧啶:5462 18.34%

鸟嘌呤:5847 19.63%

但我的输出结果为 0 nan%。

c++ function ostream shadowing
1个回答
0
投票

要事第一。全局变量通常不是推荐的编程方式。在你的情况下,他们肯定不是。把他们的定义移到里面

main
:

int main(){
    int Adenine = 0;
    int Thymine = 0;
    int Cytosine = 0;
    int Guanine = 0;
    
    char numbOfNucs;
    
    int totalNucleotides = 0;

    //...

接下来,几乎总是避免在全局或命名空间范围内使用

using namespace
。删除该行并使用
std::
作为标准库标识符的前缀;更长,但相信我,它更安全。你只需要
std::cout
std::endl
std::ofstream
;没什么大不了的。

你的典型问题是按值与按引用函数参数。如果函数应该访问原始对象,则需要通过引用传递它们。因此,将

nucleotideCounterFunc
的声明和定义都更改为

int nucleotideCounterFunc(int &Adenine, int &Thymine, int &Cytosine, int &Guanine, char const numbOfNucs)

计算

nucleotideCounterFunc
里面的总和,最后返回总和:

    return Adenine + Thymine + Cytosine + Guanine;
};//End of nucleotideCounterFunc

将返回值存储在

totalNucleotides
:

    //Inside main:
    totalNucleotides = nucleotideCounterFunc(Adenine, Thymine, Cytosine, Guanine, numbOfNucs);

事实上,您应该将

totalNucleotides
初始化为
nucleotideCounterFunc
的结果,而不是
0
,但我们将跳过那个。可以通过多种方式修改此代码以提高可读性,但当您学习更多基础知识时,我将剩下的留给您自己。

PS:这个阶段不要尝试使用指针;你会烧伤自己。

© www.soinside.com 2019 - 2024. All rights reserved.