Journal Articles
Browse in : |
All
> Journals
> CVu
> 114
(20)
All > Journal Columns > Code Critique (70) Any of these categories - All of these categories |
Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.
Title: Code Critique Competition
Author: Administrator
Date: 03 June 1999 13:15:31 +01:00 or Thu, 03 June 1999 13:15:31 +01:00
Summary:
Body:
We can often learn from the mistakes and struggles of novices. I am often surprised when I discover things I always knew are untrue, and novice code is a rich source for such insights. Rather than leave all the work (and consequential benefits) to myself and The Harpist I have decided to provide regular problem code from genuine novices for your inspection.
Your task is a very simple one. Read the following, study the code and then provide as helpful a response as you can. The result will be judged for clarity of explanation as well as accuracy in identifying problems. There will be a prize for the entry that I consider would best meet the needs of the original questioner. Rewriting the code to make it clean and intelligible is certainly desirable.
By the way, the code is exactly as it was originally presented except that I corrected a couple of spelling mistakes in the comments.
I want my program to do this:
You should be able to add your forename, surname and finally your personal number. If you input 0 as your personal number you will stop adding more people to the register.
My problem is that my sorting routine does not work.
I want to sort by the personal numbers and display the results on the screen.
so....
-
What is wrong with my sorting routine?
-
How should I construct a clean stop when I don't want to add more people to the register (I want to use 0 as last personal number)
I know there are a lot of errors in the code besides this one…but I'm a beginner and need time to develop a program that work at all , but right now I just want this to work .
#include <stdio.h> #include <string.h> #include <conio.h> //Not standard in C i know...:-) #include <stdlib.h> #include <io.h> //Prototyper void lasin(); void meny(); void sortera(); void avsluta(); void utskrift(); typedef struct{ char pnumber[15]; char forname[10]; char lastname[15]; } perspost; int main(void){ meny(); getch(); return 0; } void meny(){ int val; clrscr(); printf(" MENY "); puts(""); puts(""); printf("1......Add people to the register\n"); printf("2......print\n"); printf("Val:"); scanf("%d",&val); switch(val){ case 1: lasin(); break; case 2: sortera(); break; } } void lasin(){ perspost preg; FILE *my_fil; char namn[10]; int i=0,ptr=1; clrscr(); printf("filename(must be named 'fil.txt' :-)? :"); scanf("%s",namn); my_fil=fopen(namn,"wb"); fseek(my_fil,0,SEEK_SET); clrscr(); while(ptr!=0){ printf("personnummer(personalnumber): "); scanf("%s",preg.pnumber); ptr=atoi(preg.pnumber); if(ptr==0) meny(); printf("Förnamn(forname): "); scanf("%s",preg.forname); printf("Efternamn(lastname: "); scanf("%s",preg.lastname); fseek(my_fil,i*sizeof(perspost),SEEK_SET); fwrite(&preg ,sizeof(perspost),1,my_fil); i++; } fclose(my_fil); } /*Mabe i wont have to open the file agein?? Something is wrong here!!?? */ void sortera(){ FILE *my_fil; int sf,ft,storlek,k,m; perspost pos1,pos2,tmp; my_fil=fopen("fil.txt","rb"); fseek(my_fil,0,SEEK_END); ft=ftell(my_fil); sf=sizeof(perspost); storlek=ft/sf; for(k=1;k<storlek;k++) { fseek(my_fil,k*sf,SEEK_SET); fread(&pos1,sf,1,my_fil); for(m=k+1;m<=storlek;m++) { fseek(my_fil,m*sf,SEEK_SET); fread(&pos2,sf,1,my_fil); if(strcmp(pos1.pnumber,pos2.pnumber)>0) { tmp=pos1; pos1=pos2; pos2=tmp; fseek(my_fil,k*sf,SEEK_SET); fwrite(&pos1,sf,1,my_fil); fseek(my_fil,m*sf,SEEK_SET); fwrite(&pos2,sf,1,my_fil); } } } //Something may be wrong here too.... utskrift(); } void utskrift(){ FILE *my_fil; int n=0,sf,ft,storlek; perspost preg; clrscr(); my_fil=fopen("fil.txt","rb"); fseek(my_fil,0,SEEK_END); ft=ftell(my_fil); sf=sizeof(perspost); storlek=ft/sf; while(n<=storlek){ fseek(my_fil,n*sizeof(preg),SEEK_SET); fread(&preg,sizeof(preg),1,my_fil); printf("%s\n",preg.pnumber); printf("%s\n",preg.forname); printf("%s\n",preg.lastname); n++; } printf("END"); getch(); meny(); }
Notes:
More fields may be available via dynamicdata ..