2017-02-18 5 views
-4

Also habe ich etwas Code, den ich für eine Aufgabe schreibe und ich bin fest auf die Suche nach den Min- und Max-Werten von 3 Floats. Ich bin derzeit immer den FehlerMin und Max Wert C++

/usr/include/c++/4.9/bits/predefined_ops.h:121:39: Fehler: ungültiges Typargument von einstelligen '' (hat 'float') {return bool (_M_comp ( __it1, * __ it2)); }

Dies ist mein aktueller Code:

#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

müssen Sie 'std :: min' und' std :: max' verwenden – simpel01

Antwort

0

std::max_element und std::min_element nicht funktionieren, wie Sie denken. Sie nehmen Iteratoren zur Sequenz und einen Komparator, nicht nur Zahlen. Sie geben auch Iteratoren und keine Werte zurück. Lesen Sie daher die Dokumentation und verstehen Sie, wie Sie sie verwenden, bevor Sie versuchen, Funktionen nach dem Zufallsprinzip aufzurufen. Es gibt Beispiele auf den Seiten, die ich verlinkt habe, die dir helfen sollten.