Floating point

This is a discussion on Floating point within the C++ Programming forums, part of the General Programming Boards category; Is it possible to check to see if a user has used a floating point number as of an integer?...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    14

    Floating point

    Is it possible to check to see if a user has used a floating point number as of an integer?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Take input as a string. Attempt to convert the string to an int. If it fails, try to convert it to a floating point value. If it is a value that could be either, such as "42", make your own rule for handling it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    ok thank you i've done this soo far....

    Code:
    int check_string(string x1){
    	unsigned int pos = x1.find(".",0);
    	int y = 0;
    	if(pos != string::npos){
    		cout << "Invalid input! Conversion Failed"<<endl;
    		y = 1;
    	}
    
    	return (y);
    }
    which is all dandy.... but how do i check if the string contains letters? because i need to convert it back to an integer

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int main()
    {
    
    	double input;
    
    	cout << "Enter a number: ";
    	cin >> input;
    
    	if ( (input / static_cast<int>(input)) == 1) 
    	{	
    		//Is int...do whatever
    	}
    
    	else {}
    
    
    
    	return 0;
    
    }
    Last edited by Enahs; 12-04-2005 at 02:06 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Or !isdigit(). (<cctype>)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    using namespace std; // for homework and toy programs
    
    int main()
    {
       ifstream file("file.txt");
       string info;
       while ( getline(file, info) )
       {
          istringstream iss(info);
          char c;
          float afloat;
          if ( (iss >> afloat) && !iss.get(c) )
          {
             cout << " afloat = " << afloat << endl;
          }
       }
       return 0;
    }
    
    /* file.txt
    abc
    123
    123.456
    0x14
    9a
    */
    
    /* my output
     afloat = 123
     afloat = 123.456
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Or use strtod() and see if there is a '.' between the start and end pointer (and check errno as well).

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    A simple way to check the input straight from cin is to read into an int. If it succeeds, then the first characters of the input were numbers. Then check the next character in the stream to see if it is a newline (from the user hitting enter to send the input). If it isn't, then the input isn't valid because they entered a decimal point or a non-numeric character.
    Code:
    int val = 0;
    while (!(cin >> val) || cin.get() != '\n')
    {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
      // Invalid input!
    }
    The clear and ignore clean out the fail state and any and all bad characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  2. floating point binary program, need help
    By ph34r me in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 06:10 AM
  3. floating point question
    By Eric Cheong in forum C Programming
    Replies: 8
    Last Post: 09-10-2004, 10:48 PM
  4. 2 questions about floating point and %
    By ams80 in forum C Programming
    Replies: 2
    Last Post: 08-14-2002, 10:55 AM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM

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