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++?