delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/03/11:02:03

From: random AT visi DOT net (Michael Bosley)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Testing for float or int
Date: Sun, 03 May 1998 14:41:23 GMT
Organization: @Home Network
Lines: 39
Message-ID: <354c8104.39472875@news>
References: <35481B84 DOT 167E AT jcu DOT edu DOT au>
NNTP-Posting-Host: cx582964-a.nwptn1.va.home.com
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Thu, 30 Apr 1998 16:34:44 +1000, Oon Lin <Oon DOT Lin AT jcu DOT edu DOT au>
wrote:

>I had been surfing around Delorie's lib C online reference for a
>function that can test whether an input is in float or int.
>
>For example , let's say I have a float variable named foo.
>
>When I get the input using
>
>cin << foo ;
>
>I'd like to test is if the input was convertable to an int without loss
>of precision. For example , if the input given was 6.0000 , then
>converting it to an int would give 6. But if the input was 6.1234 , then
>conversion should not take place.
> 

Why couldn't you just do something like

void fori() {
  float input;
  int check;

  cin >> input;   // argh, i always forget which < or > to use here ;)
  int check = (int)input;
  if ((float) check < input)
    cout << "You passed me a floating point value" << endl;
  else if ((float) check == input)
    cout << "You passed me an integer value" << endl;

} // end void fori

Thanks,

random
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      Do Not Send Me Unsolicited Email!      
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019