Thread: Would anyone do this differently?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    Would anyone do this differently?

    i want to convert a string into a numerical value but there can be different data types...NumberType is <int || float || long || long double>
    Code:
    template<class NumberType> NumberType parse(const char &stringOfNumbers)
    {
          //some parsing algorithm
    
        //isn't there a header with these defintions?
        NumberType convertedNumber;
        int  sizeFloat = sizeof(float); 
        int longSize = sizeof(long);
        int longdoubleSize = sizeof(long double);
    
        switch(sizeof(NumberType))
        {
             case floatSize:
                        convertedNumber = atof(stringOfNumber, 10);               //stringOfNumbers = "1234.54"
                        break;
             case longSize:
                        convertedNumber = atol(stringOfNumbers, 10);
                         break;
               //.....................
         }
    
            
    
    }
    Last edited by misplaced; 09-28-2004 at 03:41 PM.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    That's not quite right. You are passing in a const reference to a single character. Also, you cannot count on floats, longs, doubles, etc, being different sizes.

    This type of function is quite easy using a stringstream. Any type that has an operator>> defined will work.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    See http://www.parashift.com/c++-faq-lit....html#faq-38.1, 38.2, and 38.3 for examples of how to do this.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    First create specialized functions, then call the funtion you want.
    Code:
    template<class type> type convert(type,type){
    //..... general case
    }
    //specializations
    template<float> float convert(const char* s){
        return atof(s,10);
    }
    template<double> double convert(const char* s){/**/}
    template<short> short convert(const char* s){/**/}
    template<long> long convert(const char* s){
        return atoi(s,10);
    }
    template<long long int> long long int convert(const char* s){/**/}
    /*Then...*/
    template<class NumberType> NumberType parse(const char &stringOfNumbers)
    {
        //call specialized funtion...
        NumberType convertedNumber = convert<NumberType>(stringOfNumber);
    /*..............................*/        
    }

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    ok...thanks guys...i'll look into it (string stream & parashift)

    i guess erath's version would work too...i wonder why i didn't think of that (rather it's the best way or not, i ussually would think of something like that)...


    ...but how would different (numerical) data types be the same size?...i'm just curious, not trying to argue.

  6. #6
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    this is a example to convert string into number:
    Code:
    #include<iostream>
    #include<string>
    #include<sstream>
    using namespace std;
    
    class calculation
    {
    private:
    
    long double num1,num2;
    char theoperator;
    void calculate()
    {
    switch(theoperator)
    {
    case '+':
    addition();
    break;
    case '-':
    substraction();
    break;
    case '*':
    multiplication();
    break;
    case '/':
    if(!num2)
    {cout<<"\ndenominator shouldn't be zero"<<endl;return;}
    division();
    break;
    }
    cout<<"="<<num1<<endl;
    }
    void addition()
    {
    num1+=num2;
    }
    void substraction()
    {
    num1-=num2;
    }
    void multiplication()
    {
    num1*=num2;
    }
    void division()
    {
    num1/=num2;
    }
    
    public:
    
    void check(string input)
    {
    string num;
    if(input.find("+",0)<input.size())
    {
    theoperator='+';
    num=input.substr(0,input.find("+",0));
    istringstream istream1(num);
    istream1>>num1;
    num=input.substr(input.find("+",0)+1,input.size()-input.find("+",0)-1);
    istringstream istream2(num);
    istream2>>num2;
    }
    else if(input.find("-",0)<input.size())
    {
    theoperator='-';
    num=input.substr(0,input.find("-",0));
    istringstream istream1(num);
    istream1>>num1;
    num=input.substr(input.find("-",0)+1,input.size()-input.find("-",0)-1);
    istringstream istream2(num);
    istream2>>num2;
    }
    else if(input.find("*",0)<input.size())
    {
    theoperator='*';
    num=input.substr(0,input.find("*",0));
    istringstream istream1(num);
    istream1>>num1;
    num=input.substr(input.find("*",0)+1,input.size()-input.find("*",0)-1);
    istringstream istream2(num);
    istream2>>num2;
    }
    else if(input.find("/",0)<input.size())
    {
    theoperator='/';
    num=input.substr(0,input.find("/",0));
    istringstream istream1(num);
    istream1>>num1;
    num=input.substr(input.find("/",0)+1,input.size()-input.find("/",0)-1);
    istringstream istream2(num);
    istream2>>num2;
    }
    else {cout<<"no operator"<<endl;return;}
    calculate();
    }
    
    };
    
    main()
    {
    string input;
    calculation hoho;
    while(1)
    {
    cin>>input;
    hoho.check(input);
    }
    }

    blow me ... ...

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    how abt something like this

    Code:
    #include <iostream>
    #include <sstream>
    
    
    using namespace std;
    
    
    template<class NumberType> 
    NumberType parse(string a,NumberType b)
    {
     
    
        NumberType convertedNumber;
        stringstream ss(a);
        ss >> b;
        return b;        
    }    
    
    
    
    int main( )
    {
        double num;
        num=parse("1996.25",num);
        cout <<"\n"<< num;
        
        
        int num1;
        num1=parse("1996.25",num1);
        cout <<"\n"<< num1;
        
        
        getchar();
        return 0;
    }
    the 2nd parameter in the parse function is to tell that function what type of data to return...

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    that seems a bit too easy

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    It's not. And that's the beauty of templates.
    Last edited by Hunter2; 09-29-2004 at 11:02 AM. Reason: Link wasn't useful...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does it run differently?
    By Leftos in forum C Programming
    Replies: 26
    Last Post: 12-12-2007, 06:03 AM
  2. Help with code behaving differently with different computers...
    By conright in forum Windows Programming
    Replies: 4
    Last Post: 01-25-2003, 05:57 PM
  3. Does windows XP handle color depth differently than ME or 2000?
    By Stormkoopa in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2002, 06:10 AM
  4. Images displayed differently in Opera????
    By Imperito in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-26-2002, 02:50 PM
  5. thinking differently doesn't pay off until you are dead
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 01-21-2002, 01:43 AM