Thread: what is wrong in my code?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    32

    what is wrong in my code?

    Code:
    #include <stdio.h>
    
    //x is a global variable
    int x;   //QUESTION A:
    
    /**
    The parseInt() function returns a 1 (one) if the C string parameter
    num_str is formatted as a valid integer, otherwise the function
    returns a 0 (zero).
    */
    int parseInt(char num_str[])
    {
        int i;   
        for (; num_str[i] != '\0'; i++)
            if( num_str[i] == '+' || num_str[i] == '-' )
    	i++;
        for (num_str [i] == '\0')
                return 0;
        
        return 1;
    }
    
        int i;   //QUESTION A:
        for (i = 0; num_str[i] != '\0'; i++)
            if( num_str[i] < '0' || num_str[i] > '9' )
                return 0;
        
        return 1;
    }
    
    int main() 
    {
        char str[100];
        printf("Please enter a list of integers or EOF"
               " to finish (example: 897 8 -3 EOF).\n" );
        
        while ( 1 )
        {
            scanf( "%s", str );
            if ( feof(stdin) )
                break;
            
            printf( "%s is ", str );
            if ( !parseInt(str) )
                printf( "NOT " );
            printf( "is a valid integer.\n" );
        }
    
        return

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sjcc
    what is wrong in my code?
    It won't compile.

    Rather, you should tell us what is wrong with your code, and then ask specific help on how to fix it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Did you try compiling it?
    If so, what errors do you get?
    Sounds like "Remove the errors in the following code" kind of homework.
    Last edited by stevesmithx; 05-01-2009 at 07:52 AM.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    ya, it seems as if this is a code found on the net and was asked to tell the errors if any.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM