what is wrong in my code?

This is a discussion on what is wrong in my code? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> //x is a global variable int x; //QUESTION A: /** The parseInt() function returns a 1 (one) ...

  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
    22,113
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    504
    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 08: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, 08: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, 01:51 AM

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