从文本文件中读取,然后使用程序中的输入

Reading from a text file and then using the input in the program

本文关键字:程序 输入 文本 文件 读取 然后      更新时间:2023-10-16

我想让一个程序读取一个包含5个两位数的文本文件,这些数字位于不同的行上。然后我想把这些数字放在程序中,其中最低的数字将被删除,剩下的4个数字将被平均,然后输出到屏幕上。

文本文件只是一个记事本.txt文件,其中包含:

83
71
94
62
75

我已经成功地构建了一个程序,我可以手动输入数字,它会按照我的意愿进行,但我在代码中使用文件的知识有限。我读过关于矢量的书,但我想先保持简单,在尝试学习其他东西之前先掌握它的窍门。我试着设置了一个类似于我认为它可能应该是什么样子的代码。我的问题是:为什么我在调试时会收到"错误C2082:形式参数的重新定义"?

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

//function prototypes 
void getScore(int s1, int s2, int s3, int s4, int s5);
void calcAverage(int s1, int s2, int s3, int s4, int s5);
int findLowest(int s1, int s2, int s3, int s4, int s5);
int main()
{
getScore(0, 0, 0, 0, 0);
return 0;
}
//function to collect the 5 test scores 
void getScore(int s1, int s2, int s3, int s4, int s5)
{
string line1[30];
string line2[30];
string line3[30];
string line4[30];
string line5[30];
ifstream myfile("grades.txt");
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;
if(!myfile) 
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
while(!myfile.eof())
getline(myfile,line1[s1],'n');
cin >> s1;
getline(myfile,line2[s2],'n');
cin >> s2;
getline(myfile,line3[s3],'n');
cin >> s3;
getline(myfile,line4[s4],'n');
cin >> s4;
getline(myfile,line5[s5],'n');
cin >> s5;
calcAverage(s1, s2, s3, s4, s5);
}


//function to calculate the average of the 4 highest test scores 
void calcAverage(int s1, int s2, int s3, int s4, int s5)
{
int average;
int lowest;
lowest = findLowest(s1, s2, s3, s4, s5);
average = ((s1 + s2 + s3 + s4 + s5) - lowest)/4;
cout << endl;
cout << "The average of the four highest test scores is: ";
cout << average << endl;
}
//function to find the lowest test score 
int findLowest(int s1, int s2, int s3, int s4, int s5)
{
int lowest = s1;
if (s2<lowest)
lowest = s2;
if (s3<lowest)
lowest = s3;
if (s4<lowest)
lowest = s4;
if (s5<lowest)
lowest = s5;
return lowest;
return 0;
}

构建结果:

1>------ Build started: Project: droplowest, Configuration: Debug Win32 ------
1>  lowest drop.cpp
1>c:usersldwdocumentsvisual studio 2010projectsdroplowestdroplowestlowest          drop.cpp(33): error C2082: redefinition of formal parameter 's1'
1>c:usersldwdocumentsvisual studio 2010projectsdroplowestdroplowestlowest drop.cpp(34): error C2082: redefinition of formal parameter 's2'
1>c:usersldwdocumentsvisual studio 2010projectsdroplowestdroplowestlowest drop.cpp(35): error C2082: redefinition of formal parameter 's3'
1>c:usersldwdocumentsvisual studio 2010projectsdroplowestdroplowestlowest drop.cpp(36): error C2082: redefinition of formal parameter 's4'
1>c:usersldwdocumentsvisual studio 2010projectsdroplowestdroplowestlowest drop.cpp(37): error C2082: redefinition of formal parameter 's5'
1>c:usersldwdocumentsvisual studio 2010projectsdroplowestdroplowestlowest drop.cpp(42): error C2562: 'getScore' : 'void' function returning a value
1>          c:usersldwdocumentsvisual studio    2010projectsdroplowestdroplowestlowest drop.cpp(14) : see declaration of 'getScore'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

看看这里。

myfile.open();
if(!myfile.is_open()) 
{
  cout<<"Error opening output file"<<endl;
  system("pause");
  return -1;
}

由于在getScore中,您正在通过s5重新定义s1,因此您得到了关于重新定义形式参数的错误。这五个分数是作为getScore的自变量给出的,所以您不必再次定义它们。你可以直接使用它们。

要从文件中读取您的程序,而不是手动输入分数,只需将每个cin >> s#;更改为myfile >> s#;(用实际数字1-5替换#)。cinmyfile都是流,因此可以以相同的方式使用它们。他们只是从不同的地方读。

我会把你的getScore函数改成这样:

//function to collect the 5 test scores 
void getScore(int s1, int s2, int s3, int s4, int s5)
{
    ifstream myfile("grades.txt");
    if (!myfile) 
    {
        cout << "Error opening output file" << endl;
        system("pause>nul");
        return;
    }
    myfile >> s1;
    myfile >> s2;
    myfile >> s3;
    myfile >> s4;
    myfile >> s5;
    calcAverage(s1, s2, s3, s4, s5);
}

请注意,我已将您的return -1;更改为仅return;。这是因为getScore被定义为不返回任何内容(void)。所以你什么都不回。此外,您不需要拥有所有的getline s。

还要注意,在findLowest函数中,末尾有多个返回。只会使用第一个,因为返回意味着这是函数的结束。我会删除第二个这样的:

//function to find the lowest test score 
int findLowest(int s1, int s2, int s3, int s4, int s5)
{
    int lowest = s1;
    if (s2 < lowest)
        lowest = s2;
    if (s3 < lowest)
        lowest = s3;
    if (s4 < lowest)
        lowest = s4;
    if (s5 < lowest)
        lowest = s5;
    return lowest;
}

为了让你的程序更容易修改(如果你以后想读取/处理更多的数字怎么办?),你想使用固定大小的数组(而不是矢量),如下所示:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUM_SCORES = 5;
//function prototypes
void getScores(string filename);
void calcAverage(int* scores);
int findLowest(int* scores);
int main()
{
    getScore("grades.txt");
    return 0;
}
//function to collect the test scores 
void getScores(string filename)
{
    int scores[NUM_SCORES] = { 0 }; // initialize all of the scores to 0.
    ifstream myfile(filename);
    if(!myfile) 
    {
        cout << "Error opening output file." << endl;
        system("pause>nul");
        return;
    }
    int i;
    for (i = 0; i < NUM_SCORES && !myfile.eof(); i++)
        myfile >> scores[i];
    if (i != NUM_SCORES)
    {
        // this means that there weren't enough numbers in the file.
        cout << "Not enough numbers in the file." << endl;
        system("pause>nul");
        return;
    }
    calcAverage(scores);
}
//function to calculate the average of the every test score but the lowest 
void calcAverage(int* scores)
{
    int lowest = findLowest(scores);
    int sum = 0;
    for (int i = 0; i < NUM_SCORES; i++)
        sum += scores[i];
    sum -= lowest;
    int average = sum / (NUM_SCORES - 1);
    cout << endl << "The average of the four highest test scores is: " << average << endl;
}
//function to find the lowest test score 
int findLowest(int* scores)
{
    int lowest = scores[0];
    for (int i = 1; i < NUM_SCORES; i++)
        if (scores[i] < lowest)
            lowest = scores[i];
    return lowest;
}

还要注意,在calcAverage中,不需要在函数的开头定义所有变量。在C++中,您可以在块中的任何位置定义它们。

没有完成整个代码。但是,正如错误所表明的那样,u已经重新声明了int s1、s2、s3、s4、s5。

尝试更改以下

ifstream myfile("grades.txt");
int s1 = 0;
int s2 = 0;
int s3 = 0;
int s4 = 0;
int s5 = 0;

ifstream myfile("grades.txt");
s1 = 0;
s2 = 0;
s3 = 0;
s4 = 0;
s5 = 0;

因为这些变量已经在函数参数列表中声明。

void getScore(int s1, int s2, int s3, int s4, int s5)
{
  ...

你不能申报。它在C++中是不允许的。

在函数原型中,只有形式参数的数据类型。

void getScore(int,int,int,int,int)

您已经将它们重新定义为int s1=0;。只需执行s1=0;