Click here to Skip to main content
15,174,232 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to print result of a student .
i m using structure for it.. all the variables are defined in structure while i adding marks of 5 subject then it gives me error like invalid pointer addtion, rather i'd usd more than one structure varibale.

my code is

C#
struct result
{
 char enroll[100];


 int  m1[20],m2[20],m3[20],m4[20],m5[20];

 int t[100];

 int per[100];

};
void result()
{
    struct result r,r1,r2,r3,r4,r5;
    struct record s;

    FILE *fp;

    clrscr();

    fp=fopen("stud.txt","wb");

    printf("Enter Enrollment No : ");

    gets(r.enroll);

    if(strcmp(r.enroll,s.enroll_no)==0)
    {
       printf("Enter Marks for 5 subject:");

       scanf("%d %d %d %d %d",r1.m1,r2.m2,r3.m3,r4.m4,r5.m5);

       r.t=r1.m1+r1.m2+r1.m3+r1.m4+r1.m5;

       r1.per=r.t/5;
    }

    else
    {
    printf("Record Does Not Exist :");

    printf("Press Any Key to Exit ");

    exit(0);
   }

printf("per=%d",r1.per);
printf("total=%d",r.t);
getch();



}
Posted
Updated 2-Jan-14 0:47am
v4
Comments
fre_ber 1-Jan-14 12:57pm
   
How is your struct defined? You need to give scanf pointers to the char buffers.

Also, you can't simply add strings in C. Is it string concatenation wou want or adding integers?
Richard MacCutchan 2-Jan-14 6:13am
   
I think you need to go back to your Student notes or C-language reference and study the language a bit more. There is no point in all these structures and arrays in the above code; in fact in its current state it won't compile.

you are adding strings that wont work. You better use strcat for combine the strings.

but most strange is:

scanf("%s %s %s %s %s",r1.m1,r2.m2,r3.m3,r4.m4,r5.m5);


For eacht string a own member of a different struct?


Why not:

scanf("%s %s %s %s %s",r1.m1,r1.m2,r.m3,r1.m4,r1.m5);


and you better use more describale names than m1. Like name or description...
   
Comments
Member 10011989 2-Jan-14 5:43am
   
if i used single member of struct the error is same.. after making varibale integer then also i m unable to solve the problem
KarstenK 6-Jan-14 2:24am
   
CPallini has a fine solution. I didnt get that you wanted to sum up the input :-O
You should't use strings for storing integers.
And you should always check the return value of scanf. e.g.
C
#include <stdio.h>
#define N 5

struct result
{
  int enroll;
  int m[N];
  int t;

  double per;
};

int main()
{

  struct result r;

  printf("Enter Marks for 5 subjects: ");

  if ( scanf("%d %d %d %d %d",&r.m[0],&r.m[1],&r.m[2],&r.m[3],&r.m[4]) == N)
  {
    int n;
    r.t = 0;
    for ( n=0; n<N; ++n)
      r.t += r.m[n];
    r.per = (double)r.t / N;
    printf("r.t = %d, r.per = %g\n", r.t, r.per);

  }
  else
  {// handle user input error
  }
  return 0;
}
   
Comments
Member 10011989 2-Jan-14 6:33am
   
thank alot... its wroking nw.. thanks to all of u guys
CPallini 2-Jan-14 6:36am
   
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900