add,view=ok, search,edit got error... help!

This is a discussion on add,view=ok, search,edit got error... help! within the C Programming forums, part of the General Programming Boards category; search,edit,delete error.. only add and view were working good... Code: #include<stdio.h> #include<stdlib.h> #include<string.h> FILE *grading; float sci,math,eng,ave; char name[50]; main() ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    5

    add,view=ok, search,edit got error... help!

    search,edit,delete error..
    only add and view were working good...


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    FILE *grading;
    float sci,math,eng,ave;
    char name[50];
    
    
    main()
    {
    
    int choice;
    
    mainmenu:
    {
    system("CLS");
    fflush(stdin);
    printf("\n\n\n\n\n\n\t\t\t************************ ****");
    printf("\n\t\t\t* Student Grading System *");
    printf("\n\t\t\t****************************");
    printf("\n\t\t\t [1] Add");
    printf("\n\t\t\t [2] View");
    printf("\n\t\t\t [3] Edit");
    printf("\n\t\t\t [4] Search");
    printf("\n\t\t\t [5] Delete");
    printf("\n\t\t\t [6] Empty Record");
    printf("\n\t\t\t [7] Exit");
    printf("\n\t\t\t****************************");
    printf("\n\t\t\tPlease Enter Your Choice : ");
    scanf("%d",&choice);
    
    switch (choice)
    {
    case 1 : goto add;
    case 2 : goto view;
    case 3 : goto edit;
    case 4 : goto search;
    case 5 : goto dele;
    case 6 : goto empty;
    case 7 :
    printf("\n\n\n\t\t******************************** *****");
    printf("\n\n\n\n\n\t\t\t Thank you!!!\n\n\n\n\n");
    printf("\n\t\t************************************ *");
    printf("\n\n\n\n\t\t press any key to exit...");
    getch();
    return 0;
    default:
    printf("\n\t\t\tInvalid Choice!!!\n\t\t\tPlease refer to the menu!!!");
    system("PAUSE");
    goto repeat;
    }
    }
    
    add:
    {
    char choice;
    
    system("CLS");
    fflush(stdin);
    printf("Enter Full Name : ");
    scanf("%[^\t\n0-9]",&name);
    printf("Enter grade in English : ");
    scanf("%f",&eng);
    printf("Enter grade in Math : ");
    scanf("%f",&math);
    printf("Enter grade in Science : ");
    scanf("%f",&sci);
    
    ave = (eng+math+sci)/3;
    printf("\n-------------------------------\n");
    printf("Your average is %.2f",ave);
    
    /* SAVE FILE */
    grading=fopen("record.txt","a+");
    fprintf(grading,"%s %.2f %.2f %.2f %.2f\n",name,eng,math,sci,ave);
    fflush(grading);
    fclose(grading);
    
    /*SHOW MESSAGE*/
    printf("\n-------------------------------");
    printf("\nRecord Added!");
    printf("\n-------------------------------");
    
    fflush(stdin);
    
    repeat:
    printf("\n\nDo you want to add another record?\n\n");
    printf("[Y] for new a record or [N] to go back to main menu : ");
    scanf("%c",&choice);
    if((choice=='Y')||(choice=='y'))
    {
    goto add;
    }
    else if((choice=='N')||(choice=='n'))
    {
    goto mainmenu;
    }
    else
    {
    system("CLS");
    printf("\nInvalid choice!!\n");
    goto repeat;
    }
    view:
    {
    int ctr=0;
    
    grading=fopen("record.txt","a+");
    while(!feof(grading))
    {
    fscanf(grading,"%s %f %f %f %f",&name,&eng,&math,&sci,&ave);
    if(feof(grading))
    {
    break;
    }
    printf("-------------------------------");
    printf("\nName : %s\n",name);
    printf("English : %.2f\n",eng);
    printf("Math : %.2f\n",math);
    printf("Science : %.2f\n",sci);
    printf("Average : %.2f\n",ave);
    ctr++;
    
    }
    fclose(grading);
    printf("\n-------------------------------");
    printf("\nPress any key to continue...");
    getch();
    goto mainmenu;
    }
    
    edit:
    {
    char tempdata[100],search[50],nname[50];
    float nsci,nmath,neng;
    int test,test2;
    FILE *temp;
    
    system("CLS");
    
    temp=fopen("temprecord.txt","a+");
    grading=fopen("record.txt","r");
    
    printf("Enter the full name : ");
    scanf("%s",&search);
    do{
    fscanf(grading,"%s %f %f %f %f",&name,&eng,&math,&sci,&ave);
    test=stricmp(name,search);
    if(feof(grading))
    {
    break;
    }
    else if(test!=0)
    {
    fprintf(temp,"%s %f %f %f %f\n",name,eng,math,sci,ave);
    }
    else
    {
    printf("%s has been found\n\n",name);
    strcpy(nname,name);
    neng=eng;
    nmath=math;
    nsci=sci;
    test2=test;
    }
    }
    while(!feof(grading));
    if(test2==0)
    {
    printf("Current Name is %s , Enter New Name : ",nname);
    scanf("%s",&name);
    printf("Current grade is %.2f, Enter new grade in English : ",neng);
    scanf("%f",&eng);
    printf("Current grade is %.2f, Enter new grade in Math : ",nmath);
    scanf("%f",&math);
    printf("Current grade is %.2f, Enter new grade in Science : ",nsci);
    scanf("%f",&sci);
    
    ave = (eng+math+sci)/3;
    printf("\n-------------------------------\n");
    printf("Your average is %.2f\n",ave);
    system("PAUSE");
    
    fprintf(temp,"%s %.2f %.2f %.2f %.2f\n",name,eng,math,sci,ave);
    printf("\n-------------------------------");
    printf("\nRecord is updated!\n");
    system("PAUSE");
    }
    else
    {
    printf("\n-------------------------------");
    printf("Record not found!!");
    }
    
    remove("record.txt");
    rename("tempreco.txt","record.txt");
    printf("\n-------------------------------");
    printf("\nPress any key to continue...");
    goto mainmenu;
    
    }
    
    search:
    {
    char choice,search[50];
    int ctr=0;
    
    system("CLS");
    fflush(stdin);
    grading=fopen("record.txt","r+");
    printf("Enter the full name : ");
    scanf("%s",&search);
    while(!feof(grading))
    {
    fscanf(grading,"%s %f %f %f %f",&name,&eng,&math,&sci,&ave);
    }
    if(feof(grading))
    getch();
    else if(strcmp(search,name)==1)
    {
    printf("-------------------------------");
    printf("\nName : %s\n",name);
    printf("English : %.2f\n",eng);
    printf("Math : %.2f\n",math);
    printf("Science : %.2f\n",sci);
    printf("Average : %.2f\n",ave);
    ctr++;
    }
    else
    {
    printf("-------------------------------\n");
    printf("The name doesn't exist!!!");
    }
    fflush(stdin);
    
    repeat2:
    printf("\n-------------------------------");
    printf("\nDo you want to search again?\n\n");
    printf("[Y] for yes or [N] to go back to main menu: ");
    scanf("%c",&choice);
    if((choice=='Y')||(choice=='y'))
    {
    goto search;
    }
    else if((choice=='N')||(choice=='n'))
    {
    goto mainmenu;
    }
    else
    system("CLS");
    printf("\nInvalid choice!!\n");
    goto repeat2;
    
    }
    
    dele:
    {
    char choice,tempdata[100],search[50],nname[50];
    int test,ctr=0;
    FILE *temp;
    
    system("CLS");
    temp=fopen("temprecord.txt","a+");
    grading=fopen("record.txt","a+");
    printf("-------------------------------\n");
    printf("Name(s) :\n");
    printf("-------------------------------\n");
    while(!feof(grading))
    {
    fscanf(grading,"%s %f %f %f %f",&nname,&eng,&math,&sci,&ave);
    }
    if(feof(grading))
    {
    printf("%s\n",nname);
    }
    printf("\n-------------------------------\n");
    
    grading=fopen("record.txt","r");
    fflush(stdin);
    printf("Enter the full name : ");
    scanf("%s",&search);
    printf("\n-------------------------------\n");
    do
    {
    fscanf(grading,"%s %f %f %f %f",&name,&eng,&math,&sci,&ave);
    test=stricmp(name,search);
    if(feof(grading))
    {
    break;
    }
    else if(test!=0)
    {
    fprintf(temp,"%s %f %f %f %f\n",name,eng,math,sci,ave);
    }
    else
    {
    printf("%s has been deleted\n",name);
    ctr++;
    }
    }
    while(!feof(grading));
    if(ctr==0)
    {
    printf("-------------------------------\n");
    printf("The name doesn't exist!!!\n");
    }
    fclose(temp);
    fclose(grading);
    remove("record.txt");
    rename("tempreco.txt","record.txt");
    printf("-------------------------------");
    fflush(stdin);
    
    repeat4:
    printf("\n\nDo you want to delete another record?\n\n");
    printf("[Y] for Yes or [N] to go back to main menu: ");
    scanf("%c",&choice);
    if((choice=='Y')||(choice=='y'))
    {
    goto mainmenu;
    }
    else if((choice=='N')||(choice=='n'))
    {
    goto mainmenu;
    }
    else
    {
    system("CLS");
    printf("\nInvalid choice!!\n");
    goto repeat4;
    }
    }
    
    empty:
    {
    system("CLS");
    printf("-------------------------------\n");
    printf("Record Emptied!!");
    grading=fopen("record.txt","w+");
    fclose(grading);
    printf("\n-------------------------------");
    system("Pause");
    goto mainmenu;
    }
    
    }
    getch();
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. Learn to indent. No one wants to read that.
    2. What error? No one wants to guess.


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

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    goto? Learn to use functions.


    Code:
    fflush(stdin);

    fflush
    is not defined for input streams.




    Code:
    grading=fopen("record.txt","a+");
    while(!feof(grading))
    {
        fscanf(grading,"%s %f %f %f %f",&name,&eng,&math,&sci,&ave);
        if(feof(grading))
        {
            break;
        }
        ...
    }
    You've managed to deal with the quirks of using feof to control a loop by using a second call after the read operation. A better way is to simply use the return value of the fscanf call instead to control the loop. This way you avoid some slightly awkward coding.
    Code:
    grading=fopen("record.txt","a+");
    while(fscanf(grading,"%s %f %f %f %f",&name,&eng,&math,&sci,&ave)==5)
    {
        ...
    }



    Code:
    edit:
    {
       char tempdata[100],search[50],nname[50];
       float nsci,nmath,neng;
       int test,test2;
       FILE *temp;
    
       system("CLS");
    
       temp=fopen("temprecord.txt","a+");
    
       ...
    
       remove("record.txt");
       rename("tempreco.txt","record.txt");
       printf("\n-------------------------------");
       printf("\nPress any key to continue...");
       goto mainmenu;
    
    }
    See anything wrong there? FYI you also do this in the delete portion of the code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    5
    still i can't create a list, it's only view the last input data.... in search, always record not found.... in edit, record updated but not saving.... pls help...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    struct student
    {
    float sci,math,eng,ave;
    char name[50];
    }stud;
    
    
    FILE *grading;
    
    main()
    {
    
    int choice;
    
    mainmenu:
          {     
           system("CLS");
           fflush(stdin);
           printf("\n\n\n\n\n\n\t\t\t****************************");
           printf("\n\t\t\t* Student Grading System *");
           printf("\n\t\t\t****************************");
           printf("\n\t\t\t [1] Add");
           printf("\n\t\t\t [2] View");
           printf("\n\t\t\t [3] Edit");
           printf("\n\t\t\t [4] Search");
           printf("\n\t\t\t [5] Delete");
           printf("\n\t\t\t [6] Empty Record");
           printf("\n\t\t\t [7] Exit");
           printf("\n\t\t\t****************************");
           printf("\n\t\t\tPlease Enter Your Choice : ");
           scanf("%d",&choice);
    
           switch (choice)
               {
                  case 1 : goto add;
                  case 2 : goto view;
                  case 3 : goto edit;
                  case 4 : goto search;
                  case 5 : goto dele;
                  case 6 : goto empty;
                  case 7 :
                         printf("\n\n\n\t\t*************************************");
                         printf("\n\n\n\n\n\t\t\t   Thank you!!!\n\n\n\n\n");
                         printf("\n\t\t*************************************");
                         printf("\n\n\n\n\t\t      press any key to exit...");
                         getch();
                         return 0;
                  default:
                     printf("\n\t\t\tInvalid Choice!!!\n\t\t\tPlease refer to the menu!!!");
                  system("PAUSE");
                  goto repeat;
                }
           }
    
    add:
           {
             char choice;
                  
             system("CLS");
             fflush(stdin);
             printf("Enter Full Name : ");
             scanf("%s",&stud.name);
             printf("Enter grade in English : ");
             scanf("%f",&stud.eng);
             printf("Enter grade in Math : ");
             scanf("%f",&stud.math);
             printf("Enter grade in Science : ");
             scanf("%f",&stud.sci);
    
             stud.ave=(stud.eng+stud.math+stud.sci)/3;
             printf("\n-------------------------------\n");
             printf("Your average is %.2f",stud.ave);
    
             /* SAVE FILE */
             grading=fopen("record.txt","a+");
             fprintf(grading,"%s %.2f %.2f %.2f %.2f\n",stud.name,stud.eng,stud.math,stud.sci,stud.ave);
             fflush(grading);
             fclose(grading);
    
             /*SHOW MESSAGE*/
             printf("\n-------------------------------");
             printf("\nRecord Added!");
             printf("\n-------------------------------");
    
             fflush(stdin);
    
             repeat:
             printf("\n\nDo you want to add another record?\n\n");
             printf("[Y] for new a record or [N] to go back to main menu : ");
             scanf("%c",&choice);
                if((choice=='Y')||(choice=='y')) 
                {
                goto add;
                }
                else if((choice=='N')||(choice=='n'))
                {
                goto mainmenu;
                }
                else
                 {
                  system("CLS");
                  printf("\nInvalid choice!!\n");
                  goto repeat;
                 }
    view:
            {
              int c=1;
              
              grading=fopen("record.txt","a+");
              while(fscanf(grading,"%s %f %f %f %f",&stud.name,&stud.eng,&stud.math,&stud.sci,&stud.ave)==5)
              {
                  printf("-------------------------------");
                  printf("\nName    : %s\n",stud.name);
                  printf("English : %.2f\n",stud.eng);
                  printf("Math    : %.2f\n",stud.math);
                  printf("Science : %.2f\n",stud.sci);
                  printf("Average : %.2f\n",stud.ave);
                  c++;
                  if(c>5)
                  {
                  c=1;
                  printf("Press key to next");
                  getch();
                  }
                  printf("End of List");
                  getch();
                  goto mainmenu;
             }
           }
    
    edit:
             {
               char tempdata[100],find[50],nname[50];
               float nsci,nmath,neng,ave;
               int test,test2;
               FILE *temp;
               
               system("CLS");
    
               temp=fopen("temprecord.txt","a+");
               grading=fopen("record.txt","a+");
    
               printf("Enter the full name : ");
               scanf("%s",&find);
               do{
                 fscanf(grading,"%s %.2f %.2f %.2f %.2f",&stud.name,&stud.eng,&stud.math,&stud.sci,&stud.ave);
                       { 
                        if((strcmp(find,stud.name))==0)
                           {
                           printf("%s has been found\n\n",stud.name);
                           strcpy(nname,stud.name); 
                           neng=stud.eng;
                           nmath=stud.math;
                           nsci=stud.sci;
                           test2=test;
                           }
                        else
                           {
                           printf("\nRecord not found!!");
                           getch();
                           goto edit;
                           }
                      
                         }                                                       
                    } while((test=test2)&&(test2==0));
                      {
                      printf("\nCurrent Name is %s , Enter New Name : ",stud.name);
                      scanf("%s",&nname);
                      printf("\nCurrent grade is %.2f, Enter new grade in English : ",stud.eng);
                      scanf("%f",&neng);
                      printf("\nCurrent grade is %.2f, Enter new grade in Math : ",stud.math);
                      scanf("%f",&nmath);
                      printf("\nCurrent grade is %.2f, Enter new grade in Science : ",stud.sci);
                      scanf("%f",&nsci);
                      ave = (stud.eng+stud.math+stud.sci)/3;
                      printf("\n-------------------------------\n");
                      printf("\nYour average is %.2f\n",stud.ave);
                      system("PAUSE");
    
                      fprintf(temp,"%s %.2f %.2f %.2f %.2f\n",stud.name,stud.eng,stud.math,stud.sci,stud.ave);
                      printf("\n-------------------------------");
                      printf("\nRecord is updated!\n");
                      system("PAUSE");
                      }
                      
                      remove("record.txt");
                      rename("temprecord.txt","record.txt");
                      printf("\n-------------------------------");
                      printf("\nPress any key to continue...");
                      getch();
                      goto mainmenu;
                      
    
              }
    
    search:
             {
               int ctr=0;
               char choice;
               char search[50];
                                     
                system("CLS");
                fflush(stdin);
                grading=fopen("record.txt","r+");
                printf("Enter the full name : ");
                scanf("%s",&search);
                while(fread(&stud,sizeof(stud),1,grading)==1)
         {
          if((strcmpi(stud.name,search))==0)
                                       {
                           printf("%s %.2f %.2f %.2f %.2f",stud.name,stud.eng,stud.math,stud.sci,stud.ave);
                           }
                        else
                           {
                           printf("\nRecord not found!!");
                           getch();
                           goto search;
                           }
                        }
                            
                fflush(stdin);
                
                repeat2:
                  printf("\n-------------------------------");
                  printf("\nDo you want to search again?\n\n");
                  printf("[Y] for yes or [N] to go back to main menu: ");
                  scanf("%c",&choice);
                  if((choice=='Y')||(choice=='y')) 
                  {
                  goto search;
                  }
                  else if((choice=='N')||(choice=='n')) 
                  {
                  goto mainmenu;
                  }
                  else
                  system("CLS");
                  printf("\nInvalid choice!!\n");
                  goto repeat2;
    
    }
    
    dele:
       {
         char choice,tempdata[100],nname[50],search[50];
         int test,ctr1=0;
         FILE *temp;
    
         system("CLS");
         temp=fopen("temprecord.txt","a+");
         grading=fopen("record.txt","a+");
         printf("-------------------------------\n");
         printf("Name(s) :\n");
         printf("-------------------------------\n");
         while(fscanf(grading,"%s %f %f %f %f",&stud.name,&stud.eng,&stud.math,&stud.sci,&stud.ave)==5)
              {
                  printf("-------------------------------");
                  printf("\nName  : %s\n",stud.name);
                  printf("English : %.2f\n",stud.eng);
                  printf("Math    : %.2f\n",stud.math);
                  printf("Science : %.2f\n",stud.sci);
                  printf("Average : %.2f\n",stud.ave);
                  ctr1++;
                }
              
         printf("\n-------------------------------\n");
         grading=fopen("record.txt","r");
         fflush(stdin);
         printf("Enter the full name : ");
         scanf("%s",&search);
         printf("\n-------------------------------\n");
         do
         {
         fscanf(grading,"%s %f %f %f %f",&stud.name,&stud.eng,&stud.math,&stud.sci,&stud.ave);
         test=stricmp(search,stud.name);
         if(feof(grading))
           {
             break;
           }
           else if(test!=0) 
           {
             fprintf(temp,"%s %f %f %f %f\n",stud.name,stud.eng,stud.math,stud.sci,stud.ave);
           }
           else
           {
           printf("%s has been deleted\n",stud.name);
           ctr1++;
           }
         }
    while(!feof(grading));
    if(ctr1==0)
     {
      printf("-------------------------------\n");
      printf("The name doesn't exist!!!\n");
     }
     fclose(temp);
     fclose(grading);
     remove("record.txt");
     rename("tempreco.txt","record.txt");
     printf("-------------------------------");
     fflush(stdin);
    
     repeat4:
     printf("\n\nDo you want to delete another record?\n\n");
     printf("[Y] for Yes or [N] to go back to main menu: ");
     scanf("%c",&choice);
     if((choice=='Y')||(choice=='y')) 
     {
      goto mainmenu;
     }
     else if((choice=='N')||(choice=='n')) 
     {
     goto mainmenu;
     } 
     else 
     {
     system("CLS");
     printf("\nInvalid choice!!\n");
     goto repeat4;
     }
    }
    
    empty:
           {
           system("CLS");
           printf("-------------------------------\n");
           printf("Record Emptied!!");
           grading=fopen("record.txt","w+");
           fclose(grading);
           printf("\n-------------------------------");
           system("Pause");
           goto mainmenu;
           }
           
    }
    getch();
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do your names involve spaces? Because you won't be able to read in something with a space using scanf and %s.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21