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

Join 135,932 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,666 people online right now. Registration is fast and FREE... Join Now!




check me out for mistakes pls - C

 
Reply to this topicStart new topic

check me out for mistakes pls - C, program still not working well even though 0 errors

zattara13
17 May, 2008 - 08:19 AM
Post #1

New D.I.C Head
*

Joined: 27 Mar, 2008
Posts: 26

search fucntion not working
help me out pls

CODE

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


struct STUDENTS {
char name[50];
char IDnum[7];
char address[100];
int     mark1;
int  mark2;
char grade[2];
};

struct STUDENTS array1[41]={"","","",0,0,""};
struct STUDENTS student;


void insertstud ( FILE *ffPtr );
void edit ( FILE *ffPtr );
void results ( FILE *ffPtr );

int main()
{
    
    
    int choice;
    int i;

    FILE *cfPtr;

    if (( cfPtr = fopen( "E:\\Question 2.dat","wb" ))==NULL)  
    {
        printf("File could not be opened.\n");
    }
    else
    {
        for(i=0; i<41; i++)
        {
            fwrite(&array1[i], sizeof( struct STUDENTS ),1,cfPtr );
        }

        while (( choice = menu() )!=6 )  
        {

            switch (choice) {

                case 1:
                    insertstud( cfPtr );
                    break;

                case 2:
                    edit( cfPtr );
                    break;

                case 3:
                //    results( cfPtr );
                    break;

                case 4:
                    //R_tabForm( cfPtr );
                    break;

                case 5:
                //    sort_by_ID( cfPtr );
                    break;

                default:
                    printf(" Incorrect choice\n");
                    break;
            }
        }
        fclose( cfPtr );
    }

    return 0;
}

int menu (void)
{
    int choice;

    printf("1 - Enter new student\n");
    printf("2 - Edit student file\n");
    printf("3 - Generate final result\n");
    printf("4 - Generate another date file with results in tabulation form\n");
    printf("5 - Sort databse by ID number\n");
    printf("6 - Quit\n");

    printf("\n\nEnter your choice... ");
    scanf ("%d", &choice);

    return (choice);
}

void insertstud ( FILE *ffPtr )
{
    
    int pos = 0;
    int i    = 0;
    
    fseek( ffPtr,(pos - 1) * sizeof(struct STUDENTS ), SEEK_SET );
    fread( &student, sizeof( struct STUDENTS ),1, ffPtr );
    
    printf("\nEnter Name: ");
    scanf ("%s", student.name);
    printf("\nEnter ID: ");
    scanf ("%s", student.IDnum);
    printf("\nEnter Address: ");
    scanf ("%s", student.address);
    printf("\nEnter coursework mark: ");
    scanf ("%d", &student.mark1);
    printf("\nEnter exam mark: ");
    scanf ("%d", &student.mark2);

    fseek( ffPtr,(pos - 1) * sizeof(struct STUDENTS ), SEEK_SET );
    fread( &student, sizeof( struct STUDENTS ),1, ffPtr );
    i++;

}

int search(FILE *ffPtr, char key[7])
{
    struct STUDENTS tempStudent;
    int i = 1;

    while (tempStudent.IDnum  != key)
    {
      fseek( ffPtr, (i - 1)*sizeof(struct STUDENTS), SEEK_SET );
      fread (&tempStudent, 1, sizeof(struct STUDENTS) ,ffPtr);
      i++;
    }

    return (i);

}


void edit ( FILE *ffPtr )
{
   char ID[7];
   int pos = 0;


    printf("Enter ID number of student to edited\n");
    scanf("%s", &ID );


    pos = search (ffPtr, ID);

    fseek( ffPtr, (pos -1) * sizeof( struct STUDENTS ), SEEK_SET );

    fread( &student, sizeof( struct STUDENTS ), 1, ffPtr );

    if ( pos == -1 ) {
        printf("  the id card number %s does not exist", ID );

    }

    else {
        printf("%s%s%s%d%d%s",
            student.name, student.IDnum, student.address,
            student.mark1, student.mark2, student.grade );

        printf(" enter the new students id\n");
        scanf("%s", &student.IDnum );
        printf(" enter name of student, ID card, address, coursework mark, mark of final exam and overall grade\n");
        scanf("%s%s%s%d%d%s",
            &student.name, &student.IDnum, &student.address,
            &student.mark1, &student.mark2, &student.grade );

        
    fseek( ffPtr, (pos-1) * sizeof( struct STUDENTS ), SEEK_SET );

    fwrite ( &student, sizeof( struct STUDENTS ), 1, ffPtr );
    
    }
}


void results ( FILE *ffPtr )
{
    int i;
    int final_R;
    char ID[7];


    for (i=0;i<41;i++ ) {

    fseek( ffPtr, (i -1) * sizeof( struct STUDENTS ), SEEK_SET );

    fread( &student, sizeof( struct STUDENTS ), 1, ffPtr );

    

    
    final_R= student.mark1 + student.mark2;

    //IF mark > 98
//    student.grade ="A+";
        
    fseek( ffPtr, (i - 1) * sizeof( struct STUDENTS ), SEEK_SET );

    fwrite ( &student, sizeof( struct STUDENTS ), 1, ffPtr );
    
    }
}

void R_tabForm(FILE *readPtr)
{
FILE *writePtr;
int i =1;

if (( writePtr= fopen ("marilyn_chetcuti_Q2.txt", "w" )) ==NULL )  {
    printf("File could not be opened.\n");
}
else {
    fprintf ( writePtr,"%-50s\t%-8s\t%-12\t%-12\t%-6s\n",
        "Name","ID card","Mark C_Work","Mark F_Exam","Grade");
}
fseek( readPtr, 1, SEEK_SET );
while ( !feof( readPtr)) {
    
    
    fread ( &student, sizeof( struct STUDENTS ), 1, readPtr );


if ( student.IDnum!=0 ) {
    
        fprintf(writePtr, "%-50s\t%-8s\t%-12d\t%-12d\t%-6s\n",
            student.name, student.IDnum,
            student.mark1, student.mark2, student.grade );
}


}

fclose( writePtr );

}

void sort(FILE *readPtr)
{
    int i=0;
    struct STUDENTS HOLD;

    fseek( readPtr, 1, SEEK_SET );

    while ( !feof( readPtr)) {
    
    //load array
    fread ( &student, sizeof( struct STUDENTS ), 1, readPtr );


    array1[i] = student;
    i++;
    }

//bubble sort

    // strcmp(array1[i].IDnum,array1[i+1].IDnum)
//    HOLD = array1[i];

//bubblesort

     fseek( readPtr, 1, SEEK_SET );

        for( i =0;i<41;i++)
        {
            fwrite(&array1[i], sizeof( struct STUDENTS ),1,readPtr );
        }

}

User is offlineProfile CardPM
+Quote Post

godmoktail
RE: Check Me Out For Mistakes Pls - C
17 May, 2008 - 08:39 AM
Post #2

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 17


My Contributions
What's not working in the search function?
User is offlineProfile CardPM
+Quote Post

zattara13
RE: Check Me Out For Mistakes Pls - C
17 May, 2008 - 08:53 AM
Post #3

New D.I.C Head
*

Joined: 27 Mar, 2008
Posts: 26

QUOTE(zattara13 @ 17 May, 2008 - 09:19 AM) *

search fucntion not working
help me out pls

CODE

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


struct STUDENTS {
char name[50];
char IDnum[7];
char address[100];
int     mark1;
int  mark2;
char grade[2];
};

struct STUDENTS array1[41]={"","","",0,0,""};
struct STUDENTS student;


void insertstud ( FILE *ffPtr );
void edit ( FILE *ffPtr );
void results ( FILE *ffPtr );

int main()
{
    
    
    int choice;
    int i;

    FILE *cfPtr;

    if (( cfPtr = fopen( "E:\\Question 2.dat","wb" ))==NULL)  
    {
        printf("File could not be opened.\n");
    }
    else
    {
        for(i=0; i<41; i++)
        {
            fwrite(&array1[i], sizeof( struct STUDENTS ),1,cfPtr );
        }

        while (( choice = menu() )!=6 )  
        {

            switch (choice) {

                case 1:
                    insertstud( cfPtr );
                    break;

                case 2:
                    edit( cfPtr );
                    break;

                case 3:
                //    results( cfPtr );
                    break;

                case 4:
                    //R_tabForm( cfPtr );
                    break;

                case 5:
                //    sort_by_ID( cfPtr );
                    break;

                default:
                    printf(" Incorrect choice\n");
                    break;
            }
        }
        fclose( cfPtr );
    }

    return 0;
}

int menu (void)
{
    int choice;

    printf("1 - Enter new student\n");
    printf("2 - Edit student file\n");
    printf("3 - Generate final result\n");
    printf("4 - Generate another date file with results in tabulation form\n");
    printf("5 - Sort databse by ID number\n");
    printf("6 - Quit\n");

    printf("\n\nEnter your choice... ");
    scanf ("%d", &choice);

    return (choice);
}

void insertstud ( FILE *ffPtr )
{
    
    int pos = 0;
    int i    = 0;
    
    fseek( ffPtr,(pos - 1) * sizeof(struct STUDENTS ), SEEK_SET );
    fread( &student, sizeof( struct STUDENTS ),1, ffPtr );
    
    printf("\nEnter Name: ");
    scanf ("%s", student.name);
    printf("\nEnter ID: ");
    scanf ("%s", student.IDnum);
    printf("\nEnter Address: ");
    scanf ("%s", student.address);
    printf("\nEnter coursework mark: ");
    scanf ("%d", &student.mark1);
    printf("\nEnter exam mark: ");
    scanf ("%d", &student.mark2);

    fseek( ffPtr,(pos - 1) * sizeof(struct STUDENTS ), SEEK_SET );
    fread( &student, sizeof( struct STUDENTS ),1, ffPtr );
    i++;

}

int search(FILE *ffPtr, char key[7])
{
    struct STUDENTS tempStudent;
    int i = 1;

    while (tempStudent.IDnum  != key)
    {
      fseek( ffPtr, (i - 1)*sizeof(struct STUDENTS), SEEK_SET );
      fread (&tempStudent, 1, sizeof(struct STUDENTS) ,ffPtr);
      i++;
    }

    return (i);

}


void edit ( FILE *ffPtr )
{
   char ID[7];
   int pos = 0;


    printf("Enter ID number of student to edited\n");
    scanf("%s", &ID );


    pos = search (ffPtr, ID);

    fseek( ffPtr, (pos -1) * sizeof( struct STUDENTS ), SEEK_SET );

    fread( &student, sizeof( struct STUDENTS ), 1, ffPtr );

    if ( pos == -1 ) {
        printf("  the id card number %s does not exist", ID );

    }

    else {
        printf("%s%s%s%d%d%s",
            student.name, student.IDnum, student.address,
            student.mark1, student.mark2, student.grade );

        printf(" enter the new students id\n");
        scanf("%s", &student.IDnum );
        printf(" enter name of student, ID card, address, coursework mark, mark of final exam and overall grade\n");
        scanf("%s%s%s%d%d%s",
            &student.name, &student.IDnum, &student.address,
            &student.mark1, &student.mark2, &student.grade );

        
    fseek( ffPtr, (pos-1) * sizeof( struct STUDENTS ), SEEK_SET );

    fwrite ( &student, sizeof( struct STUDENTS ), 1, ffPtr );
    
    }
}


void results ( FILE *ffPtr )
{
    int i;
    int final_R;
    char ID[7];


    for (i=0;i<41;i++ ) {

    fseek( ffPtr, (i -1) * sizeof( struct STUDENTS ), SEEK_SET );

    fread( &student, sizeof( struct STUDENTS ), 1, ffPtr );

    

    
    final_R= student.mark1 + student.mark2;

    //IF mark > 98
//    student.grade ="A+";
        
    fseek( ffPtr, (i - 1) * sizeof( struct STUDENTS ), SEEK_SET );

    fwrite ( &student, sizeof( struct STUDENTS ), 1, ffPtr );
    
    }
}

void R_tabForm(FILE *readPtr)
{
FILE *writePtr;
int i =1;

if (( writePtr= fopen ("marilyn_chetcuti_Q2.txt", "w" )) ==NULL )  {
    printf("File could not be opened.\n");
}
else {
    fprintf ( writePtr,"%-50s\t%-8s\t%-12\t%-12\t%-6s\n",
        "Name","ID card","Mark C_Work","Mark F_Exam","Grade");
}
fseek( readPtr, 1, SEEK_SET );
while ( !feof( readPtr)) {
    
    
    fread ( &student, sizeof( struct STUDENTS ), 1, readPtr );


if ( student.IDnum!=0 ) {
    
        fprintf(writePtr, "%-50s\t%-8s\t%-12d\t%-12d\t%-6s\n",
            student.name, student.IDnum,
            student.mark1, student.mark2, student.grade );
}


}

fclose( writePtr );

}

void sort(FILE *readPtr)
{
    int i=0;
    struct STUDENTS HOLD;

    fseek( readPtr, 1, SEEK_SET );

    while ( !feof( readPtr)) {
    
    //load array
    fread ( &student, sizeof( struct STUDENTS ), 1, readPtr );


    array1[i] = student;
    i++;
    }

//bubble sort

    // strcmp(array1[i].IDnum,array1[i+1].IDnum)
//    HOLD = array1[i];

//bubblesort

     fseek( readPtr, 1, SEEK_SET );

        for( i =0;i<41;i++)
        {
            fwrite(&array1[i], sizeof( struct STUDENTS ),1,readPtr );
        }

}



the porgram will just freeze and u cannot enter anything. i also have a problem when inputing the data... i cannot write bob smith because with every word i enter, the program will start skipping the inputs eg:

enter name : bob smith

enter id : enter address : asdfsdfsd

10x
User is offlineProfile CardPM
+Quote Post

zattara13
RE: Check Me Out For Mistakes Pls - C
18 May, 2008 - 08:57 PM
Post #4

New D.I.C Head
*

Joined: 27 Mar, 2008
Posts: 26

any help pls??
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:24AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month