Thread: converting string to double

  1. #1
    Unregistered
    Guest

    Question converting string to double

    all of you have been very patient in helping me but now Im faced with a problem of integrating code from people who used different styles. I understand how each works but Im not so sure how to get them to work together.

    The main part of the program simply collects an array of numbers using double as the type. Someone was kind enough to help me with a simple verification function so invalid characters could not be entered.

    Code:
    //Code by: Prelude
    
    bool validate ( std::string val )
    {
      int i, j;
      const std::string valid = "0123456789-.";
      for ( i = 0; i < val.length(); i++ ) {
        for ( j = 0; j < valid.length(); j++ )
          if ( val[i] == valid[j] ) 
            break;
        if ( j == valid.length() )
          return false;
      }
      return true;
    }
    The problem I have is how can I take my arrary using a double data type and ?convert? it to use this function which uses a string.

  2. #2
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    try looking up atol.
    Be a leader and not a follower.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >>Code by: Prelude

    Have you worked any of this out yourself?

    >Someone was kind enough to help me with a simple verification function so invalid characters could not be entered. <

    There's built in stream member functions to do this, and they'll do the conversion for you -

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	double d;
    
    	cin >> d;
    	while(cin.fail())
    	{
    		cout << "char entered ";
    		cin.clear();
    		cin.ignore();
    		cin>>d;
    	}
    
    	return 0;
    }

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    didn't know about that. what other data types does .fail perform checks on?
    Be a leader and not a follower.

  5. #5
    Unregistered
    Guest

    sweet

    hey that works great, the only problem is that if I were to enter something like: RRRR

    it would print "char entered" as many times as total characters (4).

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    151
    >what other data types does .fail perform checks on?<

    It's extendable -

    Code:
    #include <iostream>
    
    using namespace std;
    
    class c
    {
    private:
    	int a;
    	int b;
    public:
    	friend istream& operator>>(istream& is,c& rhs){
    		return is>> rhs.a >> rhs.b;
    	}
    };
    
    
    int main()
    {
    	c d;
    
    	cin >> d;
    	while(cin.fail())
    	{
    		cout << "char entered ";
    		cin.clear();
    		cin.ignore();
    		cin>>d;
    	}
    
    	return 0;
    }
    but fundamentally it only distinguishes between a character and a digit (this is pretty much all you need for the primitive types that it has been pre-defined for)

    >the only problem is that if I were to enter something like: RRRR

    it would print "char entered" as many times as total characters (4).<

    You're allowed to modify it.

  7. #7
    Unregistered
    Guest

    =)

    easier said than done, but Ill give it my best shot

  8. #8
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    ta for explaining.
    Be a leader and not a follower.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >The problem I have is how can I take my arrary using a double data type and ?convert? it to use this function which uses a string.

    Use c_str() (I believe that's it) to get the character array part of the string. Then use atof() to convert to a double.
    Code:
    string val;
    double num;
    .
    .
    num = atof(val.c_str());
    cout << "num:" << num << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  2. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. converting huge #'s in string format to double.
    By ganonl in forum C++ Programming
    Replies: 7
    Last Post: 08-21-2003, 08:41 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM