Re: hi i am a girl who were trying a bit on C can someone help?
From: Raymond Martineau (bk039_at_freenet.carleton.ca)
Date: 10/25/04
- Next message: Dave Vandervies: "Re: C to Java Byte Code"
- Previous message: Rob Thorpe: "Re: Commenting the source code."
- In reply to: kimimaro: "hi i am a girl who were trying a bit on C can someone help?"
- Next in thread: kimimaro: "Re: hi i am a girl who were trying a bit on C can someone help?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 25 Oct 2004 12:48:43 -0400
On Mon, 25 Oct 2004 11:25:52 -0400, "kimimaro" <little_cloudie@yahoo.com>
wrote:
>here is the 1st program that I've made in which it will ask for you to key
>in a user ID if found it will modify the existing record of the ID you've
>entered and if not found it will call add_record function where you can
>create a new record but since the last 2 days , it continue to fail can
>someone pls help me out thanks in advance
How does it fail? Is the record not appearing in the file, or is there
another problem with it's failure (e.g. it's waxing the entire records.txt
file)?
You may want to take a look at the compiler warnings that get issued. Even
though such warnings are not standard, they do help locate what is going
wrong.
>
>the code are :
>
You might also want to fix the indentation - you seem to be using tabs in
your code (or your compiler is adding them for you), with those tabs set at
a non-stanard size. Try to find an option that tells the compiler that a
tab is equal to 8 spaces, or to only use spaces in the indentation.
>/*Function 1a*/
>
>void modify_record(){
>char Name[50];
>int Target, ID, selection, Found=0;
>FILE *rec, *temp;
>
>temp=fopen("temp.txt", "w+");
You should check the return value here. If it happens that "temp.txt"
doesn't get open, you will successfully hose "records.txt" below.
>if((rec=fopen("record.txt", "r+")) == NULL)
> printf("\nerror: file not found.\n");
>else {
> printf(" ENTER the employee ID to be modified: ");
> scanf("%d", &ID);
You probable mean:
scanf("%d", &Target);
Otherwise, you will end up with Target using an undefined value below.
>
> gotoxy(1,15);
> printf("\n Employee details edit ");
You shouldn't mix-and-match the gotoxy() function with the printf()
function. They are technically from different libraries and don't exactly
understand the presence of each other. However, you can keep it if it
works for you.
BTW, putting a newline at the beinning of a string after setting the
location of the cursor is superflouius. You might want to move it to the
end instead.
> printf("Please enter the department for this employee, [0]
>Administration [1] Management [2] Accounting [3] Others", empty, empty,
>empty, empty);
Unless they enhance some form of readability, the "empty" parameters should
be removed. Otherwise, they're just slowing down the call to printf.
> scanf("%d",&selection);
A user can potentially crash the application if he types in 'a'. You may
want to replace this with a fgets/sscanf pair once you learn more about the
language.
Also, you may want to assign a default value for selection before the
scanf() statement - otherwise, your program might falsly detect that the
user made one of the selections listed below if he typed an invalid value.
> if(selection==0 ){
> printf("\n Your selection is %s\n\n%s%s\n ",
>Department[selection], empty, empty);
> fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
>Department[selection]);
> }
> else if(selection==1){
> printf("\n Your selection is %s\n\n%s%s\n ",
>Department[selection], empty, empty);
> fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
>Department[selection]);
> }
> else if(selection==2){
> printf("\n Your selection is %s\n\n%s%s\n ",
>Department[selection], empty, empty);
> fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
>Department[selection]);
> }
> else if(selection==3){
> printf("\n Your selection is %s\n\n%s%s\n ",
>Department[selection], empty, empty);
> fprintf(temp, "%i %*c %c %*c", ID, Name, Gender,
>Department[selection]);
> }
> else
> printf("\n\n INVALID CHOICE! PLEASE Re-Enter.\a");
You may want to convert the if/else ladder into a switch statement. While
you might not have learned how to do it yet, the switch statement is
generally more suitable for making decisions based on one variable.
> } while(selection!=0 && selection!=1 && selection!=2 &&
>selection!=3);
Replace with:
} while (selection < 0 || selection > 3);
> /*Function 1b adding employee*/
>
> void add_record(){
> int ID;
> int selection;
> FILE *rec, *temp;
>
>if ((rec=fopen("record.txt","a+"))==NULL)
This is your problem right here. It does create the record successfully,
but since it is called by modify_record, the change made won't m
> printf("The new ID for this employee: %04i", ID);
> printf("\n Employee Name : \n");
> fflush(stdin);
> gets(Name);
Replace gets(Name) with fgets(Name, 49, stdin).
Otherwise, a person will wonder why the name
"Ghosseindhatsghabyfaird-johnson" can potentically crash the system. (Yes,
that name does exist: http://www.netfunny.com/rhf/jokes/new91/rshak.html)
> printf("\n Employee Gender: \n");
> scanf("%c", &Gender);
You might want to check this field to make sure that it only accepts 'M'
and 'F', and to ensure that the capitilization of those letters is
consistant.
- Next message: Dave Vandervies: "Re: C to Java Byte Code"
- Previous message: Rob Thorpe: "Re: Commenting the source code."
- In reply to: kimimaro: "hi i am a girl who were trying a bit on C can someone help?"
- Next in thread: kimimaro: "Re: hi i am a girl who were trying a bit on C can someone help?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|