2017-02-18 248 views
-4

所以我有一些代碼,我正在爲一項任務編寫代碼,而且我一直在尋找來自3個浮點數的最小值和最大值。我目前得到的錯誤最小值和最大值C++

/usr/include/c++/4.9/bits/predefined_ops.h:121:39:錯誤:一元的「」無效的類型參數(有「浮」){ 返回布爾( _M_comp( __it1,* __ it2)); }

這是我當前的代碼:

#include <iostream> 
#include <string> 
#include <algorithm> 

using namespace std; 

//Name: Tanner Langan 
//Student Number: 300224340 
//Date: 2//16/2017 
//Assignment 3 


int main() 
{ 
char c1; 
float x1,x2,x3; 

cout << "Please Enter 1 character and 3 float numbers. One at a time" << endl; 
cout << "You must choose either X, N, V, or S for the character." << endl; 
cout << "If you choose X the result displayed will be the the maximum of x1, x2, and x3" << endl; 
cout << "If you choose N the result displayed will be the the minimum of x1, x2, and x3" << endl; 
cout << "If you choose V the result displayed will be the the average of x1, x2, and x3" << endl; 
cout << "If you choose S the program will terminate."; 
cin >> c1; 
cin >> x1; 
cin >> x2; 
cin >> x3; 

while (c1 != 'S') 
{ 
if(c1 == 'X' || c1 == 'x') 
{ 
cout << "The max is" << std::max_element(x1,x2,x3) << endl; 
main(); 
} 
if(c1 == 'N' || c1 == 'n') 
{ 

cout << "The min is" << std::min_element(x1,x2,x3) << endl; 
main(); 
} 
if(c1 == 'V' || c1 == 'v') 
{ 
cout << "The average is" << (x1+x2+x3)/3 << endl; 
main(); 
} 

} 
return 0; 

} 
+0

你需要使用'std :: min'和'std :: max' – simpel01

回答

0

std::max_elementstd::min_element你想象不工作。他們實際上將迭代器用於序列和比較器,而不僅僅是數字。它們也會返回迭代器,而不是值,因此在嘗試隨機調用函數之前閱讀文檔並理解如何使用它們。我鏈接的頁面上有一些例子可以幫助你。