[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Function giving exception

Maxx

4/14/2011 3:12:00 PM

I'm writing this program that can store information about 50 students
and prints them. I was using pointer to structure construct in this
program, the program gets compiled well with 0 errors but the program
is not printing any output and i'm getting a unhandled exception.

Here is the program::::


#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;

}STUDENT;


STUDENT stdnt_arr[MAX_LEN];

int sn=0;

void get_input();
void rec_pr();


int 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;
}

void get_input()
{

stdnt_arr[sn]=(STUDENT *)malloc(sizeof(STUDENT));

printf("Enter Your Name::");
scanf("%s",stdnt_arr[sn]->name);


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);

}

The get_input() function is working well but the rec_pr() is giving me
a unhandled exception when running... Please help me through this
12 Answers

Fred

4/14/2011 3:29:00 PM

0

On Apr 14, 8:12 am, Maxx <grungeddd.m...@gmail.com> wrote:
> I'm writing this program that can store information about 50 students
> and prints them. I was using pointer to structure construct in this
> program, the program gets compiled well with 0 errors but the program
> is not printing any output and i'm getting a unhandled exception.
>
> Here is the program::::
>
> #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;
>
> }STUDENT;
>
> STUDENT stdnt_arr[MAX_LEN];
>
> int sn=0;
>
> void get_input();
> void rec_pr();
>
> int 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;
>
> }
>
> void get_input()
> {
>
>         stdnt_arr[sn]=(STUDENT *)malloc(sizeof(STUDENT));

This will not compile. stdnt_arr[nr] is of type 'STUDENT', not
'STUDENT *'

>
>         printf("Enter Your Name::");
>         scanf("%s",stdnt_arr[sn]->name);

Compile error. Should be stdnt_arr[sn].name
and same throughout the program
>
>         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);
>
> }
>
> The get_input() function is working well but the rec_pr() is giving me
> a unhandled exception when running... Please help me through this

When fixed to compile, your program either accepts data for one
STUDENT
and does nothing else ( first input is "I" ), or tries to print data
for one STUDENT that has never had its fields filled in (first
input is "P").

--
Fred K

Maxx

4/15/2011 7:23:00 PM

0

On Apr 14, 8:28 am, Fred <fred.l.kleinschm...@boeing.com> wrote:
> On Apr 14, 8:12 am, Maxx <grungeddd.m...@gmail.com> wrote:
>
>
>
> > I'm writing this program that can store information about 50 students
> > and prints them. I was using pointer to structure construct in this
> > program, the program gets compiled well with 0 errors but the program
> > is not printing any output and i'm getting a unhandled exception.
>
> > Here is the program::::
>
> > #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;
>
> > }STUDENT;
>
> > STUDENT stdnt_arr[MAX_LEN];
>
> > int sn=0;
>
> > void get_input();
> > void rec_pr();
>
> > int 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;
>
> > }
>
> > void get_input()
> > {
>
> >         stdnt_arr[sn]=(STUDENT *)malloc(sizeof(STUDENT));
>
> This will not compile. stdnt_arr[nr] is of type 'STUDENT', not
> 'STUDENT *'
>
>
>
> >         printf("Enter Your Name::");
> >         scanf("%s",stdnt_arr[sn]->name);
>
> Compile error. Should be stdnt_arr[sn].name
> and same throughout the program
>
>
>
>
>
> >         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);
>
> > }
>
> > The get_input() function is working well but the rec_pr() is giving me
> > a unhandled exception when running... Please help me through this
>
> When fixed to compile, your program either accepts data for one
> STUDENT
> and does nothing else ( first input is "I" ), or tries to print data
> for one STUDENT that has never had its fields filled in (first
> input is "P").
>
> --
> Fred K

Ok got it.. But i wanted stdnt_arr[] to contain pointers which each of
them will point to struct _student.

jt

4/15/2011 10:01:00 PM

0

Maxx <grungeddd.maxx@gmail.com> wrote:
> On Apr 14, 8:28 am, Fred <fred.l.kleinschm...@boeing.com> wrote:
> > On Apr 14, 8:12 am, Maxx <grungeddd.m...@gmail.com> wrote:
> >
> > > I'm writing this program that can store information about 50 students
> > > and prints them. I was using pointer to structure construct in this
> > > program, the program gets compiled well with 0 errors but the program
> > > is not printing any output and i'm getting a unhandled exception.

< Some of Fred's reply snipped and things reorderes a bit >

> Ok got it.. But i wanted stdnt_arr[] to contain pointers which each of
> them will point to struct _student.

> > > typedef struct _student
> > > {
> > >         char name[25],dept[2],address[50],d_o_b[10];
> > >         int age,roll;
> >
> > > }STUDENT;
> >
> > > STUDENT stdnt_arr[MAX_LEN];

If you want an array of pointers to 'STUDENT' structures you
have to tell the compiler. Here you ask for an array of
'MAX_LEN' structures of type 'STUDENT'.

You'd have to use

STUDENT *stdnt_arr[MAX_LINE];

to get an array of pointers.

> > > void get_input();
> > > void rec_pr();

It's better to tell the compiler that tose functions don't take
arguments explicitely by using 'void' in the parantesis - the
way you wrote it it tells the compiler that they will take an
undisclosed number of arguments. That will just keep the com-
piler from warning you if you call those functions with argu-
ments they don't accept. (Of course, if this is meant to be
C++ things are diffferent.)

> > > int main()
> > > {
> > >         char c,chrin[50],nr;

What's the 'chrin' array and 'nr' meant to be good for?

> > >         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);
> > >         }

Why do you use an array of students when this will at best allow
the user to input data for a single student? And, moreover, if
the user enters 'P' first there's no data about a student yet
that could be printed out and you're rather likely in for a
crash of your program because the rec_pr() function will try to
use data from unintialized memory...

> > > void get_input()
> > > {
> >
> > >         stdnt_arr[sn]=(STUDENT *)malloc(sizeof(STUDENT));
> >
> > This will not compile. stdnt_arr[nr] is of type 'STUDENT', not
> > 'STUDENT *'

After you have made 'stdn_arr' an array of pointers you can (and
should) drop the cast. It doesn't buy you anything but keeps the
compiler from warning you if you forgot to include <stdlib.h>.
(Again, if this is meant to be C++ things are different, but then
you should be using the 'new' operator anyway.)

> > printf("Enter Your Name::");
> > scanf("%s",stdnt_arr[sn]->name);
> >
> > printf("Enter Your Dept::");
> > scanf("%s",stdnt_arr[sn]->dept);

Even if 'stdnt_arr' has been made an array of pointers what
you'r doing here is extremely dangerous. All the char arrays
in the structure have a very limited amount of space (25 for
'name', just 2 for 'dept' etc. If the user inputs more than
one less than that scanf() will happily write past the end of
those arrays. You can avoid that by telling scanf() about the
maximum number of chars fitting into the arrays, e.g. using

printf("Enter Your Name::");
scanf("%24s",stdnt_arr[sn]->name);

printf("Enter Your Dept::");
scanf("%1s",stdnt_arr[sn]->dept);

(note that one extra char in the array is needed for the
terminating '\0' character). But then you may have to re-
move the characters that didn't fit into the array from
the input buffer or they will be read into the next char
array you "scanf". Moreover, scanf() will stop at the
first white space character, so if names consist of a
christian name and a surname, usually separated by a
space, things won't work as you expect it to...

> > > The get_input() function is working well

Not really with the code you posted;-) The bugs you have
in there may not become obvious before you you try to
retrive with rec_pr() what you put in there...

> > > but the rec_pr() is giving me
> > > a unhandled exception when running...

If you press 'P' first then that's no surprise - the pro-
gram hadn't had any iput yet, so you print out stuff from
uninitialized memory. That's a nice recipe for getting a
program to crash.

And if you get an "unhandled exception" then I would
guess that you compiled this prgram with a C++ and not
a C compiler. That opens up another can of worms since
C and C++ are similar but different languages. C has no
exceptions and thus a program compiled with a C compiler
won't abort with an "unhandled exception". So is this
meant to be a C++ program and you just went to the wrong
newsgroup? Otherwise you should try to figure out how to
get it compiled by a C compiler (or how to get your com-
piler suite to treat your program as a C program) after
fixing the worst bugs.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

Maxx

4/16/2011 6:35:00 AM

0

On Apr 15, 3:01 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
> Maxx <grungeddd.m...@gmail.com> wrote:
> > On Apr 14, 8:28 am, Fred <fred.l.kleinschm...@boeing.com> wrote:
> > > On Apr 14, 8:12 am, Maxx <grungeddd.m...@gmail.com> wrote:
>
> > > > I'm writing this program that can store information about 50 students
> > > > and prints them. I was using pointer to structure construct in this
> > > > program, the program gets compiled well with 0 errors but the program
> > > > is not printing any output and i'm getting a unhandled exception.
>
> < Some of Fred's reply snipped and things reorderes a bit >
>
> > Ok got it.. But i wanted stdnt_arr[] to contain pointers which each of
> > them will point to struct _student.
> > > > typedef struct _student
> > > > {
> > > >         char name[25],dept[2],address[50],d_o_b[10];
> > > >         int age,roll;
>
> > > > }STUDENT;
>
> > > > STUDENT stdnt_arr[MAX_LEN];
>
> If you want an array of pointers to 'STUDENT' structures you
> have to tell the compiler. Here you ask for an array of
> 'MAX_LEN' structures of type 'STUDENT'.
>
> You'd have to use
>
> STUDENT *stdnt_arr[MAX_LINE];
>
> to get an array of pointers.
>
> > > > void get_input();
> > > > void rec_pr();
>
> It's better to tell the compiler that tose functions don't take
> arguments explicitely by using 'void' in the parantesis - the
> way you wrote it it tells the compiler that they will take an
> undisclosed number of arguments. That will just keep the com-
> piler from warning you if you call those functions with argu-
> ments they don't accept. (Of course, if this is meant to be
> C++ things are diffferent.)
>
> > > > int main()
> > > > {
> > > >         char c,chrin[50],nr;
>
> What's the 'chrin' array and 'nr' meant to be good for?
>
>
>
> > > >         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);
> > > >         }
>
> Why do you use an array of students when this will at best allow
> the user to input data for a single student? And, moreover, if
> the user enters 'P' first there's no data about a student yet
> that could be printed out and you're rather likely in for a
> crash of your program because the rec_pr() function will try to
> use data from unintialized memory...
>
> > > > void get_input()
> > > > {
>
> > > >         stdnt_arr[sn]=(STUDENT *)malloc(sizeof(STUDENT));
>
> > > This will not compile. stdnt_arr[nr] is of type 'STUDENT', not
> > > 'STUDENT *'
>
> After you have made 'stdn_arr' an array of pointers you can (and
> should) drop the cast. It doesn't buy you anything but keeps the
> compiler from warning you if you forgot to include <stdlib.h>.
> (Again, if this is meant to be C++ things are different, but then
> you should be using the 'new' operator anyway.)
>
> > >        printf("Enter Your Name::");
> > >        scanf("%s",stdnt_arr[sn]->name);
>
> > >        printf("Enter Your Dept::");
> > >        scanf("%s",stdnt_arr[sn]->dept);
>
> Even if 'stdnt_arr' has been made an array of pointers what
> you'r doing here is extremely dangerous. All the char arrays
> in the structure have a very limited amount of space (25 for
> 'name', just 2 for 'dept' etc. If the user inputs more than
> one less than that scanf() will happily write past the end of
> those arrays. You can avoid that by telling scanf() about the
> maximum number of chars fitting into the arrays, e.g. using
>
>         printf("Enter Your Name::");
>         scanf("%24s",stdnt_arr[sn]->name);
>
>         printf("Enter Your Dept::");
>         scanf("%1s",stdnt_arr[sn]->dept);
>
> (note that one extra char in the array is needed for the
> terminating '\0' character). But then you may have to re-
> move the characters that didn't fit into the array from
> the input buffer or they will be read into the next char
> array you "scanf". Moreover, scanf() will stop at the
> first white space character, so if names consist of a
> christian name and a surname, usually separated by a
> space, things won't work as you expect it to...
>
> > > > The get_input() function is working well
>
> Not really with the code you posted;-) The bugs you have
> in there may not become obvious before you you try to
> retrive with rec_pr() what you put in there...
>
> > > > but the rec_pr() is giving me
> > > > a unhandled exception when running...
>
> If you press 'P' first then that's no surprise - the pro-
> gram hadn't had any iput yet, so you print out stuff from
> uninitialized memory. That's a nice recipe for getting a
> program to crash.
>
> And if you get an "unhandled exception" then I would
> guess that you compiled this prgram with a C++ and not
> a C compiler. That opens up another can of worms since
> C and C++ are similar but different languages. C has no
> exceptions and thus a program compiled with a C compiler
> won't abort with an "unhandled exception". So is this
> meant to be a C++ program and you just went to the wrong
> newsgroup? Otherwise you should try to figure out how to
> get it compiled by a C compiler (or how to get your com-
> piler suite to treat your program as a C program) after
> fixing the worst bugs.
>                             Regards, Jens
> --
>   \   Jens Thoms Toerring  ___      j...@toerring.de
>    \__________________________      http://t...


Oh sorry i forget to edit my post, it should be::::

#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;

}STUDENT;

STUDENT *stdnt_arr[MAX_LEN];

int sn=0;

void get_input();
void rec_pr();

int 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;

}

void get_input()
{

stdnt_arr[sn]=(STUDENT *)malloc(sizeof(STUDENT));

printf("Enter Your Name::");
scanf("%s",stdnt_arr[sn]->name);

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);

}

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..

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..

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..

Thanks..

Ben Bacarisse

4/16/2011 10:55:00 AM

0

Maxx <grungeddd.maxx@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.

jt

4/16/2011 10:17:00 PM

0

Maxx <grungeddd.maxx@gmail.com> wrote:
> Oh sorry i forget to edit my post, it should be::::

I've got not much to add to what Ben wrote, just a few remarks.

> printf("Enter Your Name::");
> scanf("%s",stdnt_arr[sn]->name);

> printf("Enter Your Dept::");
> scanf("%s",stdnt_arr[sn]->dept);

> printf("Enter Your address::");
> scanf("%s",stdnt_arr[sn]->address);

Even if you had problems with fgets() it's much better and safer
at getting text input. Your code with scanf) is just waiting
for crashes or difficult to understand problems - it's akin to
closing your eyes and ears when crossing a street with lots of
traffic. You might get lucky but don't complain if you get hit
by a truck...

> 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);

Using scanf() for input of ints (or doubles) is less proble-
matic. But you still should check if scanf() did read any-
thing by checking its return value - what about someone just
making a typo and enters 'q1 instead of '21'? scanf() will
balk at the 'q' (since it isn't possibly part of an int and
will read nothing at all. Checking the return value of scanf()
will tell you if there was such a problem.

> 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);

As Ben has pointed out that, if you get into this function,
you never have set any values in 'stdnt_arr', so this at best
will output garbage (or crash the program). But even that out
of the way I guess you should sprinkle your format strings
with a few new-line characters - or do you want everything
printed out on a single line?

> 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..

The way your program is written the user is given a choice
exactly once at the start of the program if (s)he wants to
enter data or print out data. If the user enters 'I' (s)he
will be asked for information about the student, which get
stored (in a very dangerous way) into a structure. Then the
program ends and all data just being read in are forgotten.
If the user enters 'P' at the first prompt garbage is printed
out or the program crashes.

That doesn't seem to fit your stated intents. One way to improve
on it would be to start with a loop in main() that asks the user
repeatedly for new instructions. Don't react to 'P' before data
have been entered for at least one student (or check the value
of 'sn' in 'rec_pt()' and print out something like "No students
in database yet" if 'sn' is 0. Don't react to 'I' anymore if
'sn' has reached 'MAX_LEN' since that's the maximum number of
records that can be stored. If you do that it probabl will get
a bit boring to get only information about the first student
entered from rec_pr(). Either print out all that are available
or ask for which student information is to be printed out (e.g.
ask for a recored number in rec_pr(), check that it's smaller
than 'sn' and, if that's the case. print the data from that
record...

The next step in the development of the program then would be to
store the information that has been entered e.g. in a file, so
that it can be read in again the next time the program is started.

> 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..

There are two issues here. First of all, also VSC++ can be made
to output more warnings (I can't remember how to do that but the
few times I was forced to use it this definitely was possible).
Raise the warning level rather high and try to understand what
each warning is about - many warnings have good reasons and
you will benefit a lot from understanding them and correcting
the parts of the code that result in warnings. Never dismiss
a warning as unreasonable unless you really understand what
it's about - while there are some that can be disregarded that
should be done only if one knows why.

The other point is the "unhandled exception" result of the pro-
gram. I have only very limited experience with Windows, but
that sounds very much like the output from a crashed C++ pro-
gram. Thus my suspicion that VSC++ is actually compiling your
program as a C++ and not a C program. It can do both as far as
I know, but you must make it clear what you want. I'm not sure
how VSC++ is handling things, but compiler suites like that
often default to C++ mode if the file extension is 'cpp', 'c++'
or 'C' (with an uppercase 'C'). Make sure your file with the
program has 'c' as its extension and perhaps check were to set
the compiler mode...
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

pete

4/17/2011 7:36:00 AM

0

Maxx wrote:

> int 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;
>
> }

int main(void)
{
int c;

puts("\t\tWelcome to student database");
for (;;) {
puts("\nChoose The following option:");
puts("I- Input Record\nP- Print Record\nE- Exit\n");
c = getchar();
switch(c) {
case 'I':
case 'i':
get_input();
break;
case 'P':
case 'p':
rec_pr();
break;
default:
fprintf(stderr,"No option selected");
case 'E':
case 'e':
exit(0);
}
getchar();
}
return 0;
}

--
pete

pete

4/17/2011 7:41:00 AM

0

Maxx wrote:

> void get_input()
> {
>
> stdnt_arr[sn]=(STUDENT *)malloc(sizeof(STUDENT));
>
> printf("Enter Your Name::");
> scanf("%s",stdnt_arr[sn]->name);
>
> 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);
>
> }



void get_input()
{

stdnt_arr[sn] = malloc(sizeof *stdnt_arr[sn]);

if (stdnt_arr[sn] != NULL) {
printf("Enter Your Name::");
scanf("%s",stdnt_arr[sn]->name);

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()
{
if (stdnt_arr[0] != NULL && stdnt_arr[0]->name != NULL) {
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);
}
}

--
pete

Maxx

4/17/2011 6:20:00 PM

0

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

Maxx

4/17/2011 6:29:00 PM

0

On Apr 16, 3:17 pm, j...@toerring.de (Jens Thoms Toerring) wrote:
> Maxx <grungeddd.m...@gmail.com> wrote:
> > Oh sorry  i forget to edit my post, it should be::::
>
> I've got not much to add to what Ben wrote, just a few remarks.
>
> >         printf("Enter Your Name::");
> >         scanf("%s",stdnt_arr[sn]->name);
> >         printf("Enter Your Dept::");
> >         scanf("%s",stdnt_arr[sn]->dept);
> >         printf("Enter Your address::");
> >         scanf("%s",stdnt_arr[sn]->address);
>
> Even if you had problems with fgets() it's much better and safer
> at getting text input. Your code with scanf) is just waiting
> for crashes or difficult to understand problems - it's akin to
> closing your eyes and ears when crossing a street with lots of
> traffic. You might get lucky but don't complain if you get hit
> by a truck...
>
> >         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);
>
> Using scanf() for input of ints (or doubles) is less proble-
> matic. But you still should check if scanf() did read any-
> thing by checking its return value - what about someone just
> making a typo and enters 'q1 instead of '21'? scanf() will
> balk at the 'q' (since it isn't possibly part of an int and
> will read nothing at all. Checking the return value of scanf()
> will tell you if there was such a problem.
>
> > 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);
>
> As Ben has pointed out that, if you get into this function,
> you never have set any values in 'stdnt_arr', so this at best
> will output garbage (or crash the program). But even that out
> of the way I guess you should sprinkle your format strings
> with a few new-line characters - or do you want everything
> printed out on a single line?
>
> > 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..
>
> The way your program is written the user is given a choice
> exactly once at the start of the program if (s)he wants to
> enter data or print out data. If the user enters 'I' (s)he
> will be asked for information about the student, which get
> stored (in a very dangerous way) into a structure. Then the
> program ends and all data just being read in are forgotten.
> If the user enters 'P' at the first prompt garbage is printed
> out or the program crashes.
>
> That doesn't seem to fit your stated intents. One way to improve
> on it would be to start with a loop in main() that asks the user
> repeatedly for new instructions. Don't react to 'P' before data
> have been entered for at least one student (or check the value
> of 'sn' in 'rec_pt()' and print out something like "No students
> in database yet" if 'sn' is 0. Don't react to 'I' anymore if
> 'sn' has reached 'MAX_LEN' since that's the maximum number of
> records that can be stored. If you do that it probabl will get
> a bit boring to get only information about the first student
> entered from rec_pr(). Either print out all that are available
> or ask for which student information is to be printed out (e.g.
> ask for a recored number in rec_pr(), check that it's smaller
> than 'sn' and, if that's the case. print the data from that
> record...
>
> The next step in the development of the program then would be to
> store the information that has been entered e.g. in a file, so
> that it can be read in again the next time the program is started.
>
> > 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..
>
> There are two issues here. First of all, also VSC++ can be made
> to output more warnings (I can't remember how to do that but the
> few times I was forced to use it this definitely was possible).
> Raise the warning level rather high and try to understand what
> each warning is about - many warnings have good reasons and
> you will benefit a lot from understanding them and correcting
> the parts of the code that result in warnings. Never dismiss
> a warning as unreasonable unless you really understand what
> it's about - while there are some that can be disregarded that
> should be done only if one knows why.
>
> The other point is the "unhandled exception" result of the pro-
> gram. I have only very limited experience with Windows, but
> that sounds very much like the output from a crashed C++ pro-
> gram. Thus my suspicion that VSC++ is actually compiling your
> program as a C++ and not a C program. It can do both as far as
> I know, but you must make it clear what you want. I'm not sure
> how VSC++ is handling things, but compiler suites like that
> often default to C++ mode if the file extension is 'cpp', 'c++'
> or 'C' (with an uppercase 'C'). Make sure your file with the
> program has 'c' as its extension and perhaps check were to set
> the compiler mode...
>                             Regards, Jens
> --
>   \   Jens Thoms Toerring  ___      j...@toerring.de
>    \__________________________      http://t...



Yeah totally was ignorant about the danger associated with using
scanf() like that.. Never had any problem like that kind so was
completely unaware

Also was doing a fatal mistake by not providing some sort of storage
mechanism so the program could store the input data at exit...Now
clearly understood why printf() was giving NULL output...
And thanks for providing the instructions on how to proceed, its spot
on i was looking for something like that.Thanks a lot...