Thread: The problem read and write in files

  1. #1
    Unregistered
    Guest

    Unhappy The problem read and write in files

    Hi,everybody!
    I definetely don't know what my program wants from me!
    I'll appreciate if somebody helps we to fine what is wrong.
    Thank you very much!

    This program reads in two file,one of them has masters records(customid,name,balance);another file has transactions for each customer with payment("P") or new order("O") records.
    The program is reading in two structures,calculate Balance Due and prints the invoice for each customer.

    The compile errors :
    87: passing `char' to argument 1 of `findtotal(char *, int)' lacks a cast
    102: parse error before `{'
    106: `transitem' undeclared (first use this function)
    `transitemqty' undeclared (first use this function)
    107: `return' with a value, in function returning void
    In function `void printinvoice(customer *, int, transaction *, int *)':
    127: ANSI C++ forbids comparison between pointer and integer


    THIS IS THE 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()
    {
    struct customer cust[20];
    struct transaction trans[60];
    //struct newcustomer newcust[100];
    int num,num1;

    // float findtotal(transaction.);
    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[20],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;
    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("custominvoice.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
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I'd like to help but it's hard to read, try this:

    [X]
    //Type your code here
    [/X]

    Replace the 'X' with 'CODE'

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >trans[count1].totalamt = findtotal(trans[count1].item[20], trans[count1].itemqty);
    This is causing this:
    >87: passing `char' to argument 1 of `findtotal(char *, int)' lacks a cast
    because the function is declared as this:
    >float findtotal(char transitem[20], int transitemqty)
    You need to pass the complete array like this:
    >trans[count1].totalamt = findtotal(trans[count1].item, trans[count1].itemqty);

    >102: parse error before `{'
    This normally means you have a bracket missing, indeed you have. If you lay you code out neatly (indentation), you'll see where it should go.

    >float total;
    >float total = 0.00;
    You are declaring this variable twice in as many lines. You need only do it once:
    >float total = 0.00;

    >if (trans[count1].typepmt == "O")
    To compare a char use single quotes: 'O'

    There's probably a few more, but that'll get you started.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FILES (read & write)
    By gavra in forum C Programming
    Replies: 24
    Last Post: 06-16-2008, 01:22 AM
  2. read() write() problem
    By spank in forum Linux Programming
    Replies: 5
    Last Post: 04-28-2007, 10:35 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. using threads to write & read from files
    By kishorepalle in forum C Programming
    Replies: 4
    Last Post: 10-19-2004, 05:19 PM
  5. how to read and write files in c++
    By kerick in forum C++ Programming
    Replies: 6
    Last Post: 03-29-2002, 09:41 PM