Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 107,710 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,117 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



{ .. logic error in my program ..! }

 
Reply to this topicStart new topic

{ .. logic error in my program ..! }, multi-operations program

Mi$s-Q8
post 19 Jul, 2008 - 11:31 AM
Post #1


New D.I.C Head

*
Joined: 30 Apr, 2008
Posts: 21


My Contributions



hi,

this is my 3rd assigment, it's about text files
the program is multi-operations program, that user have to enter a letter of operation code that is defined in the main ...
with no compilation error, i beleive that i have some logic errors, because the program isn't working well

plz, if any one have any notation that could help .. help me !
thanz for reading , smile.gif

CODE

/**************************************************************/
/**************************************************************/
/********************** Assigment 3 *****************************/
/**************************************************************/
/**************************************************************/                                              


#include<stdio.h>
#include<stdlib.h>
#include<string.h>



typedef struct student
{
    int id;
    char fn[30];
    char ln[30];
    float md;
    float final;
    struct student *next;
    }node;



/**************************************************************/
/* which add a node to a list in its correct order position   */
/**************************************************************/

void insert_in_order(node *p,FILE *in,FILE *out)
{
    node *t,*s,*curr,*prev;
    int flag=0;
    
    t=(node*)malloc(sizeof(node));
    fscanf(in,"%d",&(t->id));
    fscanf(in,"%s",t->fn);
    fscanf(in,"%s",t->ln);
    fscanf(in,"%f",&(t->md));
    fscanf(in,"%f",&(t->final));
    t->next=NULL;
    s=p;
    while(s!=NULL&&flag!=1)
    {
        if(t->id==s->id)
        {
            fprintf(out,"ID is already insert");
            flag=1;
        }
        s=s->next;
    }
    if(flag==0)
    {
        if(p==NULL)
            p=t;
        else if (strcmp(p->fn,t->fn)==1)
        {
            t->next=p;
            p=t;
        }
        else
        {
            curr=p;
            while (strcmp(t->fn,curr->fn)==1&&curr!=NULL)
            {
                prev=curr;
                curr=curr->next;
            }
            prev->next=t;
            t->next=curr;
        }
    }
}

/**************************************************************/
/* which removes a particular node from the list using ID     */
/**************************************************************/

void delet_node(node *p,FILE *in,FILE *out)
{
    node *prev=NULL,*curr=p;
    int id;
    fscanf(in,"id",&id);
    while(curr!=NULL&&curr->id!=id)
    {
        prev=curr;
        curr=curr->next;
    }
    if(curr==NULL)
        fprintf(out,"The ID not exist");
    else
    {
        prev->next=curr->next;
        curr->next=NULL;
        free(curr);
    }
}

/**************************************************************/
/* which lists all the records in the linked list            */
/**************************************************************/

void list_records(node *p,FILE *out)
{
    if(p!=NULL)
    {
        fprintf(out,"%d ",(p->id));
        fprintf(out,"%s ",(p->fn));
        fprintf(out,"%s ",p->ln);
        fprintf(out,"%f ",p->md);
        fprintf(out,"%f\n",p->final);
        list_records(p->next,out);
    }
}

/**************************************************************/
/* which calculates and returns the average of final exams   */
/**************************************************************/

float calc_average(node *p)
{
    static float sum=0;
    static int c=0;
    if(p==NULL)
        return (sum/c);
    else
    {
        sum=sum+p->final;
        c++;
        return (calc_average(p->next));
    }
}


/**************************************************************/
/* which findes and returns a particular record using the ID  */
/* you should print the whole record from the main .          */
/**************************************************************/

node *find_record(node *p,FILE *in)
{
    int id;
    fscanf(in,"%d",&id);
    if(p==NULL)
        return NULL;
    else if(p->id==id)
        return p;
    else
        return (find_record(p->next,in));
}


main()
{
    FILE *in,*out;
    node *p=NULL,*r;
    char ch;

    in=fopen("in.txt","r");
    out=fopen("out.txt","w");
    if(in!=NULL&&out!=NULL)
    {
        fprintf(out,"1)I for insert anew record.\n");
        fprintf(out,"2)D for deleting arecord from the array.\n");
        fprintf(out,"3)F for the average of final exam.\n");
        fprintf(out,"4)C for the smallest and largest score of the final exam.\n");
        fprintf(out,"5)S for sort the array by descending order according to the first name .\n");
        fprintf(out,"6)Q to quit the program.\n");
    
        fprintf(out,"Enter an opration cod:");
        fscanf(in," %c",&ch);
    
        while(ch!='Q'&&ch!='q')
        {
            if(ch=='I'||ch=='i')
                insert_in_order(p,in,out);
            else if(ch=='D'||ch=='d')
            {
                delet_node(p,in,out);
                list_records(p,out);
            }
            else if(ch=='L'||ch=='l')
                list_records(p,out);
            else if(ch=='C'||ch=='c')
                fprintf(out,"The average of final exams=%f\n",calc_average(p));
            else if(ch=='F'||ch=='f')
            {
                r=find_record(p,in);
                list_records(r,out);
            }
            else
            fprintf(out,"wrong operation cod...");
    
            fprintf(out,"Enter an opration cod:");
            fscanf(in," %c",&ch);
        }
        fclose(in);
    }
    else
        printf("The file not exist");

    return 0;
}


User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 19 Jul, 2008 - 12:11 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 11,745



Thanked 17 times

Dream Kudos: 25
My Contributions


Can you specify what component is not working as desired?
User is offlineProfile CardPM

Go to the top of the page

Mi$s-Q8
post 19 Jul, 2008 - 12:36 PM
Post #3


New D.I.C Head

*
Joined: 30 Apr, 2008
Posts: 21


My Contributions



i think that there is a logic error in insertion function !
but i'm not sure about that ..
that's why i asked for help sad.gif !
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 8/30/08 03:13AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month