exception einbauen
-
hallo! wie kann ich in mein Programm eine Ausnahmebehandlung einbauen die erkennt wenn die Anzahl der Elemente gleich Null ist oder ein Element
negativ ist? habe die ausführungen in meinem c++ buch leider nicht so richtig verstandenAlso hier das Programm, wäre sehr nett wenn mir jemand weiterhelfen könnte:
#include <iostream.h> #include <stdio.h> #include <vector> class NegativesElement{}; class vec { public: float mittel(); private: int Element; vector<int> v; }; int summe = 0; int main() { int anzahl; cout << "Anzahl der Elemente eingeben: "; cin >> anzahl; vector<vec> v(anzahl); float mittelwert=0; for (int i=0; i<anzahl; ++i){ v[i].mittel(); } mittelwert=v[anzahl].mittel()/(anzahl+1); cout << endl << "Mittelwert: " << mittelwert << endl << endl; system("PAUSE"); return 0; } float vec::mittel() { cout<<"INT Wert eingeben: "; cin >> Element; if(Element<0){ throw "Negatives Element"; getchar();} summe += Element; float mittelwert=0; int i=0; return summe; }
schonmal vielen Dank
Mathias
-
ich throw nen ball und mein freund catched ihn
-
hey danke für den tip!
du meinst das bestimmt so:
//.. public: float mittel() throw(NegativesElement); //.. try{ for (int i=0; i<anzahl; ++i){ v[i].mittel();} } catch (NegativesElement&) { cout << "\nFehler: Negatives Element\n"; } //.. float vec::mittel() throw (NegativesElement) { cout<<"INT Wert eingeben: "; cin >> Element; if(Element<=0) throw NegativesElement();