lines 16 and 18 are wrong, you are passing a pointer to a single character not the whole string. Here's how to code it scanf ("%s", name[i]);
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
If you use fgets() you will also have to remove the terminating '\n' that fgets() will add to the end of the string, assuming there was enough room in the buffer to hold it.
fgets(name[i],sizeof(name[i]),stdin);
if( name[i][strlen(name[i])] == '\n')
name[i][strlen(name[i])] = '\0';
or you can do it like this
char* ptr;
fgets(name[i],sizeof(name[i]),stdin);
if( (ptr = strchr(name[i],'\n\)) != NULL)
*ptr = '\0';
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Use fflush after your scanf or gets lines.
printf ("Enter the number of student >> ");
scanf ("%d", &n);
fflush(stdin) ;
..........
fgets (name[i], sizeof(name[i]), stdin);
fflush(stdin) ;
..........
fgets (name[i], sizeof(add[i]), stdin);
fflush(stdin) ;
I gave you positive rep for one of your previous posts -- I want it back! fflush() is only intended for output streams, not input streams, also a few compilers have implemented it for input streams. Never suggest using fflush(stdin) because its non-standard and not implemented by most compilers.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Of course fflush(stdout) didn't work because stdout is an output only stream. flush(stdin) worked for you only because your compiler allows it, not because its standard C language.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Well, it works with Microsoft's latest and greatest compiler 16.00.30319.01 for 80x86
And it works with Gnu C compiler 4.5.2, with Gnu C 3.4.5
Borland's 5.5.1 C Compiler.
Microsoft's Compiler 12.00.8804
Borland's old Turbo C 2.01
Borland's C++ 5.82
I might have to dig out one of my four working Apple IIe's to find a compiler it doesn't work on.
Showing that multiple compilers do not follow the standard is not proof that it works for all compilers. It simply proves that the compilers don't follow the standard. And if you continue to rely on undefined behavior to work a certain way, I'd love to see what happens when you write some critical software for your job on a compiler that doesn't implement undefined behavior to your needs. It could be weeks before you remember this thread...
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944