错误的函数参数太多了

时间:2017-07-01 20:23:42

标签: c++ function arguments

我继续得到这个错误,我多次查看我的代码而且我看不到我错误的转向。该程序应该有5个用户输入评级,然后找到最高和最低输入,并将其与平均值一起返回给用户。

提前致谢。

#include <iostream> // whatever this is
#include <iomanip>
using namespace std;

int getValue();
int findLowest();
int findHighest();
double findAverage();

int main(){  // main function

    cout << setw(15)<<""<< "Product Review"; // displays this at the top

    int ratingOne = getValue();
    int ratingTwo = getValue();
    int ratingThree = getValue();
    int ratingFour = getValue();
    int ratingFive = getValue();
    int low = findLowest(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive);
    int high = findHighest(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive);
    double average = findAverage(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive);

    cout << "\nThe highest rating received was: " << high;
    cout << "\nThe lowest rating received was: " << low;
    cout << std::fixed << std::setprecision(2) << "\nThe average rating received among 5 users was: " << average;
}

int getValue(){
    int rating; 

    do {
        cout << "\nOn a scale of 1 to 10, how would you rate our product? ";
        cin >> rating; 

        if (rating < 1 || rating > 10) {cout << "\nYou have entered an invalid number. Please try again.";}
    } while (rating < 1 || rating > 10);
    return rating;
}

int findLowest(){
    int low = 11;
    if (ratingOne < low) { low = ratingOne }
    if (ratingTwo < low) { low = ratingTwo }
    if (ratingThree < low) { low = ratingThree }
    if (ratingFour < low) { low = ratingFour }
    if (ratingFive < low) { low = ratingFive }
    return low;
}
int findHighest(){
    int high = 0;
    if (ratingOne > high) { high = ratingOne }
    if (ratingTwo > high) { high = ratingTwo }
    if (ratingThree > high) { high = ratingThree }
    if (ratingFour > high) { high = ratingFour }
    if (ratingFive > high) { high = ratingFive }
    return high;
}
double findAverage(){
    double average;
    average = ratingOne + ratingTwo + ratingThree + ratingFour + ratingFive;
    average = average / 5;
    return average;
}

3 个答案:

答案 0 :(得分:0)

函数findLowest和findHighest没有任何参数,但是你提供了5.为了正确地做,你应该写:

#include <iostream> // whatever this is
#include <iomanip>
using namespace std;

int getValue();
int findLowest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive);
int findHighest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive);
double findAverage(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive);


int main(){  // main function

cout << setw(15)<<""<< "Product Review"; // displays this at the top    

int ratingOne = getValue();
int ratingTwo = getValue();
int ratingThree = getValue();
int ratingFour = getValue();
int ratingFive = getValue();
int low = findLowest(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive);
int high = findHighest(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive);
double average = findAverage(ratingOne, ratingTwo, ratingThree, ratingFour, ratingFive);

cout << "\nThe highest rating received was: " << high;
cout << "\nThe lowest rating received was: " << low;
cout << std::fixed << std::setprecision(2) << "\nThe average rating received among 5 users was: " << average;
}

int getValue(){
int rating; 

do {
cout << "\nOn a scale of 1 to 10, how would you rate our product? ";
cin >> rating; 

if (rating < 1 || rating > 10) {cout << "\nYou have entered an invalid number. Please try again.";}
} 

while (rating < 1 || rating > 10);
return rating;
}

int findLowest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){
int low = 11;
if (ratingOne < low) { low = ratingOne; }
if (ratingTwo < low) { low = ratingTwo; }
if (ratingThree < low) { low = ratingThree; }
if (ratingFour < low) { low = ratingFour; }
if (ratingFive < low) { low = ratingFive; }
return low;
}
int findHighest(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){
int high = 0;
if (ratingOne > high) { high = ratingOne; }
if (ratingTwo > high) { high = ratingTwo; }
if (ratingThree > high) { high = ratingThree; }
if (ratingFour > high) { high = ratingFour; }
if (ratingFive > high) { high = ratingFive; }
return high;
}
double findAverage(int ratingOne, int ratingTwo, int ratingThree, int ratingFour, int ratingFive){
double average;
average = ratingOne + ratingTwo + ratingThree + ratingFour + ratingFive;
average = average / 5;
return average;
}   

答案 1 :(得分:0)

函数原型中参数的数量和类型

    int findLowest();
    int findHighest();
    double findAverage();

应与您定义中的匹配。他们没有。看来你想将类型为int的参数(5)传递给上面的函数,因此在原型中。你需要告诉它。

        int findLowest(int, int, int, int, int);
        int findHighest(int, int, int, int, int);
        double findAverage(int, int, int, int, int);

并在您的函数定义中。

            int findLowest(int ratingOne, int ratingTwo, int ratingThree,  int ratingFour, int ratingFive){...}
            int findHighest(int ratingOne, int ratingTwo, int ratingThree,  int ratingFour, int ratingFive){...}
            double findAverage(int ratingOne, int ratingTwo, int ratingThree,  int ratingFour, int ratingFive){..}

以及调用函数时的参数和类型数也应与函数定义匹配。

答案 2 :(得分:0)

好的,所以我想出来了,但我不知道为什么。这是我的最终代码。

{{1}}

之前我把它作为我的代码,它给了我上面讨论过的错误。     int findLowest(){...}

我还通过说&#34; int&#34;来编译程序。在()中有5次,但这些值在天文数字上是不合适的。我输入5个数字1-10,结果类似于24908140489,-301213901398,489723498234等等。不确定为什么会发生这种情况,但也许它是存储数字的默认值,而且有些错误。应该保存该编辑以回顾它,因为现在我不知道是什么导致它发生。至少它现在有效,感谢您的帮助!