Thread: help with struct, c-programming

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    3

    help with struct, c-programming

    I use Microsoft visual studios 2012 and i don't know what im doing wrong, can someone correct me?

    i get the debug Assertion failed

    line 55

    Expression: (str!=NULL)

    I pointed out where line 55 is to make it easier.

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #define size 1
    
    
    struct students
    {
        char last[15], first [15];
        int t1,t2,t3;
        float avg;
        char grade;
    };
    void load (struct students s[], int n)
    {
    int i;
    for (i=0; i<n; i++)
    { 
    printf("Enter last name");
    gets(s[i].last); 
    printf("Enter first name");
    gets(s[i].first);
    printf("Enter three scores");
    scanf("%d%d%d",&s[i].t1,&s[i].t2, &s[i].t3);
    s[i].avg=(s[i].t1+s[i].t2+s[i].t3)/(float)3;
    if(s[i].avg>70)
        s[i].grade ='P';
        else
    s[i].grade ='F';
    fflush(stdin);
    }
    }
    void print(struct students s[],int n)
    {
    int i;
    for (i=0; i<n; i++)
    {
    printf("%s%s\n",s[i].first, s[i].last);
    printf("test scores: %d %d %d \n",s[i].t1, s[i].t2, s[i].t3);
    printf("average grade: %f %%\n\n",s[i].avg, s[i].grade);
    }
    }
    void sort(struct students s[], int n)
    {
    int i,j;
    students t;
    for(i=0;i<n-1;i++)
    for(j=0;j<n-1;j++)
    if (s[i].avg<s[j+1].avg)
    {
    t=s[i];
    s[i]=s[j+1];
    s[j+1]=t;
    }
    }
    void pass(struct students s[], int n)               //Source.cpp<-- line 55
    {
    int count=0, i;
    for(i=0; i<n; i++)
        if (s[i].grade = 'P')
        count ++;
        printf("number of passes %d\n\n",count);
    }
    void savetext(struct students s[], int n)
    {
    int i;
    FILE *f;                //file pointer
    f=fopen("C:\\ student.txt","w");        //("C:\\student.txt")<- C will greate the file if dont exist but C will not create the folder
    for (i=0; i<n; i++)
    {
        fprintf(f,"%s\n",s[i].last);
        fprintf(f,"%s\n",s[i].first);
        fprintf(f,"%d%d%d%f%c\n",s[i].t1, s[i].t2,s[i].t3,s[i].avg,s[i].grade);
    }
    fclose(f);
    }
    void gettext(struct students s[], int n) //<---- struct (name)
    {
        int i;
        FILE *f;  
        f= fopen("c:\\students.txt","r"); //<---- \\ name
        for(i=0; i<n; i++)
        {
            fgets(s[i].last,sizeof(s[i].last),f);
            fgets(s[i].first,sizeof(s[i].first),f);  // \n acts like an fflush
            fscanf(f,"%d%d%d%f%c\n", &s[i].t1, &s[i].t2,&s[i].t3, &s[i].avg,&s[i].grade);
        }
    fclose(f);
    }
    void savebin(struct students s[],int n)
    {
        FILE *f;
        f=fopen("c:\\students.bin","wb");
    
    
        fwrite(&s,sizeof(s[0]),n,f);  //&s is a reference to the array to save sizeof(s[0]) size of each array element
                //n is how many array elements are there
                //f is file pointer
    fclose(f);
    }
    
    
    void getbin(struct students s[],int n)
    {
    FILE *f;
    f=fopen("c:\\students.bin","rb");
    fread(&s,sizeof(s[0]),n,f);
    fclose(f);
    }
    
    
    void main()
    {
    students s[size];
    load(s,size);
    sort(s,size);
    print(s,size);
    pass(s,size);
    savetext(s,size);
    gettext(s,size);
    print(s,size);
    savebin(s,size);
    getbin(s,size);
    print(s,size);
    system("pause");
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Technical details:
    • fflush(stdin); is undefined behavior - it could do something, it could do nothing, it could do what you want
    • void main() is non-standard
    • gets() is a dangerous function


    Helpful hints:
    • learn to indent
    • use function prototypes
    • help us help you: post warnings and errors with code
    • code tags use line numbers and syntax highlighting already


    In general, you should probably read the FAQ to understand the problems associated with the details I mentioned.

    In terms of code, around, but annoyingly not on line 55:

    Code:
    void pass(struct students s[], int n) 
    {
    	int count=0, i;
    	for(i=0; i<n; i++)
    		if (s[i].grade = 'P')
    			count ++;
    	printf("number of passes %d\n\n",count);
    }
    The assignment in the if will make every grade 'P', and I don't think that was intended.

    Code:
    void getbin(struct students s[],int n)
    {
    	FILE *f;
    	f=fopen("c:\\students.bin","rb");
    	fread(&s,sizeof(s[0]),n,f);
    	fclose(f);
    
    }
    Do not assume that everything will go well. You need to check if a valid file handle was returned. You should also check fread's return value. It is the only way to know whether fread() got a whole student object from the file or not.

    That holds true for any place you do input.

    I might be nagging you about this but...
    Code:
            fgets(s[i].first,sizeof(s[i].first),f);  // \n acts like an fflush
    Why do you know this, but not that fflush(stdin) is undefined?

    Edit: Since you are using fread wrong, it is probably true that fwrite doesn't work either.

    On a very quick glance, these are the problems.
    Last edited by whiteflags; 12-14-2013 at 03:00 AM.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    In addition to the comments by whiteflag,

    Code:
    f=fopen("C:\\ student.txt","w");
    looks a bit weird

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    thank you for pointing out the errors. this was suppose to be an example from my professor and i had a hard time running it cuz he hand writes his codes and he has a messy handwriting.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by jcun3 View Post
    thank you for pointing out the errors. this was suppose to be an example from my professor and i had a hard time running it cuz he hand writes his codes and he has a messy handwriting.
    Just a quick hint: When in the context of programming, the plural of code is code. "codes" is a pet hate of mine.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your professor might be interested in any of the numerous technologies that will project a computer screen. If he used one, he wouldn't have to hand write the code anymore; not to mention, it would probably be better since he would be encouraged to write it and compile it from an editor.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > i get the debug Assertion failed
    > line 55
    > Expression: (str!=NULL)
    Examine the error in detail, does it also give you a source code filename as well?

    What this error message usually means is you passed a NULL pointer to a file function.

    What you should look for is an fopen() call returning NULL.

    I notice from your code listing that you use several different filenames, which actually ought to be the same name.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2013
    Posts
    3
    the file specific is f:\dd\vctools\crt_bld\self_x86\crt\src\fprintf.c

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, this is why I told you to check for a valid file handle.
    Don't assume everything will go well.
    If you do something like
    Code:
    FILE *f = fopen("C:\\sstudents.txt", "r");
    The example file sstudents.txt probably doesn't exist, (it's actually a spelling problem) so f becomes NULL.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  2. need help with opaque programming with struct and addresses.
    By DarkAngel4ever in forum C Programming
    Replies: 11
    Last Post: 03-30-2011, 08:19 PM
  3. struct holding data inside a linked list struct
    By icestorm in forum C Programming
    Replies: 2
    Last Post: 10-06-2009, 12:49 PM
  4. com programming - interface cannot use my struct
    By y_cant_i_C in forum Windows Programming
    Replies: 1
    Last Post: 12-04-2006, 11:06 PM

Tags for this Thread