0

プログラムに、別々の行にある 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 ==========
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

関数プロトタイプでは、仮パラメータのデータ型だけを持っています。

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

そして、それらを に再定義しましたint s1=0;。するだけですs1=0;

于 2015-08-11T18:14:48.050 に答える
0

を介しgetScoreて再定義しているため、仮パラメータの再定義に関するエラーが発生しています。5 つのスコアは引数として に渡されるため、再度定義する必要はありません。あなたはそれらを使うことができます。s1s5getScore

スコアを手動で入力する代わりに、プログラムがファイルから読み取られるようにするには、それぞれcin >> s#;を(実際の数字 1 ~ 5 にmyfile >> s#;置き換えます) に変更します。とはどちらもストリームなので、同じように使用できます。彼らはただ別の場所から読んだだけです。#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;を justに変更したことに注意してくださいreturn;。これは、getScoreが何も返さない ( ) と定義されているためですvoid。だから何も返さなくていい。getlineまた、すべてのsを持っている必要はありません。

findLowest関数では、最後に複数の戻り値があることにも注意してください。return はそれが関数の終わりであることを意味するため、最初のものだけが使用されます。次のように2番目のものを削除します。

//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 に答える