Weg finder
-
Also ich habs gefunden und mal so gemacht wie ich denke. Aber wenn ich nun als User "a" eintippe macht das Programm nicht mehr weiter. Und da ich station_h als Ziel gesetzt habe (Durch die Nr. 4137) müsste er mir eigentlich eine "1" anzeigen, da ja von "a" weg++; ausgeführt wird. Doch auch dass passiert nicht. Hier der Code:
#include <cstdlib> #include <iostream> #include <string.h> using namespace std; char start; int shortest; int Ziel=0; int weg=0; int lol; int station_a=0; int station_b=0; int station_c=0; int station_d=0; int station_e=0; int station_f=0; int station_g=0; int station_h=0; string a= "a"; string b= "b"; string c= "c"; string d= "d"; int main(int argc, char *argv[]) { station_b=4137; string station; getline(cin, station); while(Ziel==0) { ///////////////////////////////////////////////////////////////////////////////// if( station.compare("a") == 1 ) { if(station_a==4137) { cout << weg; Ziel=1; } station_a=1; switch(3) { case 1: if(station_b!=1){station.erase(); station.append(B); weg++; break;} case 2: if(station_c!=1){station.erase(); station.append(c); weg++; break;} case 3: if(station_d!=1){station.erase(); station.append(d); weg++; break;} } }usw...
-
switch(3) ist absoluter Unfug! (Warum wurde bereits erklärt)
-
Weil switch immer noch nicht so funktionniert. Versuch es mal mit switch(station_a)
-
Also ich habe alle switch() mit Switch(station_"buchstabe") ersetzt. Doch funktioniert es immer noch nicht, er springt immer noch nicht weiter.
warum?
-
Also hier der Code. Es kommen zwar komische ergebnisse raus so wie 8,7,1,0 und so, scheint aber zu funktionieren, doch wenn ich z.B. B als Ziel setze, und der User "d" als startpunkt eintippt, scheint es wieder nicht zu funktionieren, da ich dann kein Ergebnmis erhalte. und noch was, Woher kommt die Zahl 8 etc.?? und wie kann ich dem jetzt sagen der soll alle Wege finden und schaun wo der kürzeste lang geht? Müsste danit doch auch gehen oder?
#include <cstdlib> #include <iostream> #include <string.h> using namespace std; char start; int shortest; int Ziel=0; int weg=0; int lol; int station_a=0; int station_b=0; int station_c=0; int station_d=0; int station_e=0; int station_f=0; int station_g=0; int station_h=0; string a= "a"; string b= "b"; string c= "c"; string d= "d"; string f= "f"; string g= "g"; string h= "h"; int main(int argc, char *argv[]) { station_h=4137; string station; getline(cin, station); while(Ziel==0) { ///////////////////////////////////////////////////////////////////////////////// if( station.compare("a") == 0) { if(station_a==4137) { cout << weg; Ziel=1; } station_a=1; switch(station_a) { case 1: if(station_b!=1){station.erase(); station.append(b); weg++; } case 2: if(station_c!=1){station.erase(); station.append(c); weg++; } case 3: if(station_d!=1){station.erase(); station.append(d); weg++; } } } ///////////////////////////////////////////////////////////////////////////////// if( station.compare("b") == 0) { if(station_b==4137) { cout << weg; Ziel=1; } station_b=1; switch(station_b) { case 1: if(station_g!=1){station.erase(); station.append(g); weg++; } case 2: if(station_a!=1){station.erase(); station.append(a); weg++; } } } ///////////////////////////////////////////////////////////////////////////////// if( station.compare("c") == 0) { if(station_c==4137) { cout << weg; Ziel=1; } station_c=1; switch(station_c) { case 1: if(station_h!=1){station.erase(); station.append(h); weg++; } case 2: if(station_a!=1){station.erase(); station.append(a); weg++; } case 3: if(station_f!=1){station.erase(); station.append(f); weg++; } } } ///////////////////////////////////////////////////////////////////////////////// if( station.compare("d") == 0) { if(station_d==4137) { cout << weg; Ziel=1; } station_d=1; switch(station_d) { case 1: if(station_f!=1){station.erase(); station.append(f); weg++; } case 2: if(station_a!=1){station.erase(); station.append(a); weg++; } } } ///////////////////////////////////////////////////////////////////////////////// if( station.compare("g") == 0) { if(station_g==4137) { cout << weg; Ziel=1; } station_g=1; switch(station_g) { case 1: if(station_b!=1){station.erase(); station.append(b); weg++; } case 2: if(station_h!=1){station.erase(); station.append(h); weg++; } } } ///////////////////////////////////////////////////////////////////////////////// if( station.compare("h") == 0) { if(station_h==4137) { cout << weg; Ziel=1; } station_h=1; switch(station_h) { case 1: if(station_g!=1){station.erase(); station.append(g); weg++; } case 2: if(station_c!=1){station.erase(); station.append(c); weg++; } } } ///////////////////////////////////////////////////////////////////////////////// if( station.compare("f") == 0) { if(station_f==4137) { cout << weg; Ziel=1; } station_f=1; switch(station_f) { case 1: if(station_c!=1){station.erase(); station.append(c); weg++; } case 2: if(station_d!=1){station.erase(); station.append(d); weg++; } } } } cin>>lol; }MFG: LPP1991
PS. Danke dass ihr eure zeit opfert mir zu helfen.
-
Hier meine Skizze:
b------g\
| \
| h
| /
a------c/
| |
| |
| |
d------f
-
Hier noch einmal aber hoffentlich richtig!^^
b------g\ | \ | h | / a------c/ | | | | | | d------fHabe zuvor vergessen Vorschau anzusehen, sorry!
-
Schmeiß den Pasta-Code weg und denk dir was Neues aus. Du könntest vielleicht erstmal mit einem Design starten. Welche Klassen brauchst du zum Beispiel?
Wenn du nicht mit Klassen umgehen kannst solltest du das tunlichst lernen.