Weg finder
-
Ja, aber er nimmt immer die Case1'er und die anderen findet der garnicht. Wieso? und wenn ich case3 wegnehme, kommt 2 raus, aber wenn ich case1 oder 2 wegnehme immer noch 3.. das ist komisch. Warum?
#include <cstdlib> #include <iostream> using namespace std; char start; int shortest; int Ziel=0; char *station; 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; int main(int argc, char *argv[]) { station_h=4137; station="a"; while(Ziel==0) { ///////////////////////////////////////////////////////////////////////////////// if (station="a") { if(station_a==4137) { cout << weg; Ziel=1; } station_a=1; switch(3) { case 1: if(station_b!=1){station="b"; weg++; break;} case 2: if(station_c!=1){station="c"; weg++; break;} case 3: if(station_d!=1){station="d"; weg++; break;} } } ///////////////////////////////////////////////////////////////////////////////// if (station="b") { if(station_b==4137) { cout << weg; Ziel=1; } station_b=1; switch(2) { case 1: if(station_g!=1){station="g"; weg++; break;} case 2: if(station_a!=1){station="a"; weg++; break;} } } ///////////////////////////////////////////////////////////////////////////////// if (station="c") { if(station_c==4137) { cout << weg; Ziel=1; } station_c=1; switch(3) { case 1: if(station_h!=1){station="h"; weg++; break;} case 2: if(station_a!=1){station="a"; weg++; break;} case 3: if(station_f!=1){station="f"; weg++; break;} } } ///////////////////////////////////////////////////////////////////////////////// if (station="d") { if(station_d==4137) { cout << weg; Ziel=1; } station_d=1; switch(2) { case 1: if(station_f!=1){station="f"; weg++; break;} case 2: if(station_a!=1){station="a"; weg++; break;} } } ///////////////////////////////////////////////////////////////////////////////// if (station="g") { if(station_g==4137) { cout << weg; Ziel=1; } station_g=1; switch(2) { case 1: if(station_b!=1){station="b"; weg++; break;} case 2: if(station_h!=1){station="h"; weg++; break;} } } ///////////////////////////////////////////////////////////////////////////////// if (station="h") { if(station_h==4137) { cout << weg; Ziel=1; } station_h=1; switch(2) { case 1: if(station_g!=1){station="g"; weg++; break;} case 2: if(station_c!=1){station="c"; weg++; break;} } } ///////////////////////////////////////////////////////////////////////////////// if (station="f") { if(station_f==4137) { cout << weg; Ziel=1; } station_f=1; switch(2) { case 1: if(station_c!=1){station="c"; weg++; break;} case 2: if(station_d!=1){station="d"; weg++; break;} } } } cin>>lol; }
-
station="f"
sowas ist eine zuweisung. das ist zwar gültig in einem if-statement, leistet aber wahrscheinlich nicht, was du willst. dei zuweisung liefert nämlich den wert der variablen nach der zuweisung zurück. da dieser ungleich 0 ist, interpretiert c++ das als "true".
da station ein cstring ist, musst du auch die entsprechende methode zum vergleichen von cstrings verwenden. http://cppreference.com/stdstring/strncmp.html
-
Weil switch nunmal nicht so funktionniert.
An switch übergibst du eine Variable, keine Zahl.
Und es ist nicht so, dass einer meckert. Es ist nur halt so, dass in dem Code recht viele Fehler stecken, was darauf schlieszen lässt dass du erst vor kurzem mit C++ angefangen hast. Daher empfiehlt man dir auch dich einmal durch ein Tutorial zu lesen (gibt ja genug im Internet, sollte auch hier einige Links geben) Danach kannst du einen groszen teil deiner Fehler selbst beheben. Wegfindung scheint halt ganz einfach noch (im Augenblick) etwas zu schwer für deinen Kenntnisstand zu sein.
-
Also, Danke! Hab ich total vergessen, ist aber klar, doch hab ich dadurch einen Fehler bekommen, Ich habe versucht, und versucht, aber der Fehler bleibt. Also hier erstmal der COde nun:
#include <cstdlib> #include <iostream> #include <string.h> using namespace std; char start; int shortest; int Ziel=0; string station[100]; 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; int main(int argc, char *argv[]) { station_h=4137; string station="a"; while(Ziel==0) { ///////////////////////////////////////////////////////////////////////////////// if( strcmp( station, "a" ) == 0 ) { if(station_a==4137) { cout << weg; Ziel=1; } station_a=1; switch(3) { case 1: if(station_b!=1){station="b"; weg++; break;} case 2: if(station_c!=1){station="c"; weg++; break;} case 3: if(station_d!=1){station="d"; weg++; break;} } }usw...
nun der Fehler:
C:\Dev-Cpp\Projects\LUB\main.cpp In function `int main(int, char**)': C:\Dev-Cpp\Projects\LUB\main.cpp no matching function for call to `strncmp(std::string&, const char[2])' C:\Dev-Cpp\include\string.h:51 candidates are: int strncmp(const char*, const char*, size_t)also was ist los? Ich versteh den Fehler nich. Egal ob ich Station als string oder char nehme.
-
strncmp erwartet drei parameter. sagt dir der compiler ja auch. als dritten parameter erwartet es die anzahl der zeichen, die verglichen werden sollen.
vielleicht ist es für dich empfehlenswert, gleich komplett auf std::string umzusteigen. für die kannst du nämlich einfach == verwenden.
string station[100];das definiert übrigens einen array von string mit 100 elementen und keinen string mit 100 zeichen.
-
Also, ich hab es vertsnaden, doch mal eine Frage, wie müsste ich es machen um den User einen Namen einzugeben, und den zu vergleichen. Ich hab das letztenms noch I-Wo gebraucht, doch finde ich die Datei leider nich mehr wo das stand!
-
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.