Accepting decimal numbers

This is a discussion on Accepting decimal numbers within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm having some trouble with code, trying to make my function to be able to accept decimal numbers. But ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    Accepting decimal numbers

    Hi,

    I'm having some trouble with code, trying to make my function to be able to accept decimal numbers.

    But it somehow cannot accept the decimal numbers. It has to be able to accept the decimal number.


    Here's a sample input to test my code:
    Simpson,Homer,6,2,1,7,5.3

    When the decimal number isn't accepted, it prints out "bad"

    Here's my code:

    Code:
    #include <stdio.h> 
    #include<iostream> 
    #include<iomanip>
    #include<sstream>
    #include<fstream>
    #include<string>
    #include<cctype>
    
    using std:: cout;
    using std:: cin;
    using std:: endl;
    using namespace std;
    
    
    bool validation(string& s);
    bool input(string& s);
    
    
    int main()
    {
    	string text;
    	input(text);
    	
    	return 0;
    }
    
    bool validation(string& s)
    {
    	int flag = 0;
    
    	for(string:: size_type i=0; i<s.length(); i++)
    	{
    		if(!isalpha(s[i]) && s[i] != ',' && !isdigit(s[i]))
    		{
    			cout << "bad" << endl;
    			return false;
    		}
    		
    		if(isalpha(s[i]) && s[i] == ',')
    		{
    			if(isdigit(s[i]))
    				flag = 1;
    			else 
    				flag = 1;
    		}
    		else
    			flag = 0;
    	}
    
    	if(flag == true)
    	{
    		cout << "ok" << endl;
    		return true;
    	}
    	return 0;
    }
    
    bool input(string& s)
    {
    	string line;
    	string inputText;
    
    	istringstream iss;
    	cout << "Please enter text\n" << endl;
    	while(getline(cin, line))
    	{
    		iss.clear();
    		iss.str(line);
    
    		if(iss >> inputText)
    		{
    			validation(inputText);
    			cout << "\n\n" << "text"  << endl;
    		}
    	}
    	return true;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Your validation function checks the string to see if it is an integer. You have to add extra code to allow for the decimal point (and possible other things) if you want it to allow decimals as well. Usually whole numbers (called integers) and decimal numbers (called floating point numbers) are validated separately because there are different types in C++ to hold those two types of numbers (int and double are the default types for integers and floating point numbers).

    Also, streams already do validation like this. So unless you have an assignment to write the validate function, you should just use the streams which work very well. To use the stream, you first have to have a variable of the type you expect. Then you read into that type and check the return value. For example, this validates a decimal number:
    Code:
    double decimal_num = 0.0;
    if (!(iss >> decimal_num))
       cout << "bad" << endl;

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    53
    thanks

    but is there a way where my "validation" function can validate whether it is a decimal or not.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course. Decide what your willing to accept for "decimal": does .7 work? What about 7? What about 7.? Once you know what you're willing to take, then you can code that into your function.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    			if(isdigit(s[i]))
    				flag = 1;
    			else 
    				flag = 1;
    Huh?

    Also, as a habit, instead of this:
    Code:
    	for(string:: size_type i=0; i<s.length(); i++)
    use something like this:
    Code:
            size_t len = s.length();
    	for(string:: size_type i=0; i<len; i++)
    This avoids calling the s.length() for every iteration of the loop.

    And as the others pointed out, you should check for a decimal point (and perhaps + and - as well) in when checking if it's a floating point number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 10-28-2006, 04:30 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 01:14 AM
  4. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  5. Help needed with VERY large numbers.
    By jwarner in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2004, 11:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21