Maxx
4/17/2011 6:20:00 PM
On Apr 16, 3:54 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> Maxx <grungeddd.m...@gmail.com> writes:
> > On Apr 15, 3:01 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
>
> <snip yards of text>
>
> > Oh sorry i forget to edit my post, it should be::::
>
> There was no need to quote all of Jens's message (including the sig
> block) in order to post a new version.
>
> > #include <stdio.h>
> > #include <stdlib.h>
> > #define MAX_LEN 100
>
> > typedef struct _student
> > {
> > char name[25],dept[2],address[50],d_o_b[10];
> > int age,roll;
>
> Spaces please!
>
> > }STUDENT;
>
> I'd reserve ALL CAPS for macros and raving on the Internet.
>
> > STUDENT *stdnt_arr[MAX_LEN];
>
> > int sn=0;
>
> This is a VERY BAD IDEA. Global variables should be used only in very
> specific situations and when they are the right solution, a two letter
> rather cryptic name is not really suitable. You need, I think, to pass
> this data to the two functions rather than have is as a global.
>
> > void get_input();
> > void rec_pr();
>
> void get_input(void);
> void rec_pr(void);
>
> This will enable the compiler to tell you if misuse the function in
> certain ways. It's good to get help from the compiler.
>
> > int main()
>
> Ditto, though here it matters less since few program call main.
>
>
>
> > {
> > char c,chrin[50],nr;
>
> > printf("\t\tWelcome to student database\nChoose The following
> > option");
> > printf("I- Input Record\tP- Print Record\n");
>
> > c=getchar();
> > switch(c)
> > {
> > case 'I':
> > get_input();
> > break;
>
> > case 'P':
> > rec_pr();
> > break;
>
> > default:
> > fprintf(stderr,"No option selected");
> > exit(2);
> > }
> > printf("\nNow Exiting::");
> > return 0;
>
> So this program either gets input and exits or tries to print
> non-existent data and exits. What's the point of that?
>
> I don't think you'll be able to fix the program until you design it's
> top level to do something useful.
>
> > }
>
> > void get_input()
> > {
>
> > stdnt_arr[sn]=(STUDENT *)malloc(sizeof(STUDENT));
>
> > printf("Enter Your Name::");
> > scanf("%s",stdnt_arr[sn]->name);
>
> scanf("%24s",stdnt_arr[sn]->name);
>
> Is safer. However, you will run into all sorts of trouble is the name
> is longer than you expect. You need to have a plan to deal with that.
> A lot of programming courses start with program that do input and output
> but in my opinion that's a flaw. Output is fine but input is very much
> harder and can only really be done well once the student knows a lot
> more.
>
>
>
> > printf("Enter Your Dept::");
> > scanf("%s",stdnt_arr[sn]->dept);
>
> > printf("Enter Your address::");
> > scanf("%s",stdnt_arr[sn]->address);
>
> > printf("Enter your age::");
> > scanf("%d",&stdnt_arr[sn]->age);
> > printf("%d\n",stdnt_arr[sn]->age);
>
> > printf("Enter your roll no::");
> > scanf("%d",&stdnt_arr[sn]->roll);
>
> > printf("Enter your date of birth::");
> > scanf("%s",stdnt_arr[sn]->d_o_b);
>
> > sn++;
>
> > }
>
> > void rec_pr()
> > {
>
> > printf("Name::%s",stdnt_arr[0]->name);
> > printf("Dept::%s",stdnt_arr[0]->dept);
> > printf("Address::%s",stdnt_arr[0]->address);
> > printf("Age::%d",stdnt_arr[0]->age);
> > printf("Roll_no::%d",stdnt_arr[0]->roll);
> > printf("Date of Birth::%s",stdnt_arr[0]->d_o_b);
>
> > }
>
> This is bound to fail since sn[0] can't be anything but NULL when this
> function is called.
>
> > My apology for that...
>
> > Actually i intended this program to be able to store 50 records, and
> > each time the program is run the program will input the record in a
> > new array position that is empty or print the records according to the
> > user's choice..
>
> Where will these records be stored between runs?
>
> > I used fgets() at first but for some reason the function didn't took
> > any input so i went back to using scanf() just to try out the
> > program..
>
> I think it is better to find out why fgets is not doing what you
> expect. If you have a deadline, just using something else can be a good
> idea, but it's generally better to try to find out what is happening
> with a program that's not working.
>
> > No this is a C program.. i'm using microsoft visual C++ compiler may
> > be thats the reason i'm not getting any warning or errors..
>
> I am sure your MS C compiler can be told to give you lots of warnings.
> It's a very good idea to get it to do so.
>
> --
> Ben.
I declared those variables global just to avoid them passing to the
function, not knowing the danger they might bring..Ah thats the flaw i
totally overlooked i didn't made a suitable storage for the program to
hold its data between runs..Have to rethink it again.
The problem i had with fgets()(which i realized very recently) was
that deadline problem that you mentioned, I completely forgot about
that so my program was giving a out of bounds exceptions or something
like that.
Actually i'm totally novice to MS visual studio compiler other than
compiling i don't know any workaround with its various options that
would throw informative warnings..Thus i totally miss the warnings..
Anyways thanks for the help