Thread: A challenge

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    A challenge

    Hi....

    I'm new here and i got this problem....

    My function is to generate a function that is the same with atoi, just that the string library cannot be used.

    eg, if the string is keyed in as "123.87", the value returned is 123.87. If "123.67.78" is keyed in, the value should be 123.67. The second decimal is ignored.

    Kindly help? thanks....
    Code:
    #include <stdio.h>
    
    main ()
    
    {
    
    char array[5]; 
    int i;
    double sum = 0;
    float d= 10;
    int count = 0;
    
    printf ("Enter a string to be converted to double:\n");
    
    for (i=0; i<5; i++)
    
    scanf("%c", &array[i]);
    
    if (*array>= 65 && *array <= 90 || *array >= 97 && *array<= 122) {
    
    printf ("-32768");   
    
    }
    else
    
    {
    
    while (*array != '\0' && count !=2)
       		{
    		if (count == 0)
           	 sum = sum*10 +(*array - 48);
          	 if (count == 1)
        	 	 {
              	sum = sum + ((*array -48)/d);
               	d = d*10;
                }
          	*array ++;
    		 if (*array == '.')
           	 { count ++;
           	 }
               }
    	
    	}
    
    
    
    printf ("The number is %f.\n", sum);
    printf ("The number of decimal is %d.\n", count);
    
    }
    Code tags added by Kermi3

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    /*stares at obfuscated sunset*/
    /*sniffle*/damnit, vVv, I love ya, man.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Jack -

    I am posting this, and the reason VvV posted what he did, because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.

    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. AI Challenge
    By unknown_111 in forum General AI Programming
    Replies: 0
    Last Post: 10-02-2007, 12:18 AM
  2. A Challenge in C++
    By Brad0407 in forum C++ Programming
    Replies: 38
    Last Post: 07-18-2007, 12:56 PM
  3. Programming Challenge (for my school)
    By Ezerhorden in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2006, 06:56 AM
  4. Challenge?
    By Mackology101 in forum C Programming
    Replies: 5
    Last Post: 11-23-2004, 02:30 PM
  5. Requesting a challenge
    By RealityFusion in forum C++ Programming
    Replies: 8
    Last Post: 08-18-2003, 08:24 PM