Thread: Conversion of string into a number (integer or float)

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    104

    Conversion of string into a number (integer or float)

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main (void) {
    float getint (void);
    float a;
    printf ("Data:- ");
    a=getint();
    printf ("\nNumber is %f",a);
    }
    
    float getint(void) {
    	char ch;
    	float n=0, dec=0, total;
    	int xcount=0, i;
    	ch=getchar();
    	while (!isdigit(ch) && ch!='.') ch=getchar();
    	while (isdigit(ch)) {
    		n=(n*10)+(ch-'0');
    		ch=getchar();
    		}
    
    	if (ch=='.') {
    		ch=getchar();
    		while (isdigit(ch)) {
    			xcount++;
    			dec=(dec*10)+(ch-'0');
    			ch=getchar();}
    
    			for (i=1; i<=xcount; i++) dec=dec*0.1;
    			}
    total=n+dec;
    return total;
    }
    but I think something is wrong with the floating point precision calculation...

    Data:- 52.36

    Number is 52.360001


    I don't see anything wrong though, cause the char 52 gives 52 and the char .2 gives .2

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >float getint(void)
    That's a poor choice of name for this function. getint suggests that it gets an integer, not a float.

    >void main (void)
    No, no, no. void main is always wrong, even if your teacher says otherwise and your compiler allows it. Use this instead:
    Code:
    int main ( void )
    {
      return 0;
    }
    As for getint itself, it works just peachy. The problem is how floating-point representation has problems in binary. You'll run into precision difficulties eventually, better sooner than later so that you know they exist. You can use printf's formatting to remove parts you don't want, for example, to just print the whole number you do this:
    Code:
    printf ( "The value is: %.0f\n", value );
    My best code is written with the delete key.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Ack - Prelude beat me to it!

    Oh well, probably better that way anyhow. I do have some things to add though. I see you used

    Code:
    #include <conio.h>
    I did not see any functions in your program that needed that header (which is a header not included in the list of ANSI compliant headers) You do have a call to isdigit() though, and that would require this:

    Code:
    #include <ctype.h>
    Also, you put your function prototype for getint() inside of main. That is not so cool.

    ~/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM