Hi everyone!!

i have a problem here. i am writing a database program in C in which i used files and structures to store data. but my code is not running :/

here is my code::

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

    struct std_info
    {
        char name[15];
        char seatno[9];
        float ssc;
        float hsc;
        int ccy;
    };
    struct std_info si;
    static char *numstr[9];
    int n = 0;
    FILE *fp;

void menu();
void form();
void writeData();
void readNext(int pos);
void readPrev(int pos);
int key_chk();

int main()
{
    int key,key1;
    int nextPos =1;
    int PrevPos;
    system("cls");
    menu();
    gotoxy(20,6);

    // i am facing problem i this while loop nothing happend 
    //when i used switch it works for writing data to the file and showing next recode in the file but not previouse 
    while((key = key_chk()) != 24)
    {
        if(key == 1)
        {
            do
            {
                system("cls");
                menu();
                writeData();
                printf("\n\nAdd more data y/n? : ");
            }while(getchar() == 'y');
        }

        if(key == 3) // checks if user want to see data
        {
            key1 = key_chk();
            if(key1 == 77) //if user press right arrow key display next data in a file
            {
                system("cls");
                menu();
                readNext(nextPos);
                nextPos++;
                PrevPos = nextPos;
            }
            if(key1 == 75)//if user press left arrow key go back to previous data in a file
            {
                system("cls");
                menu();
                readPrev(PrevPos);
                PrevPos--;
            }
            nextPos = PrevPos;
        }

    }



getch();
return 0;
}
void menu() // menu
{
    static char *items[4] = {"ADD","UPDATE","VIEW","EXIT"};

    gotoxy(20,5);
    textcolor(0);
    textbackground(15);
    for(int i=0; i<4; i++)
    {
        printf(" %d:",i+1);
        cprintf(" %s ",*(items+i));
    }
}
void form() //form
{
    gotoxy(20,8);
    textcolor(15);
    textbackground(0);
    cprintf("Seat No.       : ");
    gotoxy(20,9);
    cprintf("Student's Name : ");
    gotoxy(20,10);
    cprintf("SSC Percentage : ");
    gotoxy(20,11);
    cprintf("HSC Percentage : ");
    gotoxy(20,12);
    cprintf("Course Cleared : ");
}
void writeData() writing data into the file
{
    if((fp = fopen("std_info.DAT","ab+")) == NULL)
    {
        gotoxy(20,30);
        cprintf("Can't open file");
        exit(1);
    }
    //system("cls");
    form();
    gotoxy(40,8);
    gets(si.name);
    gotoxy(40,9);
    gets(si.seatno);
    gotoxy(40,10);
    gets(*numstr);
    si.ssc = atof(*numstr);
    gotoxy(40,11);
    gets(*numstr);
    si.hsc = atof(*numstr);
    gotoxy(40,12);
    gets(*numstr);
    si.ccy = atoi(*numstr);
    fwrite(&si,sizeof(si),1,fp);
    fflush(stdin);
    fclose(fp);
}
void readNext(int pos) reading data
{
    if((fp=fopen("std_info.DAT","rb")) == NULL)
    {
        gotoxy(20,30);
        cprintf("Can't open file");
        exit(1);
    }
    long offset;
    offset = (pos-1) * sizeof(si);
    form();
    textcolor(15);
    textbackground(0);
    fseek(fp,offset,SEEK_SET);
    fread(&si,sizeof(si),1,fp);
    gotoxy(40,8);
    cprintf("%s",si.seatno);
    gotoxy(40,9);
    cprintf("%s",si.name);
    gotoxy(40,10);
    cprintf("%.1f",si.ssc);
    gotoxy(40,11);
    cprintf("%.1f",si.hsc);
    gotoxy(40,12);
    cprintf("%d",si.ccy);
    fflush(stdin);
    fclose(fp);
}
void readPrev(int pos) //reading data 
{
    if((fp=fopen("std_info.DAT","rb")) == NULL)
    {
        gotoxy(20,30);
        cprintf("Can't open file");
        exit(1);
    }
    long offset;
    offset = (pos-1) * sizeof(si);
    form();
    textcolor(15);
    textbackground(0);
    fseek(fp,offset-1,SEEK_CUR);
    fread(&si,sizeof(si),1,fp);
    gotoxy(40,8);
    cprintf("%s",si.seatno);
    gotoxy(40,9);
    cprintf("%s",si.name);
    gotoxy(40,10);
    cprintf("%.1f",si.ssc);
    gotoxy(40,11);
    cprintf("%.1f",si.hsc);
    gotoxy(40,12);
    cprintf("%d",si.ccy);
    fflush(stdin);
    fclose(fp);
}
int key_chk() // this the code for checking which key is pressed (extended or normal key)
{
    char ch,ch1;
    ch = getch();
    if(ch == 0)
    {
        ch1 = getch();
    }
    else{return ch;}
return ch1;
}

any help would be appreciated.

it did nothing when i press 1 to add record nothing happen and same goes to when pressing 3 for viewing data. i also want when i press 3 i can read record back and forth.

key_chk() returns the char representation of the key that was pressed. For example, if you press 1 then it will return '1', not 1, and that's why the loop doesn't work.

Line 38 should be: if(key == '1')

line 194: When a special key is pressed you have to call getch() a second time to get the key code. The problem is that the key code is the same as one of the other normal keys. The right arrow returns the key code of 'M'. You have to do something more with it if you intend to be able to distinguish between right arrow and 'M', for example make the value negative or add 255 to the int value. Right now, on line 52 you don't know whether the user pressed the right arrow or the 'M' key. But if you make the value negative on line 194 you could test for -77 on line 52 and your program would work correctly.

Edited 3 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.