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.
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.
depends what u want to do with the zipcode, and where you want to get this certain value from.
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
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 06:28 PM.
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.
heh thats kinda hard for someone that never did C before , lol
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
Learn the basics first and then come back to this.
yes indeed my bad. Use scanf( "%31s", szip[0] );Originally Posted by ^xor
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.
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.
A stack overflow has the same meaning as a buffer overflow?Originally Posted by ^xor
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.