zip codes

This is a discussion on zip codes within the C Programming forums, part of the General Programming Boards category; heys guys never did c before so i'm completely lost to the point where loosing my mind is an understatement. ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    Question zip codes

    heys guys never did c before so i'm completely lost to the point where loosing my mind is an understatement.

    how would u read zip codes from standard input?
    please help.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    depends what u want to do with the zipcode, and where you want to get this certain value from.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    2
    well i'll post what i'm being asked to do, i know we're not allowed to write the code for someone, but i just want someone to read it and give me an idea of where to start.


    Write a program the reads in three zip codes from standard input and prints to standard output a single integer and nothing else. The integer it prints out is the number of duplicates in the sequence of codes: 0, 1 or 2.
    0 is printed if each zip code is unique. 1 is printed if only two of the zip codes are the same. 2 is printed if all three are the same.

    Your main function must consist of just six statements:
    one declaration of any variables you might need
    three calls toscanf to read the zip codes
    one call toprintf to print the required integer output value
    and of course a return statement

    In addition, you must define a function that gets three integer arguments and returns 0, 1 or 2 depending on the number of duplicates (as described above). This function should be invoked (cal

  4. #4
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    6 statements?



    Code:
    int function( char [][32] );
    
    int main()
    {
      char szip[3][32] = { 0 };
    
      scanf( "%s", szip[0] );
      scanf( "%s", szip[1] );
      scanf( "%s", szip[2] );
    
      printf( "Output: %d", function( szip ) );
    
      return 0;
    
    }
    
    int function( char s[][32] )
    {
      You can use a loop with strcmp to compare strings
      return //answer here... 
    }
    Last edited by loko; 09-15-2005 at 07:28 PM.

  5. #5
    FOX
    Join Date
    May 2005
    Posts
    188
    Never use scanf like that. What do you think happens if someone decides to input 200 characters instead of 31 + nul? That's right, a stack overflow...

    Either use scanf("%31s", ...) or fgets.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    heh thats kinda hard for someone that never did C before , lol

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

  8. #8
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Quote Originally Posted by ^xor
    Never use scanf like that. What do you think happens if someone decides to input 200 characters instead of 31 + nul? That's right, a stack overflow...

    Either use scanf("%31s", ...) or fgets.
    yes indeed my bad. Use scanf( "%31s", szip[0] );
    But this would also put some junks in the input buffer if the input exceeds 32 characters. And it gets complicated for someone who havent program in C.

  9. #9
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    Just use scanf( "%s", strvar ); // which is what rquired above.

    Just remember as said above, its bad to use scanf like that. And tell your prof about that( most of them doesnt know ).

    And read on the links by thantos above.

  10. #10
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by ^xor
    Never use scanf like that. What do you think happens if someone decides to input 200 characters instead of 31 + nul? That's right, a stack overflow...

    Either use scanf("%31s", ...) or fgets.
    A stack overflow has the same meaning as a buffer overflow?

  11. #11
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    A stack overflow is a specific type of buffer overflow.

    A buffer overflow is a generic term for the overflow of any buffer, not necessarily one on the stack.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Downloading ZIP
    By abraham2119 in forum C Programming
    Replies: 8
    Last Post: 05-05-2009, 06:02 PM
  2. C libraries for unpacking zip and rar
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 02-23-2008, 12:16 PM
  3. action replay codes
    By c++.prog.newbie in forum Game Programming
    Replies: 2
    Last Post: 02-28-2004, 08:47 AM
  4. Zip Extractor
    By harryp in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2002, 11:44 AM

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