#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Cool My C program stopped working?


    Ok, so I am trying to create a student profile record system using c. I incorporated my program with a text file so everytime it is opened, memory is saved. The problem is that my code would compile well but when I run it, windows 7 says, the application has stopped working...blah blah blah...


    I hope you guys can help me fix this.



    So here is the code:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>

    struct student{
    char name[50];
    int age;
    float gwa;
    };

    int printmenu(){
    int x;
    printf("[1]Add a Student\n");
    printf("[2]Edit a Student\n");
    printf("[3]Print Student Profiles\n");
    printf("[4]Exit\n");
    scanf("%d",&x);
    return x;
    }


    main(){
    FILE *fp;
    fp=fopen("DIMACALI.txt","r");
    int x,tr,atemp,choice,i,j,z=0,val,editch;
    char temp[50];
    float gtemp;
    struct student s[20];
    if(fp==NULL){
    printf("Memory cannot be read.Text file does not exist or is corrupted.\n");
    exit(0);
    }

    else {z=0;
    while(!feof(fp)){
    fgets(s[z].name,50,fp);
    fscanf(fp,"\n%d\n",s[z].age);
    fscanf(fp,"%f",s[z].gwa);
    z++;
    }//end of while
    }//end of else
    fclose(fp);

    choice=printmenu();

    while(choice!=4){ if(choice==1){
    gets(s[z].name);
    printf("Type in the student name:");
    gets(s[z].name);
    printf("\nType in the age:");
    scanf("%d",&s[z].age);
    printf("\nType in the GWA:");
    scanf("%f",&s[z].gwa);
    if(z==0){tr=10;
    }


    else if(z==1){
    for(i=0;i<z;i++){
    val=strcmp(s[i].name,s[i+1].name);
    if(val>0){
    strcpy(temp,s[i].name);
    strcpy(s[i].name,s[i+1].name);
    strcpy(s[i+1].name,temp);

    atemp=s[i].age;
    s[i].age=s[i+1].age;
    s[i+1].age=atemp;

    gtemp=s[i].gwa;
    s[i].gwa=s[i+1].gwa;
    s[i+1].gwa=gtemp;
    }
    }
    }
    else if(z>1){
    for(i=0;i<=z-1;i++){
    for(j=1;j<=z;j++){
    val=strcmp(s[i].name,s[i+j].name);
    if(val>0){
    strcpy(temp,s[i].name);
    strcpy(s[i].name,s[i+j].name);
    strcpy(s[i+j].name,temp);

    atemp=s[i].age;
    s[i].age=s[i+j].age;
    s[i+j].age=atemp;

    gtemp=s[i].gwa;
    s[i].gwa=s[i+j].gwa;
    s[i+j].gwa=gtemp;
    }
    }
    }
    }
    z++;

    choice=printmenu();
    }
    if(choice==2){
    printf("Which among the students would you like to edit?\n");
    if(z==0){printf("Memory empty.Nothing to display\n");

    }
    else {for(i=0;i<z;i++){
    printf("[%d]%s\n",i,s[i].name);
    }
    scanf("%d",&editch);
    printf("Which information would you like to edit?\n");
    printf("[0]Student Name\n");
    printf("[1]Age\n");
    printf("[2]GWA\n");
    scanf("%d",&i);
    if(i==0){
    gets(s[editch].name);
    printf("New Student Name:");
    gets(s[editch].name);
    }
    if(i==1){
    printf("New Age:");
    scanf("%d",&s[editch].name);
    }
    if(i==2){
    printf("\nNew GWA:");
    scanf("%f",&s[editch].gwa);
    }
    }
    choice=printmenu();
    }//end of 2nd if

    if(choice==3){
    if(z==0){tr=10;
    }
    else if(z==1){
    for(i=0;i<z;i++){
    val=strcmp(s[i].name,s[i+1].name);
    if(val>0){
    strcpy(temp,s[i].name);
    strcpy(s[i].name,s[i+1].name);
    strcpy(s[i+1].name,temp);

    atemp=s[i].age;
    s[i].age=s[i+1].age;
    s[i+1].age=atemp;

    gtemp=s[i].gwa;
    s[i].gwa=s[i+1].gwa;
    s[i+1].gwa=gtemp;
    }
    }
    }
    else if(z>1){
    for(i=0;i<=z-1;i++){
    for(j=1;j<=z;j++){
    val=strcmp(s[i].name,s[i+j].name);
    if(val>0){
    strcpy(temp,s[i].name);
    strcpy(s[i].name,s[i+j].name);
    strcpy(s[i+j].name,temp);

    atemp=s[i].age;
    s[i].age=s[i+j].age;
    s[i+j].age=atemp;

    gtemp=s[i].gwa;
    s[i].gwa=s[i+j].gwa;
    s[i+j].gwa=gtemp;
    }
    }
    }
    }
    printf("STUDENT PROFILES\n");
    if(z==0){printf("Memory Empty.Nothing to display\n");

    }
    else {for(i=1;i<z+1;i++){
    printf("Student Name:%s\n",s[i].name);
    printf("Age:%d\n",s[i].age);
    printf("GWA:%f\n",s[i].gwa);
    }
    }
    choice=printmenu();
    }//end of 3rd if
    }//end of while loop
    if(choice==4){
    fp=fopen("DIMACALI.txt","w+");
    for(i=0;i<z;i++){
    fprintf(fp,"%s",s[i].name);
    fprintf(fp,"\n%d\n",s[i].age);
    fprintf(fp,"%f\n",s[i].gwa);
    }
    fclose(fp);
    printf("Thank you for using my program!");
    }//end of if 4



    }//END OF MAIN
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,183
    Rep Power
    2222
    Post your code using code tags, especially since your formatting is rather crappy anyway. If you're going to hide your open braces at the ends of lines, then proper indentation is imperative!

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct student{
           char name[50];
           int age;
           float gwa;
           };
    
    int printmenu(){
        int x;
        printf("[1]Add a Student\n");
        printf("[2]Edit a Student\n");
        printf("[3]Print Student Profiles\n");
        printf("[4]Exit\n");
        scanf("%d",&x);
        return x;
    }
    
    
    main(){
           FILE *fp;
           fp=fopen("DIMACALI.txt","r");
           int x,tr,atemp,choice,i,j,z=0,val,editch;
           char temp[50];
           float gtemp;
           struct student s[20];
           if(fp==NULL){
                        printf("Memory cannot be read.Text file does not exist or is corrupted.\n");
                        exit(0);
                        }
           
           else {z=0;
                     while(!feof(fp)){
                                  fgets(s[z].name,50,fp);
                                  fscanf(fp,"\n%d\n",s[z].age);
                                  fscanf(fp,"%f",s[z].gwa);
                                  z++;
                                  }//end of while
                                                    }//end of else
           fclose(fp); 
        
           choice=printmenu();
           
               while(choice!=4){ if(choice==1){
                                             gets(s[z].name);
                                             printf("Type in the student name:");
                                             gets(s[z].name);
                                             printf("\nType in the age:");
                                             scanf("%d",&s[z].age);
                                             printf("\nType in the GWA:");
                                             scanf("%f",&s[z].gwa);
                                             if(z==0){tr=10;
                                                          }
                                             
                                             
                                             else if(z==1){
                                                      for(i=0;i<z;i++){
                                                                       val=strcmp(s[i].name,s[i+1].name);
                                                                       if(val>0){
                                                                                 strcpy(temp,s[i].name);
                                                                                 strcpy(s[i].name,s[i+1].name);
                                                                                 strcpy(s[i+1].name,temp);
                                                                                 
                                                                                 atemp=s[i].age;
                                                                                 s[i].age=s[i+1].age;
                                                                                 s[i+1].age=atemp;
                                                                                 
                                                                                 gtemp=s[i].gwa;
                                                                                 s[i].gwa=s[i+1].gwa;
                                                                                 s[i+1].gwa=gtemp;
                                                                                 }
                                                                      }
                                                      }
                                             else if(z>1){
                                                     for(i=0;i<=z-1;i++){
                                                                        for(j=1;j<=z;j++){
                                                                                     val=strcmp(s[i].name,s[i+j].name);
                                                                                     if(val>0){
                                                                                               strcpy(temp,s[i].name);
                                                                                               strcpy(s[i].name,s[i+j].name);
                                                                                               strcpy(s[i+j].name,temp);
                                                                                               
                                                                                                atemp=s[i].age;
                                                                                                 s[i].age=s[i+j].age;
                                                                                                 s[i+j].age=atemp;
                                                                                                 
                                                                                                 gtemp=s[i].gwa;
                                                                                                 s[i].gwa=s[i+j].gwa;
                                                                                                 s[i+j].gwa=gtemp;
                                                                                               }
                                                                                        }
                                                                        }
                                                     }
                                             z++; 
                                                   
                                             choice=printmenu();
                                             }
                                  if(choice==2){
                                                printf("Which among the students would you like to edit?\n");
                                                if(z==0){printf("Memory empty.Nothing to display\n");
                                                        
                                                         }
                                                else {for(i=0;i<z;i++){
                                                                  printf("[%d]%s\n",i,s[i].name);
                                                                  }
                                                scanf("%d",&editch);                  
                                                printf("Which information would you like to edit?\n");
                                                printf("[0]Student Name\n");
                                                printf("[1]Age\n");
                                                printf("[2]GWA\n");
                                                scanf("%d",&i);
                                                               if(i==0){
                                                                        gets(s[editch].name);
                                                                        printf("New Student Name:");
                                                                        gets(s[editch].name);
                                                                        }
                                                               if(i==1){
                                                                        printf("New Age:");
                                                                        scanf("%d",&s[editch].name);
                                                                        }
                                                               if(i==2){
                                                                        printf("\nNew GWA:");
                                                                        scanf("%f",&s[editch].gwa);
                                                                        }
                                                                        }
                                               choice=printmenu();
                                                }//end of 2nd if
                                  
                                  if(choice==3){
                                                 if(z==0){tr=10;
                                                          }
                                                  else if(z==1){
                                                      for(i=0;i<z;i++){
                                                                       val=strcmp(s[i].name,s[i+1].name);
                                                                       if(val>0){
                                                                                 strcpy(temp,s[i].name);
                                                                                 strcpy(s[i].name,s[i+1].name);
                                                                                 strcpy(s[i+1].name,temp);
                                                                                 
                                                                                 atemp=s[i].age;
                                                                                 s[i].age=s[i+1].age;
                                                                                 s[i+1].age=atemp;
                                                                                 
                                                                                 gtemp=s[i].gwa;
                                                                                 s[i].gwa=s[i+1].gwa;
                                                                                 s[i+1].gwa=gtemp;
                                                                                 }
                                                                                 }
                                                      }
                                             else if(z>1){
                                                     for(i=0;i<=z-1;i++){
                                                                        for(j=1;j<=z;j++){
                                                                                     val=strcmp(s[i].name,s[i+j].name);
                                                                                     if(val>0){
                                                                                               strcpy(temp,s[i].name);
                                                                                               strcpy(s[i].name,s[i+j].name);
                                                                                               strcpy(s[i+j].name,temp);
                                                                                               
                                                                                                atemp=s[i].age;
                                                                                                 s[i].age=s[i+j].age;
                                                                                                 s[i+j].age=atemp;
                                                                                                 
                                                                                                 gtemp=s[i].gwa;
                                                                                                 s[i].gwa=s[i+j].gwa;
                                                                                                 s[i+j].gwa=gtemp;
                                                                                               }
                                                                                        }
                                                                        }
                                                     }
                                                     printf("STUDENT PROFILES\n");
                                                     if(z==0){printf("Memory Empty.Nothing to display\n");
                                                             
                                                              }                                                 
                                                     else {for(i=1;i<z+1;i++){
                                                                             printf("Student Name:%s\n",s[i].name);
                                                                             printf("Age:%d\n",s[i].age);
                                                                             printf("GWA:%f\n",s[i].gwa);
                                                                             }
                                                          }
                                                     choice=printmenu();
                                                }//end of 3rd if 
                                }//end of while loop
                                if(choice==4){
                                              fp=fopen("DIMACALI.txt","w+");
                                              for(i=0;i<z;i++){
                                                                fprintf(fp,"%s",s[i].name);
                                                                fprintf(fp,"\n%d\n",s[i].age);
                                                                fprintf(fp,"%f\n",s.gwa);
                                                                }
                                              fclose(fp);
                                              printf("Thank you for using my program!");
                                              }//end of if 4
           
           
           
    }//END OF MAIN

    Recommendations:
    1. Scrap your K&R indenting style and opt for the far more readable Allman style.
    2. Stop bounding off the right edge of the screen by instantaneously multiple levels of indentation. One level at a time!
    3. Set your tabs to a more manageable size, such as 3 or 4; I prefer 4. Your editor should have an option for that.
    4. Replace your tab characters with spaces. Your editor should have an option for that.
    5. Always use code tags when posting code here so that its indentation will be preserved.


    Originally Posted by ilovetech29
    The problem is that my code would compile well but when I run it, ...
    Impossible. Your code won't even compile. Here is what I get when I compile it with MinGW gcc:
    C:\otros\dcw>gcc -Wall lovetech.c
    lovetech.c:22: warning: return-type defaults to `int'
    lovetech.c: In function `main':
    lovetech.c:25: parse error before `int'
    lovetech.c:34: `z' undeclared (first use in this function)
    lovetech.c:34: (Each undeclared identifier is reported only once
    lovetech.c:34: for each function it appears in.)
    lovetech.c:36: `s' undeclared (first use in this function)
    lovetech.c:44: `choice' undeclared (first use in this function)
    lovetech.c:54: `tr' undeclared (first use in this function)
    lovetech.c:59: `i' undeclared (first use in this function)
    lovetech.c:60: `val' undeclared (first use in this function)
    lovetech.c:62: `temp' undeclared (first use in this function)
    lovetech.c:66: `atemp' undeclared (first use in this function)
    lovetech.c:70: `gtemp' undeclared (first use in this function)
    lovetech.c:78: `j' undeclared (first use in this function)
    lovetech.c:108: `editch' undeclared (first use in this function)
    lovetech.c:198: warning: control reaches end of non-void function

    C:\otros\dcw>
    What compiler are you using? And are you doing anything out of the ordinary, like using C99 or compiling this as C++?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Impossible. Your code won't even compile. Here is what I get when I compile it with MinGW gcc:


    What compiler are you using? And are you doing anything out of the ordinary, like using C99 or compiling this as C++?
    Sorry, first post I had.

    I am using devc++
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,183
    Rep Power
    2222
    Originally Posted by ilovetech29
    Sorry, first post I had.

    I am using devc++
    Which means that the compiler that you are using is MinGW gcc, which is the one that I used in my attempt to compile your program. I know because that is how I got my own copy of MinGW gcc which I continue to use even though I gave up on that buggy Dev-C++ IDE almost immediately.

    The situation still remains that the same compiler failed to compile your program as a C program. However, if I compile it as a C++ program, I only get warnings:
    C:\otros\dcw>g++ -Wall lovetech.cpp
    lovetech.cpp:22: warning: ANSI C++ forbids declaration `main' with no type
    lovetech.cpp: In function `int main()':
    lovetech.cpp:37: warning: format argument is not a pointer (arg 3)
    lovetech.cpp:38: warning: format argument is not a pointer (arg 3)
    lovetech.cpp:121: warning: int format, different type arg (arg 2)
    lovetech.cpp:25: warning: unused variable `int x'

    C:\otros\dcw>
    You still failed to tell me what special conditions you were using, such as compiling it as a C++ program -- I will disallow the possibility of it being C99, since I believe that MinGW gcc had not yet added C99 support by the time that Dev-C++ had stopped being supported. The only possible way I can see for you to have gotten an executable out of that code was for you to have compiled it as C++. So why were you hiding that pertinent fact from us?

    The fact remains that your statement is still clearly false:
    Originally Posted by ilovetech29
    ... my code would compile well ...
    Quite clearly that was not the case! You got warnings! Warnings are a sign of trouble! Warnings are far more important than error messages! You must never ignore warnings! And since you ignored those warnings, your program crashed and you can't understand why. The reason why your program crashed is because it was broken! The warnings told you that it was broken, but you chose to ignore them. Do you also ignore other warnings, like "thin ice", "bridge out", or "high voltage"? Your compiler's warnings are no different.

    Look at those warnings again:
    C:\otros\dcw>g++ -Wall lovetech.cpp
    lovetech.cpp:22: warning: ANSI C++ forbids declaration `main' with no type
    lovetech.cpp: In function `int main()':
    lovetech.cpp:37: warning: format argument is not a pointer (arg 3)
    lovetech.cpp:38: warning: format argument is not a pointer (arg 3)
    lovetech.cpp:121: warning: int format, different type arg (arg 2)
    lovetech.cpp:25: warning: unused variable `int x'

    C:\otros\dcw>
    Now look at select lines from your code:
    format argument is not a pointer (arg 3)
    Line 37 -- fscanf(fp,"\n%d\n",s[z].age);
    Line 38 -- fscanf(fp,"%f",s[z].gwa);
    I group these together because they're the same mistake. *scanf requires memory addresses to know where to store the values that they create when they scan the input string. Instead, you gave it variables, not pointers. So fscanf read what was at those locations, which was undoubtedly garbage anyway, and used those values as pointers, exactly as you had told it to do. Since garbage pointer values are almost guaranteed to point outside your allotted memory space, your program tried to commit illegal access and so was terminated immediately with extreme prejudice.

    s[z].age is the variable's name; &s[z].age is its address. The *scanf family wants addresses, not names.

    warning: int format, different type arg (arg 2)
    Line 121 -- scanf("%d",&s[editch].name);
    The name field is a char array, not an int. To read in a string, you use the "%s" flag. And since array names are largely synonymous with pointers, you don't need the address operator in this case.

    Also:
    warning: ANSI C++ forbids declaration `main' with no type
    Line 22 -- main()
    main is supposed to return an int to the OS. You are using implicit declaration to make it return int, but such implicit declarations are legacy which needs to die; always make all declarations explicitly! Besides, you didn't even return anything. Return zero if the program ran successfully and non-zero if it did not (eg, if it couldn't open that text file). Get into that habit; what you had done here is totally un-sat.

IMN logo majestic logo threadwatch logo seochat tools logo