2012-03-19 6 views
-1

更新: 私はプログラムをコンパイルして実行することができました。しかし、ジャッジスコア値2.3の変数は、奇妙な方法で格納され、計算をスローしています。私は2.3を入力すると、ストアの値は2.299999999998です - これはなぜですか?スコアを入力すると最終スコアが0.3になるため、calcScoreまたは最低および最高の関数がエラーになることがあります。2.3 - 5.5 - 5.5 - 5.5 - 8.5 - 任意のアイデア?C++エラー: 'int'の前にprimary-expressionが必要ですか?

// file name starSearch.cpp 
// author: SK 
// last modified: 03/19/2012 

#include <iostream> 
#include <iomanip> 
using namespace std; 

//function prototypes 
void getJudgeData(double &); 
void calcScore(double, double, double, double, double); 
int findLowest(double, double, double, double, double); 
int findHighest(double, double, double, double, double); 

//declare variables 
double score1; 
double score2; 
double score3; 
double score4; 
double score5; 

int main() 
{ 
    getJudgeData(score1); //Call getJudgeData for first judge score 
    getJudgeData(score2); //Call getJudgeData for second judge score 
    getJudgeData(score3); //Call getJudgeData for third judge score 
    getJudgeData(score4); //Call getJudgeData for fourth judge score 
    getJudgeData(score5); //Call getJudgeData for fifth judge score 
    calcScore(score1, score2, score3, score4, score5); //calculate contestant score 
    return 0; 
} 

// 
//Begin function prototypes definitions 
// 

//defines calcScore. 
void calcScore(double score1, double score2, double score3, double score4, double score5) 
{ 
    double total = (score1 + score2 + score3 + score4 + score5 - findLowest(score1, score2, score3, score4, score5) - findHighest(score1, score2, score3, score4, score5))/3; 
    cout << setprecision(2); 
    cout << "The contestant's final score is: " << total << endl; 
} 

//defines getJudgeData. this function asks each judge for their score. 
void getJudgeData(double &score) 
{ 
    cout << "Judge, enter the contestant's score: "; 
    cin >> score; 
    if (score < 0 || score > 10) //validate score between 0-10, else display error msg 
    { 
    cout << "ERROR: Enter a value in the range 0-10.\n"; 
    return getJudgeData(score); 
    } 
    return; 
} 

//defines findLowest. this function returns the lowest score entered. 
int findLowest(double score1, double score2, double score3, double score4, double score5) 
{ 
    if (score1 < score2 && score1 < score3 && score1 < score4 && score1 < score5) 
    { 
     return score1; 
    } 

    else if (score2 < score1 && score2 < score3 && score2 < score4 && score2 < score5) 
    { 
     return score2; 
    } 

    else if (score3 < score1 && score3 < score2 && score3 < score4 && score3 < score5) 
    { 
     return score3; 
    } 

    else if (score4 < score1 && score4 < score2 && score4 < score3 && score4 < score5) 
    { 
     return score4; 
    } 

    else if (score5 < score1 && score5 < score2 && score5 < score3 && score5 < score4) 
    { 
     return score5; 
    } 
} 

//defines findHighest. this functio returns the highest score entered. 
int findHighest(double score1, double score2, double score3, double score4, double score5) 
{ 
    if (score1 > score2 && score1 > score3 && score1 > score4 && score1 > score5) 
    { 
     return score1; 
    } 

    else if (score2 > score1 && score2 > score3 && score3 > score4 && score3 > score5) 
    { 
     return score2; 
    } 

    else if (score3 > score1 && score3 > score2 && score3 > score4 && score3 > score5) 
    { 
     return score3; 
    } 

    else if (score4 > score1 && score4 > score2 && score4 > score3 && score4 > score5) 
    { 
     return score4; 
    } 

    else if (score5 > score1 && score5 > score2 && score5 > score3 && score5 > score4) 
    { 
     return score5; 
    } 
} 

ORIGINAL:

私はちょうど5点の得点を平均と最低を削除する方法を考え出すと最高カント私の代数式が整数3で書かれているライン39上のコンパイラエラーを取得しています。最高と最低を決定する関数は順番になっているようですが..助けてください。

ありがとうございます。

*// file name starSearch.cpp 
// author: SK 
// last modified: 03/19/2012 

#include <iostream> 
using namespace std; 

//function prototypes 
void getJudgeData(double); 
void calcScore(); 
int findLowest(); 
int findHighest(); 

//declare variables 
double score1; 
double score2; 
double score3; 
double score4; 
double score5; 

int main() 
{ 
    getJudgeData(score1); //Call getJudgeData for first judge score 
    getJudgeData(score2); //Call getJudgeData for second judge score 
    getJudgeData(score3); //Call getJudgeData for third judge score 
    getJudgeData(score4); //Call getJudgeData for fourth judge score 
    getJudgeData(score5); //Call getJudgeData for fifth judge score 
    calcScore(); //calculate contestant score 
    return 0; 
} 

// 
//Begin funtion prototypes definitions 
// 

//defines calcScore. 
void calcScore(double score1, double score2, double score3, double score4, double score5) 
{ 
    double total = score1 + score2 + score3 + score4 + score5 - int findLowest(a, b, c, d, e) - intfindHighest(a, b, c, d, e)/3; 
    cout << "The contestant's final score is: " << total << endl; 
} 

//defines getJudgeData. this function asks each judge for their score. 
void getJudgeData(double score) 
{ 
    cout << "Judge, enter the contestant's score: "; 
    cin >> score; 
    if (score < 0 || score > 10) //validate score between 0-10, else display error msg 
    { 
    cout << "ERROR: Enter a value in the range 0-10: "; 
    cin >> score; 
    } 
    else 
    return; 
} 

//defines findLowest. this function returns the lowest score entered. 
int findLowest(double score1, double score2, double score3, double score4, double score5) 
{ 
    if (score1 < score2, score3, score4, score5) 
    { 
     return score1; 
    } 

    else if (score2 < score1, score3, score4, score5) 
    { 
     return score2; 
    } 

    else if (score3 < score1, score2, score4, score5) 
    { 
     return score3; 
    } 

    else if (score4 < score1, score2, score3, score5) 
    { 
     return score4; 
    } 

    else if (score5 < score1, score2, score3, score4) 
    { 
     return score5; 
    } 
} 

//defines findHighest. this functio returns the highest score entered. 
int findHighest(double score1, double score2, double score3, double score4, double score5) 
{ 
    if (score1 > score2, score3, score4, score5) 
    { 
     return score1; 
    } 

    else if (score2 > score1, score3, score4, score5) 
    { 
     return score2; 
    } 

    else if (score3 > score1, score2, score4, score5) 
    { 
     return score3; 
    } 

    else if (score4 > score1, score2, score3, score5) 
    { 
     return score4; 
    } 

    else if (score5 > score1, score2, score3, score4) 
    { 
     return score5; 
    } 
}* 
+2

なぜ表現の途中に 'int findLowest(...)'がありますか? – tpg2114

+0

は答えませんでしたが、最後に '}'を '}' –

+0

に置き換えたいと思うかもしれませんが、実際のコードではないと私は思っています。ありがとう! – user1278971

答えて

3

関数はそれぞれfindLowestfindHighestと呼ばれています。 intは戻り値の型であり、呼び出すときには指定しないでください。

これが混乱の原因なら、私は良い初心者レベルのC++の本を見つけて、関数の宣言と呼び出しを読むことをお勧めします。

+1

はい、合意しました。「Accelerated C++」をお勧めします。 – 111111

+0

私は関数呼び出し文を間違って指定しました。もっと練習する必要があります。 – user1278971

+0

@ user1278971:特に「if(score1

1

問題は、あなたの行です:

double total = score1 + score2 + score3 + score4 + score5 - int findLowest(a, b, c, d, e) - intfindHighest(a, b, c, d, e)/3;

あなたはint識別子をインラインで含めるべきではありません。コンパイラはあなたの関数がintを返すことを既に知っています。他の誰かが指摘したように代わり

double total = score1 + score2 + score3 + score4 + score5 - findLowest(a, b, c, d, e) - findHighest(a, b, c, d, e)/3;

を書くことは、あなたは、二重にそれをキャストすることがあります。その場合、あなたはこのようにするでしょう。

double total = score1 + score2 + score3 + score4 + score5 - (double)findLowest(a, b, c, d, e) - (double)findHighest(a, b, c, d, e)/3;

それでも、これは実際には平均値を計算しませんので、ご注文や操作を強制するために括弧を使用する必要があります。

double total = (score1 + score2 + score3 + score4 + score5 - (double)findLowest(a, b, c, d, e) - (double)findHighest(a, b, c, d, e))/3;

+0

ここではC++の操作順序はどうですか?その部門は一度減算されたスコアの全体を平均しないでしょうか?\ – user1278971

+0

@ user1278971実際にあなたは正しいです。私は私の答えを編集します –

+0

ありがとうございます。私はそれを書くのは難しいと思っていましたが、関数呼び出しステートメントを組み込んで私を投げ捨てました。 – user1278971

0

あなたがキャストを実行したいでしたか?それは括弧(二重)で行われ、私はそれが必要かどうかわかりませんが、私もあなたがキャストしたいと思うものが二重であると思います。

+1

はキャストする必要はありませんが、これは暗黙的な変換ですが、したいと思うかもしれません。 –

+0

これが実際に関数定義に必要かどうかはわかりません。 – user1278971

+0

関数の定義ではなく、 'double total = score1 + score2 + score3 + score4 + score5 - (double)findLowest(a、b、c、d、e) - (double)findHighest(a、b、c、d) 、e)/ 3'がサムによって指摘されている –

1

Chowlettによって提供された回答に加えて、findLowestとfindHighestにカンマ演算子の使用を再考する必要があります。

例えば、findHighest

if (score1 > score2, score3, score4, score5) 

であなたのラインは、基本的に、あなたが意図しているように見えるものではありません

if (score5) 

とまったく同じです。

+0

私は5つの入力スコアの最高値をどのように比較できるかわかりません。 "if(score5)"は、それが見えるスコアの1つだけを比較します。 – user1278971

+0

@ user1278971 - ショートカットはありません。私は恐れています。あなたがそのように比較したいのなら 'if(score1> score2 && score1> score3 && ... && score1> score5)'が必要です。 – Chowlett

+0

私は見ています。そして、表現との比較がうまくいきます。ありがとう – user1278971

2

以前の回答に加えて、getJudgeData機能は非常に壊れています。参照を二重にする必要があります。そうしないと、元の変数は決して変更されず、その機能は役に立たなくなります。さらに、あなたは悪いスコアを2回入力して裁判官に対する保護はありません

void getJudgeData(double& score) 
{ 
    cout << "Judge, enter the contestant's score: "; 
    cin >> score; 
    if (score < 0 || score > 10) //validate score between 0-10, else display error msg 
    { 
     cout << "ERROR: Enter a value in the range 0-10: "; 
     return getJudgeData(score); 
    } 
    return; 
} 

この単純な再帰は毎回不正なデータを入力する裁判官からあなたを守ります、そしてあなたのコードは、それが希望を期待書かれているように見えるとしても、正しく元の変数を変更します。

+0

私は参照してください。これは関数の最初の試みです。しかし、if文がこのようにして関数を返すように定義する必要があります。 – user1278971

+0

@ user1278971:関数を返さず、関数呼び出しの_result_を返します。 –

+0

@MooingDuck:どのような結果ですか?この関数は 'void'です。 – Puppy

関連する問題