问题
I cannot figure out what is wrong with this function. I need to find the nimimum exam in the function.
void findMinExam( int exam1, int exam2, int exam3, int& minExam)
//***************************************************************************************
//Purpose: Determine the lowest exam score.
//Input: exam1, exam2, exam3, minExam
//Precondition: exam1, exam2, exam3, minExam have values and are valid.
//Output: int.
//Post condition: This function shows the lowest exam.
//Note: None.
//****************************************************************************************
{
if (exam1 < minExam)
{
minExam = exam1;
}
if (exam2 < minExam)
{
minExam = exam2;
}
if (exam3 < minExam)
{
minExam = exam3;
}
}
回答1:
Based on your description, there is no behavior problem with your code... just API quirks, but your explanation of the issue may be incorrect.
Either way, if you have C++11 support, you could simply write this instead of your own hand-crafted function, as pointed out in the comments:
minExam = std::min({exam1, exam2, exam3, minExam});
来源:https://stackoverflow.com/questions/22925247/find-minexam-in-the-function