Thread: a problem with this program, help me plzzzzzzzz

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    22

    Question a problem with this program, help me plzzzzzzzz

    this program's problems:
    1.when we enter y, doesn't get 2nd student's name.
    2.after 2nd student when we enter n, program prints mistake things.
    3.average is 0 forever.
    sorry for my bad english.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    struct student {
    char name[20];
    char family[30];
    char namedan[30];
    float mark;
    } *p;
    
    void print (student *, int);
    int marks (student *, int);
    
    int main(){
    int i;
    char ch;
    
    
    
    for(i=1;;i++){
    p=(student *) malloc (sizeof(student));
    p++;
    printf("\nenter %domin student's name:\n",i);
    gets((*p).name);
    printf("enter %domin student's family:\n",i);
    gets((*p).family);
    printf("enter %domin student's namedan:\n",i);
    gets((*p).namedan);
    printf("enter %domin student's average:\n",i);
    scanf("%f",&(*p).mark);
    
    printf("do you want to enter another student's information?(y or n): ");
    ch=getche();
    
    if (ch!='y') break;
    free(p);
    }
    print(p,i);
    printf ("average of student's marks is: %f",marks(p,i));
    
    getch();
    return 0;
    }
    
    void print (student *p, int s){
    int i;
    for (i=1;i<=s;i++){
    printf("\n%s\t%s\t%s\t%f\n",(*p).name,(*p).family,(*p).namedan,(*p).mark);
    p++;
    }
    }
    
    int marks (student *p, int s){
    int i;
    float avg,sum=0;
    
    for (i=1;i<=s;i++){
    sum=sum+(*p).mark;
    p++;
    }
    avg=sum/s;
    return avg;
    
    }
    Last edited by abbaskhan; 06-19-2011 at 03:09 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Indent.
    Code:
    for(i=1;;i++){
        p=(student *) malloc (sizeof(student));
        p++; /* <-- (1) what is this supposed to do? */
        printf("\nenter %domin student's name:\n",i);
        gets((*p).name); /* <-- (2) never used gets, read the FAQ */
        ...
        if (ch!='y') break;
        free(p); /* <-- (3) you just threw away your record */
    }
    print(p,i); /* <-- (4) which means this is invalid too */
    1 - You are moving past the space you have just allocated.
    2 - Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows
    3 - Actually you didn't, because you messed that up in step 1. But you would have.
    4 - You can't go printing stuff you've already freed, even if you do get point 1 right.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    22
    i truth this code to this shape, but now cant print the students information, why? how can i truth it?

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    struct student {
    char name[20];
    char family[30];
    char namedan[30];
    float mark;
    } *p;
    
    void print (student *, int);
    int marks (student *, int);
    
    int main(){
    int i;
    char ch;
    
    
    p=(student *) malloc (sizeof(student));
    for(i=1;;i++){
    printf("\nenter %domin student's name:\n",i);
    scanf("%s",(*p).name);
    printf("enter %domin student's family:\n",i);
    scanf("%s",(*p).family);
    printf("enter %domin student's namedan:\n",i);
    scanf("%s",(*p).namedan);
    printf("enter %domin student's average:\n",i);
    scanf("%f",&(*p).mark);
    p=(student *) malloc (sizeof(student));
    p++;
    printf("do you want to enter another student's information?(y or n): ");
    ch=getche();
    
    if (ch!='y') break;
    
    }
    print(p,i);
    printf ("average of student's marks is: %f",marks(p,i));
    
    getch();
    return 0;
    }
    
    void print (student *p, int s){
    int i;
    for (i=1;i<=s;i++){
    printf("\n%s\t%s\t%s\t%f\n",(*p).name,(*p).family,(*p).namedan,(*p).mark);
    p++;
    }
    }
    
    int marks (student *p, int s){
    int i;
    float avg,sum=0;
    
    for (i=1;i<=s;i++){
    sum=sum+(*p).mark;
    p++;
    }
    avg=sum/s;
    return avg;
    
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You seem to be confused a bit by pointers. Let's say we want to make one integer, using a pointer:
    Code:
    int *p; /* declare a pointer to an int */
    p = malloc( sizeof *p ); /* try to allocate memory for it */
    if( p != NULL ) /* see if it worked */
        *p = 5; /* put some value in that space */
    Now what if we want 2 integers instead? Well, we have to make sure we don't lose our old integer. If we just do this:
    Code:
    p++;
    We have moved from where we were, and nothing is keeping track of where we were anymore, so now we can't go find that 5. Also, we don't know if we can safely access the spot in memory that falls just past our five.

    Well, we could just allocate two in the first place:
    Code:
    int *p;
    
    p = malloc( 2 * sizeof *p ); /* allocate 2 integers */
    if( p != NULL )
    {
        *p = 5; /* put something in our first one */
        p++; /* move to the next piece */
        *p = 6; /* put something in our second one */
    }
    Well that works, but it has one problem: How do we get back to five? Well we can: p--; but that's a pain to keep jumping back and forth, and we might wander off either end and cause problems. We can do this:
    Code:
    *(p + 0) = 5;
    *(p + 1) = 6;
    That way p isn't changing back and forth, and we don't lose it. We can also write that as this:
    Code:
    p[0] = 5;
    p[1] = 6;
    Ok, that's fine, but we have one problem, looking back at our first example: We already allocated p, but now we want to resize it. We can use realloc!
    Code:
    int *p, *q; /* declare a couple of pointers to ints */
    p = malloc( sizeof *p ); /* try to allocate memory for it */
    if( p != NULL )  /* see if it worked */
    {
        *p = 5; /* put some value in that space */
    
        q = realloc( p, 2 * sizeof *p ); /* try to resize it */
        if( q != NULL ) /* if it worked... */
        {
            p = q; /* update p */
            p[ 1 ] = 6; /* add 6 to our new spot */
        }
    }
    That should get you going. Be sure to free what you are done with, and take care to read up on realloc.

    Note, if it works, you don't free the old memory, because it has been reallocated (ie: don't free p before assigning q to it, just free one time when you are all done):
    Code:
    p = malloc( ... );
    if( p )
        q = realloc( p, ... );
    if( q )
    {
        free( p ); /* no! */
        p = q; /* yes */
    }
    free( p ); /* yes */

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    22
    thank you very much, but, may you truth my code? i can't understand what you say, i'm confused completely, plz

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct student {
        char name[20];
        char family[30];
        char namedan[30];
        float mark;
    };
    
    void print (struct student *, int);
    int marks (struct student *, int);
    
    int main( void ){
        int i;
        char ch;
        struct student *p;
       
        p = malloc (sizeof *p);
    
        for(i=1;;i++){
            printf("\nenter %domin student's name:\n",i);
            scanf("%s",p[i-1].name); /* also: fgets would work */
            printf("enter %domin student's family:\n",i);
            scanf("%s",p[i-1].family);
            printf("enter %domin student's namedan:\n",i);
            scanf("%s",p[i-1].namedan);
            printf("enter %domin student's average:\n",i);
            scanf("%f",&p[i-1].mark);
    
            printf("do you want to enter another student's information?(y or n): ");
            ch=getchar();
            if (ch!='y') break;
            else
            {
                struct student *q = realloc( p, (i+1) * sizeof *p );
                if( q != NULL )
                    p = q;
                else
                {
                    printf( "out of memory\n" );
                    return EXIT_FAILURE;
                }
            }
        }
        print(p,i);
        printf ("average of student's marks is: %f",marks(p,i));
    
        getchar();
        return 0;
    }
    
    void print (struct student *p, int s){
        int i;
        for (i=1;i<=s;i++){
            printf("\n%s\t%s\t%s\t%f\n",(*p).name,(*p).family,(*p).namedan,(*p).mark);
            p++;
        }
    }
    
    int marks (struct student *p, int s){
        int i;
        float avg,sum=0;
    
        for (i=1;i<=s;i++){
            sum=sum+(*p).mark;
            p++;
        }
        avg=sum/s;
        return avg;
    }
    That should get you going in the right direction.

    You seem to be compiling as C++:
    Code:
    void print (student *, int);
    int marks (student *, int);
    
    int main(){
    int i;
    char ch;
    
    
    p=(student *) malloc (sizeof(student));
    Instead of C:
    Code:
    void print (struct student *, int);
    int marks (struct student *, int);
    
    int main( void ){
        int i;
        char ch;
        struct student *p;
       
        p = malloc (sizeof *p);

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    22
    thank youuuuuuuuuuuuuuuuuuuuuu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program problem
    By matt345 in forum C Programming
    Replies: 4
    Last Post: 01-14-2011, 03:06 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  4. can u help me with pointers?plzzzzzzzz
    By itcs in forum C Programming
    Replies: 7
    Last Post: 07-11-2003, 01:29 AM
  5. problem with my program
    By ArseMan in forum C++ Programming
    Replies: 3
    Last Post: 09-09-2001, 06:15 PM