C/C++:BEGINNER'S PRACTICAL ERROR-help please
Results 1 to 2 of 2

Thread: C/C++:BEGINNER'S PRACTICAL ERROR-help please

  1. #1
    Member
    Join Date
    Aug 2003
    Posts
    89

    Question C/C++:BEGINNER'S PRACTICAL ERROR-help please

    Anyone please help...

    I wrote a program(database) in C\C++(BLOODSHED DEV compiler), that stores PHONE NUMBERS, and at the end of the month
    when my moms phone bill arives, it takes the PHONE BILL and does a bunch of calculations
    like how many calls made to a number,most phoned number,etc......
    Its a 1000 line program, and i did all the programming , i am just a beginner and i need
    someone to "teach me the way of the dragon" on this one.



    HERE IS THE FAULTY PART OF THE SOURCE CODE(explanation below):
    class telcall{

    public:
    int coun;
    char name[30];
    char date[200][10];
    float time[200];
    float duration[200];
    float total_duration;
    char number[20];
    int distance;
    char callmore[4];
    float cost[200];
    int matched;
    int unknown;
    float semcost;
    float semduration;
    int semcalls;
    int ref;
    } ;

    void addata(){
    fflush(stdin);
    class telcall call[1000];
    class proginf append;
    class telcall writall[1000];
    char nume[20];
    int i = 1,t,ps,te=0;
    i = 1,t=0,ps=0,te=0;
    char useless[10],n[10];
    n[1] = 'n';
    FILE *inp,*inp2,*progin,*adde;
    int pass = 111,usele=1,maxx;
    char pas[10];
    clrscr();
    progin=fopen("proginf.dat","r");
    fread(&append,sizeof(append),1,progin);
    fclose(progin);

    printf("\n enter password: ");
    gets(pas);
    if (atoi(pas) != pass){
    printf("\nwrong password");
    exit(0);
    }
    usele=1;
    while (usele != 0 ){
    fflush(stdin);
    printf("enter NEW number\n");
    scanf("%s",call[i].number) ;
    fflush(stdin);
    printf("\n enter name \n");
    fflush(stdin);
    scanf("%s",call[i].name);
    */intialise all the numbers to 0 */
    call[i].date[0][0] = '0' ;
    call[i].time[0] = 00 ;
    call[i].duration[0] = 00 ;
    call[i].distance = 00 ;
    call[i].callmore[0] = '0' ;
    call[i].cost[0] = 00 ;
    call[i].matched = 0 ;
    call[i].unknown = 99 ;
    nume = call[i].number;
    t = excist(nume);
    if (t == 3){
    call[i].ref=0;
    inp = fopen("lebase.dat","w");
    fwrite(&call[i],sizeof(call[i]),1,inp);
    fclose(inp);


    }
    /*EXCISTING NUMBER */
    if (t == 1){
    printf("\n NUMBER ALREADY EXCISTS!!\n \n ");
    printf("\nNUMBER: %s \n \n \n \n ",call[i].number);
    } ;
    /*NEW NUMBER WAS ENTERED*/
    if (t == 2){
    te=0;
    fflush(stdin);
    inp2 = fopen("lebase.dat","r");
    fread(&writall,sizeof(writall),1,inp2);
    fclose(inp2);
    fflush(stdin);

    fflush(stdin);
    inp2 = fopen("lebase.dat","w") ;
    while((strlen(writall[te].number)) !=0){
    printf("\nwriting: %s",writall[te].number);
    fwrite(&writall[te],sizeof(writall[te]),1,inp2);
    te++;

    }
    fclose(inp2);
    fflush(stdin);
    call[i].ref=((writall[te-1].ref)+1);
    fflush(stdin);
    printf("REFERENCE: %d",call[i].ref);
    fflush(stdin);
    inp = fopen("lebase.dat","a+");
    fwrite(&call[i],sizeof(call[i]),1,inp);
    fclose(inp);

    /*ADD TO THE CURRENTMONTH*/
    adde=fopen(append.filename,"a+b");
    fwrite(&call[i],sizeof(call[i]),1,adde);
    fclose(adde);

    } ;
    i++ ;
    printf("enter again?(<1>y or <0>n)\n");
    cin >> usele;

    fflush(stdin);
    }
    }

    /*THIS FUCNTION TEST FOR DUPLICATIONS*/
    int excist(char nume[20]){

    class telcall calltest;
    FILE *inp;
    char dumb[5];
    int end_of_file = 0;
    if ((inp = fopen("lebase.dat","r")) == NULL){
    printf("error opening file1\n");

    return(3);
    }
    while (end_of_file == 0) {
    fread(&calltest,sizeof(calltest),1,inp);
    if ((feof(inp)) != 0){
    printf("\nFILE ADDED SUCCESFULLY \n");
    end_of_file = 1;
    }

    if ((strcmp(nume,calltest.number))== 0){

    return(1);

    }



    }

    fclose(inp);
    return(2);
    }



    Now, my prob is:

    I wrote a function("addata()") that INPUTS a NUMBER(from PHONE BOOK) to store in a file("lebase.dat")
    it also stores the number in another file with changes evey month(" adde=append.filename")
    It uses a WHILE-loop that continous to INPUT numbers from the user,until the user says
    STOP, after asking for INPUT(number and name) and still WITHIN the WHILE-loop, it
    calls another function("ecxist()") to check if the number doesnt already excist, then
    returns a flag,wich signals a WRITE TO FILE and DISPLAY NUMBERS and REFFERENCE
    , or DISPLAY ERROR MESSAGE.
    After that it ask the user if he wants to STOP or to enter another number,
    if the user says STOP the WHILE-loop stops, if the user says ANOTHER NUMBER then
    the WHILE-loop starts over.


    Now, for every NUMBER written to file("lebase.dat") gets a reffrence
    number(staring at 1 and just incrementing for every new number).

    So the 1st phone number you enter has a reffrence of 1,
    the 2nd number has a reffrence of 2.
    ..........
    the 20th number has a reffrence of 20.
    etc.

    Theoriotically this should have no problem what so ever, but when you run the program
    it works FINE until you enter 26 numbers(from PHONE book),
    the 27th phone number DOESN't have a reffrence of 27 but a "RANDOM" number like 4385,and
    also, it doesn't write to file correctly,as a consicounce of this my whole program ****s up
    and my whole FILE STRUCTURE("lebase.dat") gets ****ED.

    After analasys i found that when the user enter a duplication of a number that already
    excists, then the same thing happens(as above)...

    If anyone could help me, PLZ i beg you,pretty plz help.

    I will include the FULL SOURCE CODE and the PROGRAM(with its files) itself.

    The program is considerd to be in BETA phase(if you explore the prog you'll see why)
    Check out option 1,wich is the faulty part,and enter 27 numbers
    Oh, the password is 111 (its just to keep other people from adding "unauthorized" phone
    numbers).
    Beware, you who seek first and final principles, for you are
    trampling the garden of an angry God and he awaits you just beyond the last theorem.

  2. #2
    Member
    Join Date
    Aug 2003
    Posts
    89
    Is there none that can help thy???
    It might look like a long messed up code, but it's only a class defintion(for refrence) and 2 functions wich one is of interest and the other(addata()) is the problem.

    I thought AO might've been able to help.
    Any response will be wowed on and applaured
    Beware, you who seek first and final principles, for you are
    trampling the garden of an angry God and he awaits you just beyond the last theorem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •