プログラムに、別々の行にある 5 つの 2 桁の数字を含むテキスト ファイルを読み取らせたいと考えています。次に、これらの数値をプログラムに配置して、最小の数値を削除し、残りの 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 ==========