Thread: Help!!

  1. #1
    frankiepoon
    Guest

    Unhappy Help!!

    Hello all,I have designed a program that using the fileing to read the student data and then calculate their marks and find their average and highest marks.My programming skill is very bad,i hope some people can help me!!
    Thanks a lot!!
    /************************************************** */


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

    #define SIZE 20
    #define FILENAME "c:\\a4\\student.bin"
    #define FILE_MODE1 "r+b"
    struct record {
    int id;
    char name[20];
    int exerise;
    int exam;
    };
    typedef struct record database;
    void choice();
    void enter(database);
    void search(database);
    void erase(database);
    void cal_highest(database);
    void cal_mean(database);
    void update(database);

    main(void){


    choice();



    return 0;
    }
    void choice(){
    char input;
    int uselect;

    database student;





    printf("\n________________________________________ ___________________________________\n\n");
    printf(" This program is used to manage an existing binary data file\n\n");
    printf(" This Program is designed by Frankie software house\n");
    printf("__________________________________________ _________________________________\n\n");
    printf("1>Enter the student information\n\n");
    printf("2>Updating the record\n\n");
    printf("3>Search the student record and display it on the screen\n\n");
    printf("4>Erase the student record by entered the student id\n\n");
    printf("5>Calculate the highest total marks\n\n");
    printf("6>Calculate the mean of Examination marks\n\n");
    printf("7>Existed\n");

    printf("Please enter the number between(1-5)->");
    scanf("%c",&input);
    uselect=atoi(&input);
    switch(uselect){
    case 1:
    clrscr();
    enter(student);

    break;

    case 2:
    clrscr();
    update(student);

    break;

    case 3:
    clrscr();
    search(student);

    break;

    case 4:
    clrscr();
    erase(student);

    break;

    case 5:
    clrscr();
    cal_highest(student);

    break;

    case 6:
    cal_mean(student);
    break;

    case 7:
    printf("The program is ended!!\n");
    break;

    default:
    choice(student);
    break;
    }
    }
    void enter(database student){
    int count2;
    int found;
    database temp;
    FILE *fp;


    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error in opening file!");

    else{

    do{ printf("Enter the account number 1 to 20\n");
    printf("OR enter 0 to end the input\n");
    scanf("%d",&student.id);

    }while(student.id<0||student.id>20);
    for(count2=1;count2<=20;count2++){
    fseek(fp,sizeof(database)*(count2-1),SEEK_SET);
    fread(&temp,sizeof(database),1,fp);
    if(temp.id==student.id){
    found=0;
    printf("The record id is existed\n");

    break;
    }
    }


    while(found!=0){
    while(student.id!=0)
    {



    printf("Please enter the name->");
    scanf("%s",student.name);
    printf("\n");
    printf("Please enter the exerise marks->");
    scanf("%d",&student.exerise);
    printf("\n");
    printf("Please enter the exam marks->");
    scanf("%d",&student.exam);
    printf("\n");
    fseek(fp,sizeof(database)* (student.id-1),SEEK_SET);
    fwrite(&student,sizeof(database),1,fp);
    fseek(fp,sizeof(database)* (student.id-1),SEEK_SET);
    fwrite(&student,sizeof(database),1,fp);

    printf("\nEnter the student id number 1 to 20\n");
    printf("OR Enter 0 to come back the menu\n");
    scanf("%d",&student.id);
    if(student.id==0)
    {choice(student);
    found=1;
    }
    }
    fclose(fp);

    }
    }
    choice(student);

    return;
    }
    void search(database student){
    int id;
    int found=0;
    char i[3];
    int count;
    FILE *fp;
    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error in opening file!");
    else{
    do{
    printf("Please enter the id(1-20) to search the record");
    printf("\nOR Enter 0 to existed->");
    scanf("%s",i);
    id=atoi(i);

    printf("\n");
    }
    while(id>20||id<1);

    while(id!=0)
    {
    for(count=1;count<=20;count++){
    fseek(fp,sizeof(database)*(count-1),SEEK_SET);
    fread(&student,sizeof(database),1,fp);
    if(id==student.id){
    found=1;
    break;
    }
    else{
    found=0;

    }


    }
    if(found==1){
    printf("The record is found\n");
    printf("The id is %d\n",student.id);
    printf("The name is %s\n",student.name);
    printf("The Exam is %d\n",student.exerise);
    printf("The Exerise is %d\n",student.exam);
    }else{
    printf("The record id %d not found\n",id);

    }
    printf("Enter the number between 1-20 to search");
    printf("\nOR Enter 0 to existed->");
    scanf("%d",&id);

    if(id==0){
    choice();
    }


    }

    }

    return;
    }
    void update(database student){

    FILE *fp;

    char rubbish;
    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error in opening file!");

    else{

    printf("Enter the account number 1 to 20\n");
    printf("OR enter 0 to end the input\n");
    scanf("%d",&student.id);
    scanf("%c",&rubbish);




    while(student.id!=0)
    {



    printf("Please enter the name->");
    scanf("%s",student.name);
    printf("\n");
    printf("Please enter the exerise marks->");
    scanf("%d",&student.exerise);
    printf("\n");
    printf("Please enter the exam marks->");
    scanf("%d",&student.exam);
    printf("\n");
    fseek(fp,sizeof(database)* (student.id-1),SEEK_SET);
    fwrite(&student,sizeof(database),1,fp);

    printf("\nEnter the student id number 1 to 20\n");
    printf("OR Enter 0 to end\n");
    scanf("%d",&student.id);

    }
    fclose(fp);


    }
    return;
    }
    void erase(database student){

    FILE *fp;

    int id;
    char yn;
    student.name[20]=' ';
    student.exam=0;
    student.exerise=0;
    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error in opening file!");

    else{
    printf("Please enter the id to erase the record\n");
    printf("\nOR enter 0 to exist->");
    scanf("%d",&id);
    student.id=id;
    while(id!=0){
    fseek(fp,sizeof(database)*(id-1),SEEK_SET);
    fwrite(&student,sizeof(database),1,fp);
    printf("Please enter the id to erase the record\n");
    printf("\nOR enter 0 to exist->");
    scanf("%d",&id);
    }
    fclose(fp);
    }
    do{
    printf("Do you want to continue? Y or N\n");
    scanf("%c",&yn);
    }while((yn!='Y')&&(yn!='N')&&(yn!='y')&&(yn!='n')) ;
    if(yn=='y'||yn=='Y'){
    yn=0;
    choice(student);
    }
    else{
    printf("Bye Bye!!\n");
    }
    return;
    }
    void cal_mean(database student){
    FILE *fp;
    database store[20];
    int count=0;
    long total=0;
    char yn;
    int i=0;
    char rubbish;

    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error of opening file!\n");
    else{

    fread(store,sizeof(database),20,fp);

    while(count<=19){
    if (store[count].id==0){
    rubbish=store[count].exam;
    }
    else{
    i=i+1;
    total=total+(store[count].exam);

    }
    count=count+1;

    }
    fclose(fp);

    }

    printf("The average is %d\n",total/i);
    do{
    printf("Do you want to continue? Y or N\n");
    scanf("%c",&yn);
    }while((yn!='Y')&&(yn!='N')&&(yn!='y')&&(yn!='n')) ;
    if(yn=='y'||yn=='Y'){
    yn=0;
    choice(student);
    }
    else{
    printf("Bye Bye!!\n");
    }


    return;
    }
    void cal_highest(database student){
    FILE *fp;
    database store[20];
    float sum[20];
    char yn;
    char rubbish;
    int i;
    int studentid;

    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error of opening file!\n");
    else{

    fread(store,sizeof(database),20,fp);
    while(i<=19){
    if (store[i].id==0){
    rubbish=store[i].exam;
    }
    else{
    sum[i]=((store[i].exerise*0.3)+(store[i].exam*0.7));

    }
    i=i+1;
    }



    fclose(fp);

    }
    printf("What student total marks do you want to check?Please enter his/her id?\n");
    scanf("%d",&studentid);
    studentid=studentid-1;
    printf("%f",sum[studentid]);


    /*Ask the user to continue or not,but don't know why have display more a sentence */
    do{
    printf("Do you want to continue? Y or N\n");
    scanf("%c",&rubbish);
    scanf("%c",&yn);
    }while((yn!='Y')&&(yn!='N')&&(yn!='y')&&(yn!='n')) ;
    if(yn=='y'||yn=='Y'){
    yn=0;
    choice(student);
    }
    else{
    printf("Bye Bye!!\n");
    }


    return;
    }

  2. #2
    Frankiepoon
    Guest

    Re: Help!!

    Originally posted by frankiepoon
    Hello all,I have designed a program that using the fileing to read the student data and then calculate their marks and find their average and highest marks.My programming skill is very bad,i hope some people can help me!!
    The problem is i don't why the sentence"Do you want to continue??" is double.I mean it have display 2 times!!
    I don't know what is the reason,pls tell me why ??
    Thanks a lot!!
    /************************************************** */


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

    #define SIZE 20
    #define FILENAME "c:\\a4\\student.bin"
    #define FILE_MODE1 "r+b"
    struct record {
    int id;
    char name[20];
    int exerise;
    int exam;
    };
    typedef struct record database;
    void choice();
    void enter(database);
    void search(database);
    void erase(database);
    void cal_highest(database);
    void cal_mean(database);
    void update(database);

    main(void){


    choice();



    return 0;
    }
    void choice(){
    char input;
    int uselect;

    database student;





    printf("\n________________________________________ ___________________________________\n\n");
    printf(" This program is used to manage an existing binary data file\n\n");
    printf(" This Program is designed by Frankie software house\n");
    printf("__________________________________________ _________________________________\n\n");
    printf("1>Enter the student information\n\n");
    printf("2>Updating the record\n\n");
    printf("3>Search the student record and display it on the screen\n\n");
    printf("4>Erase the student record by entered the student id\n\n");
    printf("5>Calculate the highest total marks\n\n");
    printf("6>Calculate the mean of Examination marks\n\n");
    printf("7>Existed\n");

    printf("Please enter the number between(1-5)->");
    scanf("%c",&input);
    uselect=atoi(&input);
    switch(uselect){
    case 1:
    clrscr();
    enter(student);

    break;

    case 2:
    clrscr();
    update(student);

    break;

    case 3:
    clrscr();
    search(student);

    break;

    case 4:
    clrscr();
    erase(student);

    break;

    case 5:
    clrscr();
    cal_highest(student);

    break;

    case 6:
    cal_mean(student);
    break;

    case 7:
    printf("The program is ended!!\n");
    break;

    default:
    choice(student);
    break;
    }
    }
    void enter(database student){
    int count2;
    int found;
    database temp;
    FILE *fp;


    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error in opening file!");

    else{

    do{ printf("Enter the account number 1 to 20\n");
    printf("OR enter 0 to end the input\n");
    scanf("%d",&student.id);

    }while(student.id<0||student.id>20);
    for(count2=1;count2<=20;count2++){
    fseek(fp,sizeof(database)*(count2-1),SEEK_SET);
    fread(&temp,sizeof(database),1,fp);
    if(temp.id==student.id){
    found=0;
    printf("The record id is existed\n");

    break;
    }
    }


    while(found!=0){
    while(student.id!=0)
    {



    printf("Please enter the name->");
    scanf("%s",student.name);
    printf("\n");
    printf("Please enter the exerise marks->");
    scanf("%d",&student.exerise);
    printf("\n");
    printf("Please enter the exam marks->");
    scanf("%d",&student.exam);
    printf("\n");
    fseek(fp,sizeof(database)* (student.id-1),SEEK_SET);
    fwrite(&student,sizeof(database),1,fp);
    fseek(fp,sizeof(database)* (student.id-1),SEEK_SET);
    fwrite(&student,sizeof(database),1,fp);

    printf("\nEnter the student id number 1 to 20\n");
    printf("OR Enter 0 to come back the menu\n");
    scanf("%d",&student.id);
    if(student.id==0)
    {choice(student);
    found=1;
    }
    }
    fclose(fp);

    }
    }
    choice(student);

    return;
    }
    void search(database student){
    int id;
    int found=0;
    char i[3];
    int count;
    FILE *fp;
    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error in opening file!");
    else{
    do{
    printf("Please enter the id(1-20) to search the record");
    printf("\nOR Enter 0 to existed->");
    scanf("%s",i);
    id=atoi(i);

    printf("\n");
    }
    while(id>20||id<1);

    while(id!=0)
    {
    for(count=1;count<=20;count++){
    fseek(fp,sizeof(database)*(count-1),SEEK_SET);
    fread(&student,sizeof(database),1,fp);
    if(id==student.id){
    found=1;
    break;
    }
    else{
    found=0;

    }


    }
    if(found==1){
    printf("The record is found\n");
    printf("The id is %d\n",student.id);
    printf("The name is %s\n",student.name);
    printf("The Exam is %d\n",student.exerise);
    printf("The Exerise is %d\n",student.exam);
    }else{
    printf("The record id %d not found\n",id);

    }
    printf("Enter the number between 1-20 to search");
    printf("\nOR Enter 0 to existed->");
    scanf("%d",&id);

    if(id==0){
    choice();
    }


    }

    }

    return;
    }
    void update(database student){

    FILE *fp;

    char rubbish;
    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error in opening file!");

    else{

    printf("Enter the account number 1 to 20\n");
    printf("OR enter 0 to end the input\n");
    scanf("%d",&student.id);
    scanf("%c",&rubbish);




    while(student.id!=0)
    {



    printf("Please enter the name->");
    scanf("%s",student.name);
    printf("\n");
    printf("Please enter the exerise marks->");
    scanf("%d",&student.exerise);
    printf("\n");
    printf("Please enter the exam marks->");
    scanf("%d",&student.exam);
    printf("\n");
    fseek(fp,sizeof(database)* (student.id-1),SEEK_SET);
    fwrite(&student,sizeof(database),1,fp);

    printf("\nEnter the student id number 1 to 20\n");
    printf("OR Enter 0 to end\n");
    scanf("%d",&student.id);

    }
    fclose(fp);


    }
    return;
    }
    void erase(database student){

    FILE *fp;

    int id;
    char yn;
    student.name[20]=' ';
    student.exam=0;
    student.exerise=0;
    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error in opening file!");

    else{
    printf("Please enter the id to erase the record\n");
    printf("\nOR enter 0 to exist->");
    scanf("%d",&id);
    student.id=id;
    while(id!=0){
    fseek(fp,sizeof(database)*(id-1),SEEK_SET);
    fwrite(&student,sizeof(database),1,fp);
    printf("Please enter the id to erase the record\n");
    printf("\nOR enter 0 to exist->");
    scanf("%d",&id);
    }
    fclose(fp);
    }
    do{
    printf("Do you want to continue? Y or N\n");
    scanf("%c",&yn);
    }while((yn!='Y')&&(yn!='N')&&(yn!='y')&&(yn!='n')) ;
    if(yn=='y'||yn=='Y'){
    yn=0;
    choice(student);
    }
    else{
    printf("Bye Bye!!\n");
    }
    return;
    }
    void cal_mean(database student){
    FILE *fp;
    database store[20];
    int count=0;
    long total=0;
    char yn;
    int i=0;
    char rubbish;

    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error of opening file!\n");
    else{

    fread(store,sizeof(database),20,fp);

    while(count<=19){
    if (store[count].id==0){
    rubbish=store[count].exam;
    }
    else{
    i=i+1;
    total=total+(store[count].exam);

    }
    count=count+1;

    }
    fclose(fp);

    }

    printf("The average is %d\n",total/i);
    do{
    printf("Do you want to continue? Y or N\n");
    scanf("%c",&yn);
    }while((yn!='Y')&&(yn!='N')&&(yn!='y')&&(yn!='n')) ;
    if(yn=='y'||yn=='Y'){
    yn=0;
    choice(student);
    }
    else{
    printf("Bye Bye!!\n");
    }


    return;
    }
    void cal_highest(database student){
    FILE *fp;
    database store[20];
    float sum[20];
    char yn;
    char rubbish;
    int i;
    int studentid;

    if((fp=fopen(FILENAME,FILE_MODE1))==NULL)
    puts("Error of opening file!\n");
    else{

    fread(store,sizeof(database),20,fp);
    while(i<=19){
    if (store[i].id==0){
    rubbish=store[i].exam;
    }
    else{
    sum[i]=((store[i].exerise*0.3)+(store[i].exam*0.7));

    }
    i=i+1;
    }



    fclose(fp);

    }
    printf("What student total marks do you want to check?Please enter his/her id?\n");
    scanf("%d",&studentid);
    studentid=studentid-1;
    printf("%f",sum[studentid]);


    /*Ask the user to continue or not,but don't know why have display more a sentence */
    do{
    printf("Do you want to continue? Y or N\n");
    scanf("%c",&rubbish);
    scanf("%c",&yn);
    }while((yn!='Y')&&(yn!='N')&&(yn!='y')&&(yn!='n')) ;
    if(yn=='y'||yn=='Y'){
    yn=0;
    choice(student);
    }
    else{
    printf("Bye Bye!!\n");
    }


    return;
    }

Popular pages Recent additions subscribe to a feed