Fehlermeldung bei Sortieralgorithmus ausgeben
-
Entschuldigt meine frage, denn ich vermute mal das sie leicht für euch zu beantworten ist, jedoch bin ich noch relativ frisch auf dem gebiet des c++.
Ich möchte eine bestimmte menge zahlen sortieren lassen(was nicht das problem darstellt).//-------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include <stdio.h> #include <stdlib.h> //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { { ListBox1->Clear(); double zahl1=StrToFloat(Edit1->Text); double zahl2=StrToFloat(Edit2->Text); double zahl3=StrToFloat(Edit3->Text); double zahl4=StrToFloat(Edit4->Text); double zahl5=StrToFloat(Edit5->Text); double zahl6=StrToFloat(Edit6->Text); double zahl7=StrToFloat(Edit7->Text); double zahl8=StrToFloat(Edit8->Text); double zahl9=StrToFloat(Edit9->Text); double zahl10=StrToFloat(Edit10->Text); double y = 0; for (double i=1;i<20;i++) { if(zahl1 > zahl2) {y=zahl2;zahl2=zahl1;zahl1=y;} if(zahl2 > zahl3) {y=zahl3;zahl3=zahl2;zahl2=y;} if(zahl3 > zahl4) {y=zahl4;zahl4=zahl3;zahl3=y;} if(zahl4 > zahl5) {y=zahl5;zahl5=zahl4;zahl4=y;} if(zahl5 > zahl6) {y=zahl6;zahl6=zahl5;zahl5=y;} if(zahl6 > zahl7) {y=zahl7;zahl7=zahl6;zahl6=y;} if(zahl7 > zahl8) {y=zahl8;zahl8=zahl7;zahl7=y;} if(zahl8 > zahl9) {y=zahl9;zahl9=zahl8;zahl8=y;} if(zahl9 > zahl10) {y=zahl10;zahl10=zahl9;zahl9=y;} } ListBox1->Items->Add(FloatToStr(zahl1)); ListBox1->Items->Add(FloatToStr(zahl2)); ListBox1->Items->Add(FloatToStr(zahl3)); ListBox1->Items->Add(FloatToStr(zahl4)); ListBox1->Items->Add(FloatToStr(zahl5)); ListBox1->Items->Add(FloatToStr(zahl6)); ListBox1->Items->Add(FloatToStr(zahl7)); ListBox1->Items->Add(FloatToStr(zahl8)); ListBox1->Items->Add(FloatToStr(zahl9)); ListBox1->Items->Add(FloatToStr(zahl10)); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { ListBox1->Clear(); double zahl1=StrToFloat(Edit1->Text); double zahl2=StrToFloat(Edit2->Text); double zahl3=StrToFloat(Edit3->Text); double zahl4=StrToFloat(Edit4->Text); double zahl5=StrToFloat(Edit5->Text); double zahl6=StrToFloat(Edit6->Text); double zahl7=StrToFloat(Edit7->Text); double zahl8=StrToFloat(Edit8->Text); double zahl9=StrToFloat(Edit9->Text); double zahl10=StrToFloat(Edit10->Text); double y = 0; for (double i=1;i<20;i++) { if(zahl1 < zahl2) {y=zahl2;zahl2=zahl1;zahl1=y;} if(zahl2 < zahl3) {y=zahl3;zahl3=zahl2;zahl2=y;} if(zahl3 < zahl4) {y=zahl4;zahl4=zahl3;zahl3=y;} if(zahl4 < zahl5) {y=zahl5;zahl5=zahl4;zahl4=y;} if(zahl5 < zahl6) {y=zahl6;zahl6=zahl5;zahl5=y;} if(zahl6 < zahl7) {y=zahl7;zahl7=zahl6;zahl6=y;} if(zahl7 < zahl8) {y=zahl8;zahl8=zahl7;zahl7=y;} if(zahl8 < zahl9) {y=zahl9;zahl9=zahl8;zahl8=y;} if(zahl9 < zahl10) {y=zahl10;zahl10=zahl9;zahl9=y;} } ListBox1->Items->Add(FloatToStr(zahl1)); ListBox1->Items->Add(FloatToStr(zahl2)); ListBox1->Items->Add(FloatToStr(zahl3)); ListBox1->Items->Add(FloatToStr(zahl4)); ListBox1->Items->Add(FloatToStr(zahl5)); ListBox1->Items->Add(FloatToStr(zahl6)); ListBox1->Items->Add(FloatToStr(zahl7)); ListBox1->Items->Add(FloatToStr(zahl8)); ListBox1->Items->Add(FloatToStr(zahl9)); ListBox1->Items->Add(FloatToStr(zahl10)); } //---------------------------------------------------------------------------Gibt der nutzer aber ausversehen eine buchstaben(oder nichts) ein, bekommt man die fehlermeldung "...ist kein gültiger gleitkommawert".
Wie lass ich in einem solchen fall eine meldung in die listbox schreiben wie "Das ist keine Zahl, bitte eine gültige Zahl eingeben" oder etwas ähnliches?Ein herzliches dankeschön wenn mir jemand weiterhelfen könnte.
MFG Camorra
-
Ich könnte dir jetzt die Antwort drauf geben:
Exception bei StrToFloat abfangen,aber ich glaube, daß du noch nicht soweit bist...
Denn wenn ich deinen Kommentar lese:
Ich möchte eine bestimmte menge zahlen sortieren lassen(was nicht das problem darstellt).
und dann deinen Source-Code sehe, muß ich dir leider sagen, du solltest mit etwas einfacherem anfangen als einem Sortieralgorithmus. Und wenn, dann schau dir mal BubbleSort oder InsertionSort an...
-
Danke schonmal für die antwort.
Ich nehm mir den rat zu herzen und blätter im Buch einfach zurück und such mir n paar andere prpjekte zum üben.
MFG Camorra