Problem!!!
-
Hallo leute ich hab da ein völig unkompliziertes programm geschrieben:
#include <iostream> #include <conio.h> #include <string> using namespace std; void main(void) { string Name; string Antwort; bool RA; cout << "Ciao!" << endl; cout << "Wie heissisch du? "; cin >> Name; cout << Name << ", gaht's dir guet? "; do { cin >> Antwort; RA = true; if (Antwort[0] == 'j') cout << "Schad!" << endl; else if (Antwort[0] == 'J') cout << "Schad!" << endl; else if (Antwort == "nei") cout << "Schön, aber verzell mir ja nöd dini scheiss Problem!" << endl; else if (Antwort == "Nei") cout << "Schön, aber verzell mir ja nöd dini scheiss Problem!" << endl; else if (Antwort == "es") cout << "Das isch Pech, interresiert mich aber nöd würklich!" << endl; else if (Antwort == "Es") cout << "Das isch Pech, interresiert mich aber nöd würklich!" << endl; else if (Antwort == "cha") cout << "Schad da muen ich wohl chli nahelfe!" << endl; else if (Antwort == "Cha") cout << "Schad da muen ich wohl chli nahelfe!" << endl; else if (Antwort == "chönnt") cout << "Freut mich" << endl; else if (Antwort == "Chönnt") cout << "Freut mich" << endl; else { RA = false; cerr << "Sorry ha dich nöd verstande, schrieb nomal, aber gfälligst " << "so das ich's verstah!" << endl; } } while (!RA); cout << "Also Tschüss!" << endl; getch (); }Das Problem ist nun wen ich eine falsche antwort mit mehreren worten eingebe kommt der cerr-Teil fast am schluss so viel mal wie ich worte geschrieben habe!
Wie kann ich das ändern?
-
Hallo,
1. Aussagekräftige Titel helfen den anderen dein Problem direkt besser einzuschätzen
2. main() hat als Rückgabetyp int und nicht void und zu guter letzt
3. Probier mal die Eingabe mit getline zu machen, dann liest er in einem Rutsch die gesamte Zeile aus:#include <iostream> #include <conio.h> #include <string> using namespace std; int main(void) { string Name; string Antwort; bool RA; cout << "Ciao!" << endl; cout << "Wie heissisch du? "; getline(cin,Name); cout << Name << ", gaht's dir guet? " << endl; do { getline(cin, Antwort); RA = true; if (Antwort[0] == 'j') cout << "Schad!" << endl; else if (Antwort[0] == 'J') cout << "Schad!" << endl; else if (Antwort == "nei") cout << "Schön, aber verzell mir ja nöd dini scheiss Problem!" << endl; else if (Antwort == "Nei") cout << "Schön, aber verzell mir ja nöd dini scheiss Problem!" << endl; else if (Antwort == "es") cout << "Das isch Pech, interresiert mich aber nöd würklich!" << endl; else if (Antwort == "Es") cout << "Das isch Pech, interresiert mich aber nöd würklich!" << endl; else if (Antwort == "cha") cout << "Schad da muen ich wohl chli nahelfe!" << endl; else if (Antwort == "Cha") cout << "Schad da muen ich wohl chli nahelfe!" << endl; else if (Antwort == "chönnt") cout << "Freut mich" << endl; else if (Antwort == "Chönnt") cout << "Freut mich" << endl; else { RA = false; cerr << "Sorry ha dich nöd verstande, schrieb nomal, aber gfälligst " << "so das ich's verstah!" << endl; } } while (!RA); cout << "Also Tschüss!" << endl; getch (); }MfG Jaschi
-
Ja danke hat geklappt, das mit dem namen mach ich nächstes mal
-
Noch ein Tipp:
cerr << "Sorry ha dich nöd verstande, schrieb nomal, aber gfälligst " << "so das ich's verstah!" << endl;Der Präprozessor sorgt schon selbst dafür, dass String Literale zusammengefügt werden.
cerr << "Sorry ha dich nöd verstande, schrieb nomal, aber gfälligst " "so das ich's verstah!" << endl;Das reicht also vollkommen aus und spart uns einen extra op<< Aufruf.