Mein Programm "hängt"
-
Hallo,
ich habe ein etwas größeres Lottoprogramm mit 5 Funktionen geschrieben, dass beim Starten etwas "hängt".
Es könnte auch am PC liegen, wobei ich bezweifle, dass Vista mit so einfachen kleinen Programmen schwierigkeiten bekommt.
Ich würde mich über eure Meinung freuen.
Ich führe mein Programm aus und es dauert eine Weile, bis ich den ersten Wert eingeben kann.
Ich hab den Quellcode etwas gekürzt(Text Ausgabe etc.) Hier gibt es für den, der möchte den vollen Code:
http://rapidshare.de/files/38148106/lotto.zip.html
#include <cstdlib> #include <iostream> using namespace std; void tipp(int &t1, int &t2, int &t3, int &t4, int &t5, int &t6) { system("color 3f"); cout<<"\t\t\t****************************"<<endl; cout<<"\t\t\tDie Ziehung der Lottozahlen"<<endl; cout<<"\t\t\t****************************"<<endl; cout<<"\n\n\t\t\t-------- 6 aus 49 ----------"<<endl; cout<<"\n\nIhre Tipp-Zahlen:"<<endl; cout<<"\n"; cout<<"\n\tTipp1: "; cin>>t1; cout<<"\n\tTipp2: "; cin>>t2; cout<<"\n\tTipp3: "; cin>>t3; cout<<"\n\tTipp4: "; cin>>t4; cout<<"\n\tTipp5: "; cin>>t5; cout<<"\n\tTipp6: "; cin>>t6; cout<<"\n\n"; } // Tipp void ziehung(int &z1, int &z2, int &z3, int &z4, int &z5, int &z6) { srand( time(NULL) ); z1 =(rand() % 49)+1; z2 =(rand() % 49)+1; z3 =(rand() % 49)+1; z4 =(rand() % 49)+1; z5 =(rand() % 49)+1; z6 =(rand() % 49)+1; } // Ziehung der Zahlen void kontrolle(int &t1, int &t2, int &t3, int &t4, int &t5, int &t6) { if (t1<0 || t1>49) t1=99; else t1=t1; if (t2<0 || t2>49) t2=99; else t2=t2; if (t3<0 || t3>49) t3=99; else t3=t3; if (t4<0 || t4>49) t4=99; else t4=t4; if (t5<0 || t5>49) t5=99; else t5=t5; if (t6<0 || t6>49) t6=99; else t6=t6; if (t1==99 || t2==99 || t3==99 || t4==99 || t5==99 || t6==99 ) { system("color 4e"); cout<<"\n\nFehler bei der Eingabe der Zahlen !!!"<<endl; cout<<"\tEingabe bitte wiederholen ! \n\n\n"<<endl; system("pause"); cout<<"\n\n"; tipp(t1, t2, t3, t4, t5, t6); kontrolle(t1, t2, t3, t4, t5, t6); } else { system("color 3f");} } // Kontrolle der Zahlen void ausgabe(int &t1, int &t2, int &t3, int &t4, int &t5, int &t6, int &z1, int &z2, int &z3, int &z4, int &z5, int &z6) { int i=0; int n=100; cout<<"\t#################################################"<<endl; cout<<"\t\t Die Ergebnisse der Lottoziehung"<<endl; cout<<"\t#################################################"<<endl; cout<<"\n\n\a"; cout<<"\tHier die Lottozahlen:"<<endl; cout<<" "<<z1<<" "<<z2<<" "<<z3<<" "<<z4<<" "<<z5<<" "<<z6<<endl; cout<<"\n\tHier zur Wiederholung ihre Zahlen:"<<endl; cout<<" "<<t1<<" "<<t2<<" "<<t3<<" "<<t4<<" "<<t5<<" "<<t6<<endl; if(t1==z1 || t1==z2 || t1==z3 || t1==z4 || t1==z5 || t1==z6) i++; if(t2==z1 || t2==z2 || t2==z3 || t2==z4 || t2==z5 || t2==z6) i++; if(t3==z1 || t3==z2 || t3==z3 || t3==z4 || t3==z5 || t3==z6) i++; if(t4==z1 || t4==z2 || t4==z3 || t4==z4 || t4==z5 || t4==z6) i++; if(t5==z1 || t5==z2 || t5==z3 || t5==z4 || t5==z5 || t5==z6) i++; if(t6==z1 || t6==z2 || t6==z3 || t6==z4 || t6==z5 || t6==z6) i++; n=(i*100)/6; cout<<"\n\tSie haben "<<i<<" Richtige"<<endl; cout<<"\tSie haben somit eine Trefferquote von: "<<n<<"%"<<endl; cout<<"\n\n"; } // Gewinnausgabe int main() { int t1, t2, t3, t4, t5, t6; int z1, z2, z3, z4, z5, z6; int i=1; while(i==1) { tipp(t1, t2, t3, t4, t5, t6); ziehung(z1,z2, z3, z4, z5, z6); kontrolle(t1, t2, t3, t4, t5, t6); ausgabe(t1, t2, t3, t4, t5, t6, z1, z2, z3, z4, z5, z6); system("PAUSE"); cout<<"\n\n\t Nochmal ?"<<endl; cout<<"\n\tJa =1 und Nein=2"<<endl; cout<<"\t1 oder 2: "; cin>>i; } return EXIT_SUCCESS; }Vielen Dank schonmal im Vorraus.
-
Warum dein Programm hängt, da kann ich nur raten.
Wo genau hängt es denn, d.h. siehst du schon einige Ausgaben oder einfach beim Start?Eine Vermutung von mir: der system()-Aufruf könnte einige Zeit dauern, da dort ja ein anderes Programm gestartet wird.
P.S. und als nächstes lernen wir Arrays...
-
Also:
Man startet das Programm und dann kommt die "Textüberschrift" (Lotto, 6 aus 49)
Nun wird nach dem ersten Tipp gefragt:
cout<<"\n\nIhre Tipp-Zahlen:"<<endl; cout<<"\n"; cout<<"\n\tTipp1: "; cin>>t1;und genau da hängt er. Also ich kann eine Zahl eingeben, diese wird aber erst nach wenigen Sekunden angezeigt. Ich kann auch z.B 5 eingben und danach gleich "Enter". Hier hängt er ebenfalls. Nach der zweiten Zahl wird erst nach ein paar Sekunden gefragt.
Zwecks Arrays: Ja diese stehen tatsächlich als nächstes auf dem Programm, leider habe ich vergessen mein Scipt mit in den Urlaub zu nehmen
.
-
Bei mir funktioniert das Programm mit mingw
unter XP tadellos.Wenn du grad bei Arrays bist, kannst du dir
Schleifen gleich mit ansehen.PS: Schöne Farbe.

-
Welchen Compiler (bzw. IDE) verwendest du denn?
-
Ich hab mit dem Bloodshed Dev-C++ gearbeitet.
Da effe_eichelt keine Probleme mit seinem Kompiler hatte, wird es wohl an dev-c++ liegen, wobei ich das leider nicht so ganz verstehe.
Ich werde mich nach den Ferien ein wenig mit den Arrays beschäftigen und das Programm optimieren.
Ist eigentlich auch nur eine Übung um externe Funktionen einzubinden.
Aber gut, danke für eure Hilfe.

P.S Hat jemand lust die Tipp/Gewinnzahlen der größe nach zu sortieren?
-
Bei mir hängt da nichts (Windows XP, Dev-C++ mit mingw).
Muss also an Vista liegen
Ach ja, sortieren (allerdings mit Arrays und Schleifen):
void sortieren(int &t1, int &t2, int &t3, int &t4, int &t5, int &t6) { int* zahlen[6]={&t1,&t2,&t3,&t4,&t5,&t6}; int sortiert[6]; for (int i=0;i<6;i++) { int kleinste=999,index=-1; for (int j=0;j<6;j++) { if (*zahlen[j]!=0 && *zahlen[j]<kleinste) { kleinste=*zahlen[j]; index=j; } } sortiert[i]=*zahlen[index]; *zahlen[index]=0; } for (int i=0;i<6;i++)*zahlen[i]=sortiert[i]; }und dann noch
sortieren(z1,z2,z3,z4,z5,z6); sortieren(t1,t2,t3,t4,t5,t6);vor der Ausgabe.
-
Lern erst mal die Arrays, dann kannst du mittels std::sort() aus <algorithm> ganz einfach sortieren (lassen).
-
und die kontrolle sollte früher erfolgen. warum schon daten 'verarbeiten' wenn diese falsch sein könnten. am besten wäre es sogar wenn du die daten direkt nach der eingabe überprüfst.