2012-11-11 56 views
2

我被分配到一个程序,需要5分,下降的最低分,然后取最高4分的平均值。它不会让我使用变量'最低'。林有点困惑,可以使用一些帮助。下降最低分程序

#include <iostream> 
#include <iomanip> 

using namespace std; 

void getValues(float &score1, float &score2, float &score3, float &score4, float &score5); 
float findLowest(float score1, float score2, float score3, float score4, float score5, float lowest); 
void calcAverage(float score1, float score2, float score3, float score4, float score5, float &lowest); 
void displayVales(); 


int main(void) 
{ 
    float score1, score2, score3, score4, score5, lowest; 

    getValues(score1, score2, score3, score4, score5); 
    findLowest(score1, score2, score3, score4, score5, lowest); 
    calcAverage(score1, score2, score3, score4, score5, lowest); 

} 

void getValues(float &score1, float &score2, float &score3, float &score4, float &score5) 
{ 
    cout << "Welcome to the test averaging program!" << endl; 
    cout << "This program will drop the lowest of five scores." << endl; 

    cout << "Please enter score #1 --> "; 
    cin >> score1; 

    cout << "Please enter score #2 --> "; 
    cin >> score2; 

    cout << "Please enter score #3 --> "; 
    cin >> score3; 

    cout << "Please enter score #4 --> "; 
    cin >> score4; 

    cout << "Please enter score #5 --> "; 
    cin >> score5; 
} 

float findLowest(float score1, float score2, float score3, float score4, float score5, float lowest) 
{ 
lowest = score1; 
{ 
if (score2 < lowest) lowest = score2; 
if (score3 < lowest) lowest = score3; 
if (score4 < lowest) lowest = score4; 
if (score5 < lowest) lowest = score5; 
cout << "The lowest test score is " << lowest << endl; 
} 
return lowest; 
} 

void calcAverage (float score1, float score2, float score3, float score4, float score5, float &lowest) 

{ double average; 
cout << setw(4); 
cout.precision(2); 
cout.setf(ios::fixed); 
average = ((score1 + score2 + score3 + score4 + score5) - lowest)/4.0; 
cout << "The average of the 4 highest test scores is: " << average << endl; 

} 
+0

你是什么意思“它不会让我”。你得到一个编译错误? –

+0

它是一个调试错误,说明如何使用“最低”,但未声明。 – user1810514

+0

我用g ++ 4.6.3编译时没有遇到任何错误。你使用什么编译器? –

回答

2

将返回最低的,但不分配它...要么改变帕拉姆在findLowest做个参考或删除参数,并调用它像这样:

lowest = findLowest(score1, score2, score3, score4, score5); 
2

在你findLowest功能你需要通过引用传递(使用&符号)。

float findLowest(float score1, float score2, float score3, float score4, float score5, float &lowest) 
{ 
if (score2 < lowest) lowest = score2; 
if (score3 < lowest) lowest = score3; 
if (score4 < lowest) lowest = score4; 
if (score5 < lowest) lowest = score5; 
cout << "The lowest test score is " << lowest << endl; 
} 
+0

谢谢你解决了它 – user1810514

+0

不客气。如果这解决了您的问题,请将答案标记为正确。 – Boundless

0

您需要声明块中的最低它使用(不以主,但在findLowest):

float findLowest(float score1, float score2, float score3, float score4, float score5, float lowest) 
{ 
    float lowest = score1; 
    if (score2 < lowest) lowest = score2; 
    if (score3 < lowest) lowest = score3; 
    if (score4 < lowest) lowest = score4; 
    if (score5 < lowest) lowest = score5; 
    cout << "The lowest test score is " << lowest << endl; 
    return lowest; 
} 
1

注意,你可以简单地添加了所有的值,然后取最低值关闭。有点像这样:

#include <iostream> 
using namespace std; 
int main() { 
    const int num_of_values = 5; 
    float values[num_of_values]; 
    int lowest = 0; 
    float total = 0; 
    for (int i = 0; i < num_of_values; i++) { 
     printf("Enter value %i: ", i); 
     cin >> values[i]; 
     total += values[i]; 
     if (values[i] < values[lowest]) 
      lowest = i; 
    } 
    total -= values[lowest]; 
    printf("Result: %f \n", total/(num_of_values-1)); 
    system("pause"); 
} 
0

我也做了这个程序。我使用Visual Studio 2017在我的程序上取得了100分。

#include <iostream> 
using namespace std; 

void getScore(double&, double&, double&, double&, double&); 
int findLowest(double&, double&, double&, double&, double&); 
void calcAverage(double&, double&, double&, double&, double&, int&, int&); 

int main() 
{ 
double ts1, ts2, ts3, ts4, ts5; //declaring a variable for each requested test score 
int ta; //declaring test average into an int because of the program requirements 
getScore(ts1, ts2, ts3, ts4, ts5); //pulling the getScore function into the main which then defined 
int low = findLowest(ts1, ts2, ts3, ts4, ts5); //pulling the lowest variable into the main for further use 
calcAverage(ts1, ts2, ts3, ts4, ts5, ta, low); //calculating the average 
cout << "The average of all the scores is " << ta << endl; 
system("PAUSE"); 
return 0; 
} 

void getScore(double& ts1, double& ts2, double& ts3, double& ts4, double& ts5) 
{ 
for (int c = 0; c < 5; c++) 
{ 
    double t = 0; // current test score 
    cout << "Input the test score for test " << c + 1 << endl; 
    cin >> t; 

    if (t < 0 || t > 100) //input validation 
    { cout << "Input the test score for test " << c + 1<< " that is more than 0 and less than 100" << endl; 
     cin >> t; } 

    switch (c)//to input each test score into a variable for later use 
    { 
    case 0: if (c == 0) 
    { ts1 = t; 
     break;  } 
    case 1: if (c == 1) 
    { ts2 = t; 
     break;  } 
    case 2: if (c == 2) 
    { ts3 = t; 
     break;  } 
    case 3: if (c == 3) 
    { ts4 = t; 
     break;  } 
    case 4: if (c == 4) 
    { ts5 = t; 
     break;  } 
    } 
} 
} 

int findLowest(double& ts1, double& ts2, double& ts3, double& ts4, double& ts5) 
{ 
int lowest = 100; 
if (lowest > ts1) 
{ 
    lowest = ts1; 
} 
if (lowest > ts2) 
{ 
    lowest = ts2; 
} 
if (lowest > ts3) 
{ 
    lowest = ts3; 
} 
if (lowest > ts4) 
{ 
    lowest = ts4; 
} 
if (lowest > ts5) 
{ 
    lowest = ts5; 
} 
return lowest; //the lowest will be returned to main 
} 

void calcAverage(double& ts1, double& ts2, double& ts3, double& ts4, double& ts5, int& ta, int& low) 
{ 
ta = ts1 + ts2 + ts3 + ts4 + ts5; 
ta = ta - low; 
ta = ta/4; //calculating the average 
}