ERLEDIGT
NEIN
NEIN
ANTWORTEN
4
4
ZUGRIFFE
3528
3528
EMPFEHLEN
-
Hallo zusammen,
ich möchte beim devc++ in der console eine zahl eingeben da aber nicht jeder so clever ist eine zahl einzugeben wollte ich erst das man auch strings eingeben kann. wenn das nun eine zahl ist soll sie umgewandelt werden. ist es aber ein zeichen dann soll eine exception kommen. wie kann ich das machen?gruß an alle
ursa - have fun -
-
Hallo,
schaumal hier:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream> #include <sstream> using namespace std; class NumberFormatException{ private: string error; public: NumberFormatException(const string& error){ this->error = error; } const string& toString(){ return error; } }; template <typename T>class StringToNumberConverter{ private: string number; public: StringToNumberConverter(const string number){ this->number = number; } T convert() throw (NumberFormatException){ for(int i = 0; i < number.size(); i++){ if(number[i] < '0' || number[i] > '9') if(number[i] != '.') throw NumberFormatException("No valid input format"); } istringstream is(number); T result; is >> result; return result; } }; int main(){ string input; cout << "Please insert a number: " << endl; cin >> input; StringToNumberConverter<long> conv(input); try{ cout << conv.convert() << endl; }catch(NumberFormatException& e){ cout << "An error occured: "; cout << e.toString() << endl; return 1; } }
//edit: Kannst du auch auf floats, doubles etc anwenden...
Gruß
RedWingGeändert von RedWing (20.08.05 um 01:57 Uhr)
"I'm not deaf, I'm ignoring you"
----
-
hi,
vielen dank. das habe ich gesucht. gibt es irgendwo eine erklärung für dieses teil. sieht ja doch auf den ersten blick logisch aus aber ich will mal genau wissen wie das alles funktioniert einige sachen sind noch unklar.
thxgruß an alle
ursa - have fun -
-
Das was RedWing da gepostet hat gibts schon (von der Idee her), nennt sich boost::lexical_cast<>
Hier mal nen Beispiel das ich wirklich einfacher anzuwenden:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <iostream> #include <string> #include "boost/lexical_cast.hpp" using namespace std; using boost::lexical_cast; int main() { // Eingabe string in; getline(cin, in, '\n'); // Umwandlung mit boost::lexical_cast try { double val = lexical_cast<double>(in); cout << "Value: " << val; } catch(exception& err) { cout << "Error: " << err.what(); } }Geändert von FireFlow (20.08.05 um 06:24 Uhr)
-
Sollte mich vielleicht doch mal a weng mit boost auseinandersetzenDas was RedWing da gepostet hat gibts schon (von der Idee her), nennt sich boost::lexical_cast<>

Was genau verstehst du denn nicht?vielen dank. das habe ich gesucht. gibt es irgendwo eine erklärung für dieses teil. sieht ja doch auf den ersten blick logisch aus aber ich will mal genau wissen wie das alles funktioniert einige sachen sind noch unklar.
Gruß
RedWing"I'm not deaf, I'm ignoring you"
----
Ähnliche Themen
-
ROUND(x, d) Long oder Integer statt Double! Liegt es an Hibernate?
Von ThirdKeeper im Forum Enterprise Java (JEE, J2EE, Spring & Co.)Antworten: 4Letzter Beitrag: 13.11.09, 13:36 -
Datentyp einer Scrollbar von Integer auf Long
Von MatMagic im Forum Visual Basic 6.0Antworten: 0Letzter Beitrag: 22.04.09, 23:00 -
String in Long umwandeln
Von chickenwings im Forum JavaAntworten: 4Letzter Beitrag: 06.10.06, 15:14 -
long Datum in String
Von Nispuk im Forum C/C++Antworten: 0Letzter Beitrag: 16.04.05, 22:44 -
Line too long (5kb --> String)
Von Arne Buchwald im Forum Delphi, Kylix, PascalAntworten: 2Letzter Beitrag: 02.06.02, 22:02





Zitieren
Login






