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

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Something wrong with my sorting?


    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");//should already exist.
    	int x,atemp,choice,i,j,z=-1,val,editch;
    	char temp[50];
    	float gtemp;
    	struct student s[20];
    	while(fp!=NULL && !feof(fp)){
    		z++;
            fgets(s[z].name,50,fp);
    		fscanf(fp,"\n%d\n",&s[z].age);
    		fscanf(fp,"%f\n",&s[z].gwa);
    		
    	}//end of while
    	fclose(fp); 
    
    	do{
    		choice=printmenu();
    		switch(choice){
    			case 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){
    							for(i=0;i<z;i++){
    								for(j=1;(i+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++; 
    						break;
    				
    				
    			case 2:
    						printf("Which among the students would you like to edit?\n");
    						if(!z){
    							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);
    							switch(i){
    								case 0:
    									gets(s[editch].name);
    									printf("New Student Name:");
    									gets(s[editch].name);
    									break;
    								case 1:
    									printf("New Age:");
    									scanf("%d",&s[editch].name);
    									break;
    								case 2:
    									printf("\nNew GWA:");
    									scanf("%f",&s[editch].gwa);
    							}
    						}
    						break;
    			
    			case 3:
    					if(z>0){
    							for(i=0;i<z;i++){
    								for(j=1;(i+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=0;i<=z;i++){
    								printf("\nStudent Name:%s\n",s[i].name);
    								printf("Age:%d\n",s[i].age);
    								printf("GWA:%f\n",s[i].gwa);
    							}
    						}
    						break;
    				
    			case 4:
    						if(z){
    							fp=fopen("DIMACALI.txt","w");
    							for(i=0;i<z;i++){
    								fprintf(fp,"%s\n",s[i].name);
    								fprintf(fp,"%d\n",s[i].age);
    								fprintf(fp,"%f\n",s[i].gwa);
    							}
    							fclose(fp);
    						}
    						printf("Thank you for using my program!");
    						break;
    		}
    	}while(choice!=4);//end of while loop
    
    
    
    }//END OF MAIN
    There's something wrong with my sorting algorithm...
    everytime a value is swapped, what happens is that the value turns to trash.What's wrong with it?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    There's something wrong with your indentation.

    Please stop using hard tabs to indent code. The board takes them far too literally.

    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");  //should already exist.
      int x, atemp, choice, i, j, z = -1, val, editch;
      char temp[50];
      float gtemp;
      struct student s[20];
      while (fp != NULL && !feof(fp)) {
        z++;
        fgets(s[z].name, 50, fp);
        fscanf(fp, "\n%d\n", &s[z].age);
        fscanf(fp, "%f\n", &s[z].gwa);
    
      }                             //end of while
      fclose(fp);
    
      do {
        choice = printmenu();
        switch (choice) {
        case 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) {
            for (i = 0; i < z; i++) {
              for (j = 1; (i + 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++;
          break;
    
        case 2:
          printf("Which among the students would you like to edit?\n");
          if (!z) {
            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);
            switch (i) {
            case 0:
              gets(s[editch].name);
              printf("New Student Name:");
              gets(s[editch].name);
              break;
            case 1:
              printf("New Age:");
              scanf("%d", &s[editch].name);
              break;
            case 2:
              printf("\nNew GWA:");
              scanf("%f", &s[editch].gwa);
            }
          }
          break;
    
        case 3:
          if (z > 0) {
            for (i = 0; i < z; i++) {
              for (j = 1; (i + 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 = 0; i <= z; i++) {
              printf("\nStudent Name:%s\n", s[i].name);
              printf("Age:%d\n", s[i].age);
              printf("GWA:%f\n", s[i].gwa);
            }
          }
          break;
    
        case 4:
          if (z) {
            fp = fopen("DIMACALI.txt", "w");
            for (i = 0; i < z; i++) {
              fprintf(fp, "%s\n", s[i].name);
              fprintf(fp, "%d\n", s[i].age);
              fprintf(fp, "%f\n", s[i].gwa);
            }
            fclose(fp);
          }
          printf("Thank you for using my program!");
          break;
        }
      } while (choice != 4);        //end of while loop
    
    }                               //END OF MAIN
    > val = strcmp(s[i].name, s[i + j].name);
    You should be comparing (and swapping)
    val = strcmp(s[i].name, s[j].name);

    Also, you can copy whole structures, so you can do this
    Code:
    struct student temp;
    temp = s[i];
    s[i] = s[j];
    s[j] = temp;
    > int printmenu()
    It's a start, but where are functions like sortRecords(), inputFromFile() and so on to make main a simple controlling function rather than a bloated do-everything function.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by salem
    > int printmenu()
    It's a start, but where are functions like sortRecords(), inputFromFile() and so on to make main a simple controlling function rather than a bloated do-everything function.
    im really sorry, im just a beginner here.anyway thanks for the tip about swapping :)

IMN logo majestic logo threadwatch logo seochat tools logo