Wie filtere ich...
-
ICh will nur eine reine Zahleneingabe haben, wie kann ich ausfiltern das er keine Buchstaben zulässt ?
-
Mit if.
Bye, TGGC (Pipe my World.)
-
achwas echt ? Ein bisschen genauer und mit wenig schreibaufwand ! Geht das über Asci Code oder so ?
-
Meinst du sowas?
#include <iostream> #include <sstream> #include <string> template<typename t> t read_from_cin(std::string const &prompt) { std::string zeile; std::stringstream sstr; t ret; do { sstr.clear(); std::cout << prompt << std::flush; std::getline(std::cin, zeile); sstr.str(zeile); sstr >> ret; } while(!sstr); return ret; } // ... int x = read_from_cin<int>("Zahl eingeben: ");
-
oh gott
ICH bin der absolute Noob in c++. ich mein eine ganz einfache abfrage mit if irgendwie...So nach dem Motto:
Wenn der Quatsch eingibt --> wiederholen
-
Mhh, plötzlich verändert sich die Frage. Von wiederholen war bisher keinen Rede.
Bye, TGGC (Pipe my World.)
-
@Nackenspoiler_Joe
Probiersmal selbst und falls es nicht geht, post den entsprechenden codemfg
-
Jupp, genau das macht der Code. Wart grad, ich schreibs nochmal ohne die namespace-präfixe:
#include <iostream> #include <sstream> #include <string> using namespace std; // <-- jetzt braucht man kein std:: mehr template<typename t> t read_from_cin(string const &prompt) { string zeile; stringstream sstr; t ret; do { sstr.clear(); cout << prompt << flush; // Prompt ausgeben getline(cin, zeile); // Eine Zeile einlesen sstr.str(zeile); // Die Zeile an den Stringstream füttern, damit der dann sstr >> ret; // hier geparst werden kann. } while(!sstr); // Das solange, bis es hinhaut. return ret; //Ergebnis zurückgeben } // ... int x = read_from_cin<int>("Zahl eingeben: ");
Der template-Kram ist dazu gedacht, dass du den Typ, den du einlesen willst, selbst angeben kannst. Es ginge zum Beispiel auch:
long y = read_from_cin<long>("Zahl eingeben: "); double d = read_from_cin<double>("Dezimalzahl eingeben: ");
...und so weiter.
-
ja das wiederholen ist ja nicht das problem
-
-- MÜLL --
-
damit kann ich schon etwas mehr anfangen, bzw. gibt es noch einen einfacheren Weg ? Ich bin gerade in der Schule erst mit OOP angefagen ... also tiefes niveau
-
Konolen FAQ schrieb:
// C-Variante ( Dankeschön an Shade Of Mine! ): #include <ctype.h> #include <stdio.h> #include <string.h> #include <conio.h> char * ReadDigits ( char * ReadAll , size_t maxsize , char TerminatingChar ) { char ReadNow = '\0'; int isVZ = 0; int isPB = 0; int isEE = 0; int len = 0; ReadAll[0] = 0; while( ( ReadNow = getch() ) && ( ReadNow != TerminatingChar ) && maxsize ) { len = strlen( ReadAll ); --maxsize; if ( ( ( isdigit ( ReadNow ) ) && ( isVZ = 1 ) ) || ( ( ReadNow == '+' || ReadNow == '-' ) && ( isVZ == 0 ) && ( isVZ = 1 ) ) || ( ( ReadNow == '.' || ReadNow == ',' ) && ( isPB == 0 ) && ( isPB = 1 ) ) || ( ( ReadNow == 'E' || ReadNow == 'e' ) && ( isEE == 0 ) && ( isEE = 1 ) ) ) { ReadAll[len] = ReadNow; ReadAll[len + 1] = 0; putchar ( ReadNow ); } else if ( ( ReadNow == '' ) && ( ReadAll[0] != 0 ) ) { ++maxsize; ReadNow = ReadAll [len - 1]; if ( ( ( ReadNow == '+' || ReadNow == '-' ) && ( isVZ = 0 ) ) || ( ( ReadNow == '.' || ReadNow == ',' ) && ( isPB = 0 ) ) || ( ( ReadNow == 'E' || ReadNow == 'e' ) && ( isEE = 0 ) ) || ( ReadNow ) ) { printf ( " " ); } ReadAll[len - 1] = 0; if ( len == 1 ) { isVZ = 0; } } } return ReadAll; } // C++-Variante: #include <iostream> #include <string> #include <cctype> std::string ReadDigits ( const char TerminatingChar = ' ' ) { std::string ReadAll; char ReadNow ( '\0' ); bool isVZ ( false ); bool isPB ( false ); bool isEE ( false ); while ( ( ReadNow = getch() ) && ( ReadNow != TerminatingChar ) ) { if ( ( ( isdigit ( ReadNow ) ) && ( isVZ = true ) ) || ( ( ReadNow == '+' || ReadNow == '-' ) && ( isVZ == false ) && ( isVZ = true ) ) || ( ( ReadNow == '.' || ReadNow == ',' ) && ( isPB == false ) && ( isPB = true ) ) || ( ( ReadNow == 'E' || ReadNow == 'e' ) && ( isEE == false ) && ( isEE = true ) ) ) { ReadAll += ReadNow; std::cout << ReadNow; } else if ( ( ReadNow == '' ) && ( ReadAll.length() != 0 ) ) { ReadNow = ReadAll [ReadAll.length () - 1]; if ( ( ( ReadNow == '+' || ReadNow == '-' ) && ( isVZ = false ) ) || ( ( ReadNow == '.' || ReadNow == ',' ) && ( isPB = false ) ) || ( ( ReadNow == 'E' || ReadNow == 'e' ) && ( isEE = false ) ) || ( ReadNow ) ) { std::cout << " "; } ReadAll.resize ( ReadAll.length() - 1 ); if ( !ReadAll.length () ) { isVZ = false; } } } return ( ReadAll ); }
-
Man beachte den von CSS zitierten.