我想让一个程序读取一个包含 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 ==========