error in executable

This is a discussion on error in executable within the C Programming forums, part of the General Programming Boards category; Hi,people! In first,I want to thank a lot Hammer who hepl me wipe my errors in compilation.Now the application doesn't ...

  1. #1
    Unregistered
    Guest

    Question error in executable

    Hi,people!
    In first,I want to thank a lot Hammer who hepl me wipe my errors in compilation.Now the application doesn't have any compiling problems.
    But I got THE BIG ONE when I'm running my program.
    This is the error message:

    This program has performed an illegal operation and will be shut down.

    I'll appreciate any ideas and help!
    Thanks!

    [my code]:

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


    struct custname{
    char last[10];
    char first[10];
    };
    struct customer{
    int custid;
    struct custname name;
    float prevbalance;
    };
    struct transaction{
    char typepmt;
    int custid;
    int transid;
    char item[20];
    int itemqty;
    float totalamt;
    };
    /*struct newcustomer{
    int custid;
    struct custname name;
    float newbalance;
    };*/

    void readdata(struct customer *,int *,struct transaction *,int *);
    float findtotal(char [20] ,int ); /*we'll call it inside of readdata*/
    void printinvoice(struct customer *,int ,struct transaction *,int *);

    main()
    {
    int num,num1;
    struct customer cust[20];
    struct transaction trans[60];

    readdata(cust,&num,trans,&num1);
    printinvoice(cust,num,trans,&num1);

    }

    void readdata(struct customer *cust, int *num,struct transaction *trans,int *num1)
    {
    char *instring;
    char buffer[150],buffer1[300];
    FILE *master,*transact,*invoice;
    int count,count1;
    float transamt;


    master= fopen("master.txt","r");
    if (master == NULL){
    fprintf(stderr,"Error opening master input file\n");
    exit(1);
    }

    transact = fopen("transact.txt", "r");
    if (transact == NULL){
    fprintf(stderr,"Error opening input transaction file\n");
    exit(1);
    }

    count = 0;
    count1 = 0;

    while((instring=fgets( buffer,150,master))!=NULL){

    if(cust[count].custid==cust[count+1].custid){
    fprintf(stderr,"Customer ID is duplicate!\n");
    exit(1);
    }
    count++;

    *num= count;

    while(fscanf(transact,"%d",&trans[count1].transid) != EOF){
    fscanf(transact,"%s",trans[count1].typepmt);
    fscanf(transact,"%5f",&trans[count1].custid);
    if(&trans[count1].typepmt=="O"){

    fscanf(transact,"%s",trans[count1].item[20]);
    fscanf(transact,"%d",&trans[count1].itemqty);
    trans[count1].totalamt=findtotal(trans[count1].item,trans[count1].itemqty);
    /* fscanf(transact,"%$5.2f",&trans[count1].totalamt=findtotal(trans[count1].item,trans[count1].itemqty)); */
    }
    else{
    fscanf(transact,"%$5.2f",&trans[count1].totalamt);
    }
    count1++;
    }
    }
    *num1 = count1;

    fclose(master);
    fclose(transact);
    return ;
    }

    float findtotal(char transitem[20],int transitemqty)
    {
    float total=0.00;

    total= (float)transitem[20]*transitemqty;
    return total;
    }
    void printinvoice(struct customer *cust, int num,struct transaction *trans, int *num1)
    {
    FILE *invoice;
    int count,count1;
    float balancedue;

    invoice=fopen("cinvoice.txt","w");
    if (invoice==NULL){
    fprintf(stderr,"Error opening input transaction file\n");
    exit(1);
    }
    fprintf(invoice,"\t\tCustomer invoice\n\n");
    for(count = 0;count < num;count++){
    fprintf(invoice,"Customer Name \t\tCustomer Account Number");
    fprintf(invoice,"s% ",cust[count].name.last);
    fprintf(invoice,"s%\t ",cust[count].name.first);
    fprintf(invoice,"d%\t\n\n ",cust[count].custid);
    fprintf(invoice,"Previous Balance $8.2f%\t \n\n\n",cust[count].prevbalance);
    if(&trans[count1].typepmt=="O"){
    fprintf(invoice,"%d\t",trans[count1].transid);
    fprintf(invoice,"%s\t",trans[count1].item[20]);
    fprintf(invoice,"%$5.2f\t\n\n",trans[count1].totalamt);
    balancedue= cust[count].prevbalance + trans[count1].totalamt;
    fprintf(invoice,"Balance Due $8.2f%\t \n\n",balancedue);
    }
    else{
    fprintf(invoice,"payment\t");
    fprintf(invoice,"%$5.2f\t",trans[count1].totalamt);

    balancedue= cust[count].prevbalance - trans[count1].totalamt;
    fprintf(invoice,"Balance Due $8.2f%\t \n\n",balancedue);
    }
    count1++;
    }
    *num1 = count1;
    }

  2. #2
    CAP
    CAP is offline
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    I can't help with the code but this is for the people that can...it is a clean forum version thats all.

    Code:
    #include <stdio.h> 
    #include <string.h> 
    #include <stdlib.h> 
    
    
    struct custname{ 
    char last[10]; 
    char first[10]; 
    }; 
    struct customer{ 
    int custid; 
    struct custname name; 
    float prevbalance; 
    }; 
    struct transaction{ 
    char typepmt; 
    int custid; 
    int transid; 
    char item[20]; 
    int itemqty; 
    float totalamt; 
    }; 
    /*struct newcustomer{ 
    int custid; 
    struct custname name; 
    float newbalance; 
    };*/ 
    
    void readdata(struct customer *,int *,struct transaction *,int *); 
    float findtotal(char [20] ,int ); /*we'll call it inside of readdata*/ 
    void printinvoice(struct customer *,int ,struct transaction *,int *); 
    
    main() 
    { 
    int num,num1; 
    struct customer cust[20]; 
    struct transaction trans[60]; 
    
    readdata(cust,&num,trans,&num1); 
    printinvoice(cust,num,trans,&num1); 
    
    } 
    
    void readdata(struct customer *cust, int *num,struct transaction *trans,int *num1) 
    { 
    char *instring; 
    char buffer[150],buffer1[300]; 
    FILE *master,*transact,*invoice; 
    int count,count1; 
    float transamt; 
    
    
    master= fopen("master.txt","r"); 
    if (master == NULL){ 
    fprintf(stderr,"Error opening master input file\n"); 
    exit(1); 
    } 
    
    transact = fopen("transact.txt", "r"); 
    if (transact == NULL){ 
    fprintf(stderr,"Error opening input transaction file\n"); 
    exit(1); 
    } 
    
    count = 0; 
    count1 = 0; 
    
    while((instring=fgets( buffer,150,master))!=NULL){ 
    
    if(cust[count].custid==cust[count+1].custid){ 
    fprintf(stderr,"Customer ID is duplicate!\n"); 
    exit(1); 
    } 
    count++; 
    
    *num= count; 
    
    while(fscanf(transact,"%d",&trans[count1].transid) != EOF){ 
    fscanf(transact,"%s",trans[count1].typepmt); 
    fscanf(transact,"%5f",&trans[count1].custid); 
    if(&trans[count1].typepmt=="O"){ 
    
    fscanf(transact,"%s",trans[count1].item[20]); 
    fscanf(transact,"%d",&trans[count1].itemqty); 
    trans[count1].totalamt=findtotal(trans[count1].item,trans[count1].itemqty); 
    /* fscanf(transact,"%$5.2f",&trans[count1].totalamt=findtotal(trans[count1].item,trans[count1].itemqty)); */ 
    } 
    else{ 
    fscanf(transact,"%$5.2f",&trans[count1].totalamt); 
    } 
    count1++; 
    } 
    } 
    *num1 = count1; 
    
    fclose(master); 
    fclose(transact); 
    return ; 
    } 
    
    float findtotal(char transitem[20],int transitemqty) 
    { 
    float total=0.00; 
    
    total= (float)transitem[20]*transitemqty; 
    return total; 
    } 
    void printinvoice(struct customer *cust, int num,struct transaction *trans, int *num1) 
    { 
    FILE *invoice; 
    int count,count1; 
    float balancedue; 
    
    invoice=fopen("cinvoice.txt","w"); 
    if (invoice==NULL){ 
    fprintf(stderr,"Error opening input transaction file\n"); 
    exit(1); 
    } 
    fprintf(invoice,"\t\tCustomer invoice\n\n"); 
    for(count = 0;count < num;count++){ 
    fprintf(invoice,"Customer Name \t\tCustomer Account Number"); 
    fprintf(invoice,"s% ",cust[count].name.last); 
    fprintf(invoice,"s%\t ",cust[count].name.first); 
    fprintf(invoice,"d%\t\n\n ",cust[count].custid); 
    fprintf(invoice,"Previous Balance $8.2f%\t \n\n\n",cust[count].prevbalance); 
    if(&trans[count1].typepmt=="O"){ 
    fprintf(invoice,"%d\t",trans[count1].transid); 
    fprintf(invoice,"%s\t",trans[count1].item[20]); 
    fprintf(invoice,"%$5.2f\t\n\n",trans[count1].totalamt); 
    balancedue= cust[count].prevbalance + trans[count1].totalamt; 
    fprintf(invoice,"Balance Due $8.2f%\t \n\n",balancedue); 
    } 
    else{ 
    fprintf(invoice,"payment\t"); 
    fprintf(invoice,"%$5.2f\t",trans[count1].totalamt); 
    
    balancedue= cust[count].prevbalance - trans[count1].totalamt; 
    fprintf(invoice,"Balance Due $8.2f%\t \n\n",balancedue); 
    } 
    count1++; 
    } 
    *num1 = count1; 
    }
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Go and double check your types in your scanf's and printf's, because looking at the code after I compiled it (gcc -Wall), have got incorrect formats. something like this is what you have done
    Code:
    int a=4;
    printf("%f",a);
    Also there where a few format specifiers back to front

    you had "c%" instead of "%c"

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    I was bored so I fixed them, can you please Send me the data files so I can test it.

    Here is the file, with corrections.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-10-2009, 01:57 AM
  2. Loading a DLL that is not in the same directory as the executable
    By starcatcher in forum Windows Programming
    Replies: 10
    Last Post: 12-13-2008, 07:05 AM
  3. CreateProcess with Resource of executable, not the Filename
    By Ktulu in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2006, 01:07 AM
  4. calling an executable from an executable
    By dee in forum C Programming
    Replies: 4
    Last Post: 01-10-2004, 01:32 PM
  5. Altering The Program's Executable?
    By Aidman in forum C++ Programming
    Replies: 7
    Last Post: 12-31-2002, 05:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21