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


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.



Relevant Pages

  • Re: Has thought been given given to a cleaned up C? Possibly called C+.
    ... leave the switch statement alone and add a new form of selection ... The advantage is you don't need a goto label. ... compiler being used in 1980 in which ... they aren't keywords. ...
    (comp.lang.c)
  • Re: Handling 5000 different functionalities - Optimised way in C
    ... the jump table that's typically used to implement a switch statement. ... Perhaps that's not technically a jump table, but AFAIK it's the closest thing* one can implement in C, therefore the name is descriptive enough. ... I agree that the compiler _could_ generate something faster, by jumping to code inside the same function instead of calling another function, but that shouldn't be a noticeable difference. ... that assumes the table is sorted, which may not always be true, my first attempt was obviously wrong and I decided getting the point across was more important than figuring out how to code a binary search on a non-power-of-two-sized array, and a binary search may have a larger I-cache footprint, bomb the branch predictor, and/or confuse the data prefetcher.) ...
    (comp.lang.c)
  • Re: Simple program crashing
    ... compiler would have told you the problem the first time you compiled ... Even with -ffree-form, g77 objects to GO ... putting TABs in source code is almost as bad an idea as writing a ... there are no tabs in the source files, so something went wrong when pasting to or from the mail reader. ...
    (comp.lang.fortran)
  • Re: [OT] Switch question
    ... On Sat, 21 Feb 2004, Sean Kenwrick wrote: ... > a byte code interpreter I am working on, and I am wondering if I should take ... Each compiler can do it differently. ... If a switch statement used branching (a parallel ...
    (comp.lang.c)
  • Re: why doesnt this work? final assignments and switches
    ... whatever, however, it seems that the compiler only performs a very ... This should be sufficient to work a switch statement - I believe. ... Sure, there are work arounds, but since java offered modest developer ... conveniences to sdk1.5 - how about this one which helps the "hand coder". ...
    (comp.lang.java.programmer)