tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
4
ZUGRIFFE
3528
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Avatar von UrsaMajor
    UrsaMajor UrsaMajor ist offline Mitglied Bronze
    Registriert seit
    Mar 2005
    Beiträge
    39
    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 -

  2. #2
    Registriert seit
    Oct 2003
    Beiträge
    1.706
    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ß

    RedWing
    Geändert von RedWing (20.08.05 um 01:57 Uhr)
     
    "I'm not deaf, I'm ignoring you"
    ----

  3. #3
    Avatar von UrsaMajor
    UrsaMajor UrsaMajor ist offline Mitglied Bronze
    Registriert seit
    Mar 2005
    Beiträge
    39
    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.

    thx
     
    gruß an alle

    ursa - have fun -

  4. #4
    FireFlow FireFlow ist offline Mitglied Gold
    Registriert seit
    Feb 2004
    Beiträge
    213
    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)
     
    --//--//--// My StillLife! //--//--//--
    Obere URL dient nur als Google-Push )

  5. #5
    Registriert seit
    Oct 2003
    Beiträge
    1.706
    Das was RedWing da gepostet hat gibts schon (von der Idee her), nennt sich boost::lexical_cast<>
    Sollte mich vielleicht doch mal a weng mit boost auseinandersetzen

    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.
    Was genau verstehst du denn nicht?

    Gruß

    RedWing
     
    "I'm not deaf, I'm ignoring you"
    ----

Ähnliche Themen

  1. ROUND(x, d) Long oder Integer statt Double! Liegt es an Hibernate?
    Von ThirdKeeper im Forum Enterprise Java (JEE, J2EE, Spring & Co.)
    Antworten: 4
    Letzter Beitrag: 13.11.09, 13:36
  2. Datentyp einer Scrollbar von Integer auf Long
    Von MatMagic im Forum Visual Basic 6.0
    Antworten: 0
    Letzter Beitrag: 22.04.09, 23:00
  3. String in Long umwandeln
    Von chickenwings im Forum Java
    Antworten: 4
    Letzter Beitrag: 06.10.06, 15:14
  4. long Datum in String
    Von Nispuk im Forum C/C++
    Antworten: 0
    Letzter Beitrag: 16.04.05, 22:44
  5. Line too long (5kb --> String)
    Von Arne Buchwald im Forum Delphi, Kylix, Pascal
    Antworten: 2
    Letzter Beitrag: 02.06.02, 22:02