我想让一个程序读取一个带有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 ==========
答案 0 :(得分:1)
看看here。
myfile.open();
if(!myfile.is_open())
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
答案 1 :(得分: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 ++中使用。
答案 2 :(得分: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;
更改为return;
。这是因为getScore
被定义为不返回任何内容(void
)。所以你必须什么也不返回。此外,您不需要拥有所有getline
个。
另请注意,在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 ++中,您可以在块中的任何位置定义它们。
答案 3 :(得分:0)
在函数原型中,您只需要形式参数的数据类型。
void getScore(int,int,int,int,int)
您已将其重新定义为int s1=0;
。只需s1=0;
。