0

我想让一个程序读取一个包含 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:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest          drop.cpp(33): error C2082: redefinition of formal parameter 's1'
1>c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(34): error C2082: redefinition of formal parameter 's2'
1>c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(35): error C2082: redefinition of formal parameter 's3'
1>c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(36): error C2082: redefinition of formal parameter 's4'
1>c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(37): error C2082: redefinition of formal parameter 's5'
1>c:\users\ldw\documents\visual studio 2010\projects\droplowest\droplowest\lowest drop.cpp(42): error C2562: 'getScore' : 'void' function returning a value
1>          c:\users\ldw\documents\visual studio    2010\projects\droplowest\droplowest\lowest drop.cpp(14) : see declaration of 'getScore'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
4

4 回答 4

1

看看这里

myfile.open();
if(!myfile.is_open()) 
{
  cout<<"Error opening output file"<<endl;
  system("pause");
  return -1;
}
于 2013-07-03T16:50:08.403 回答
0

没有遍历整个代码。但正如错误所暗示的,你已经重新声明了 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++ 中是不允许的。

于 2013-07-03T17:03:12.380 回答
0

您收到有关重新定义形式参数的错误,因为getScore您正在重新s1定义s5. 五个分数作为 的参数给出getScore,因此您不必再次定义它们。你可以使用它们。

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

我会将您的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;为 just return;。这是因为getScore被定义为不返回任何内容 ( void)。因此,您无需返回任何内容。此外,您不需要拥有所有的getlines.

另请注意,在您的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++ 中,您可以在块中的任何位置定义它们。

于 2013-07-03T17:21:04.813 回答
0

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

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

并且您已将它们重新定义为int s1=0;. 做吧s1=0;

于 2015-08-11T18:14:48.050 回答