this is my program:
there are 2 problems :
1. i am not allowed to use atoi
2. there is a compilation error

this is my code :

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

typedef struct companyStatus{
    double balance;
    FILE *invF, *errF;}companyStatus;


void printWord(FILE* file);
FILE* openFile(char* name);
char* getNextWord(FILE *file);
char getNextChar(FILE *file);
char* convert(char* str ,char* arr);

char* getNextWord(FILE *file){
    char word[41];
    char c;
    int i;
    i=0;
    word[0]='\0';
    c = fgetc(file);
    while (c != ' ' && c!=EOF && c!='\n')
    {
        word[i]=c;
        i++;
        c = fgetc(file);
    }
    word[i]='\0';
    return word;
}

FILE* openFile(char* fileName,char *mode){
    FILE *file;
    file = fopen(fileName,mode);
    return file;
}

char* convert(char* str ,char* arr){
    int length;
    int i;
    length = strlen(str);
    for (i=0; i< length; i++){
        arr[i] = str[i];
    }
    arr[length]='\0';
    return arr;
}

int countLines(char* fileName){
    FILE *file;
    char c;
    int lines;

    file = openFile(fileName,"r");

    lines = 0;
    if (file != NULL){
        c = fgetc(file);
        while (c != EOF){
            if (c == '\n')
                lines++;
            c = fgetc(file);
        }
        fclose(file);
    }

    return lines;
}

int equals(char* a, char* b){
    int i,n;
    if (strlen(a) != strlen(b)) return 0;
    n = strlen(a);
    for (i=0; i<n; i++){
        if (a[i]!=b[i]) return 0;
    }
    return 1;
}

int initDataBase(char* invFileName, char* errFileName,double bal,companyStatus* currStatus){
    int rows;
    FILE* file;

    currStatus->balance=bal;
    currStatus->invF=fopen(invFileName,"r");

    fclose((FILE*)currStatus->errF);
    currStatus->errF=fopen(errFileName,"a");
    if (currStatus->errF == NULL || currStatus->invF==NULL){
        printf("Could not open file \n");
        return 0;
    }
    return 1;
}

void saveToFile (char* name,char* code, long amount,FILE *file){
    int i;
    long myNumber;
    char longString[10];

    for (i=0; i<strlen(name); i++)//write name to file
        fputc(name[i],file);
    fputc(' ',file);

    for (i=0; i<strlen(code); i++)//write code to file
        fputc(code[i],file);
    fputc(' ',file);

    ltoa(amount, longString, 10);
    for (i=0; i<strlen(longString); i++)
        fputc(longString[i],file);
    fputc('\n',file);

    fclose(file);
}


void writeError(char* code,char* name,long amount,FILE *file){
    int i;
    long myNumber;
    char longString[10];

    for (i=0; i<strlen(code); i++){
        fputc(code[i],file);}
    fputc(' ',file);

    for (i=0; i<strlen(name); i++)
        fputc(name[i],file);
    fputc(' ',file);

    ltoa(amount, longString, 10);
    for (i=0; i<strlen(longString); i++)
        fputc(longString[i],file);
    fputc('\n',file);
    fclose(file);
}


void saveErrorFile(FILE *newErrFile, FILE *currErr){
    int c;
    if (newErrFile == NULL || currErr==NULL) return;
    c = fgetc(currErr);
    while (c != EOF){
        fputc(c,newErrFile);
        c = fgetc(currErr);
    }
    fclose(newErrFile);
    fclose(currErr);
}

void saveFileToFile(FILE *from,FILE *to){
    int c;
    if (from == NULL || to==NULL) return;
    c = fgetc(from);
    while (c != EOF){
        fputc(c,to);
        c = fgetc(from);
    }
    fclose(from);
    fclose(to);
}       

int saveInventory(char *invFileName,char *errFileName, companyStatus *currStatus){

    FILE *invFile;
    FILE *newErrFile;
    invFile = openFile(invFileName,"w");
    newErrFile = openFile(errFileName,"a");

    if (invFile==NULL || newErrFile==NULL)  return 0;

    saveFileToFile(currStatus->invF,invFile);
    saveFileToFile(currStatus->errF,newErrFile);

    return 1;
}

/*========================================================================================================*/
void makeSale(char* saleFileName, char* invFileName, companyStatus *currStatus,char* currFileName){
    int salesRows;
    int invRows;
    FILE *invFile,*file, *newInvFile;
    int i,j;
    char salesManName[101]; 
    char code[21];
    char* quantity[9];
    char* cost[9];
    long quant;
    long saleCost;

    char* name[41];
    char* invCode[21];
    char* invAmount[9];
    long amount;

    code[0]='\0';
    quantity[0]='\0';
    cost[0]='\0';
    name[0]='\0';
    invCode[0]='\0';
    invAmount[0]='\0';

    salesRows = countLines(saleFileName);
    invRows = countLines(currFileName);

    file = openFile(saleFileName,"r");//open file for reading
    invFile =fopen(currFileName,"r");
    rename(currFileName,"inv1.tmp");

    newInvFile = fopen(currFileName,"a");
    if (newInvFile == NULL){
        printf("Failed to open temp file for reading newInvFile\n");
        return;
    }

    if (invFile == NULL){
        printf("Failed to open temp file for reading inv.tmp\n");
        return;
    }
    if (file == NULL){
    printf("Failed to open sale file for reading s\n",saleFileName);
        return;
    }
    printf("Please enter name of sales representative(no spaces): ");
    scanf("%s",&salesManName);
    for (i=0; i<salesRows; i++){
        code[0]='\0';
        quantity[0]='\0';
        cost[0]='\0';

        strcat(code,getNextWord(file));
        strcat(quantity,getNextWord(file));
        strcat(cost,getNextWord(file));


        for (j=0; j<invRows; j++){
            name[0]='\0';
            invCode[0]='\0';
            invAmount[0]='\0';

            strcat(name,getNextWord(invFile));
            strcat(invCode,getNextWord(invFile));
            strcat(invAmount,getNextWord(invFile));

            if (equals(invCode,code)==1){//found one on the list
                quant=atoi(quantity);
                amount=atoi(invAmount);
                if (invAmount > quant){//we can sell
                    amount-=quant;
                    saleCost=atoi(cost);
                    currStatus->balance+=saleCost;
                    saveToFile(name,code,invAmount,currStatus->invF);//save to inv
                }
                if (invAmount==quant){//no need to write to inv
                    saleCost=atoi(cost);
                    currStatus->balance+=saleCost;
                }
                else{
                    quant=atoi(quantity);
                    writeError(code,salesManName,quant,currStatus->errF);
                }//else
            }//if

        }//for j
    }//for i    
    remove("inv.tmp");
    fclose(file);
//  fclose(invFile);
    }//func
/*========================================================================================================*/

/*========================================================================================================*/
void makePurchase(char* buyFileName,char* invFileName,companyStatus *currStatus,char* currFileName){

    int buyRows,invRows;
    FILE* invFile,*file ,*newInvFile;
    int i,j;
    char name[41];//name in buy file
    char code[21];
    char quantity[10];
    char cost[10];
    long quant;
    long buyCost;
    //inv fields
    char invName[41];
    char invCode[21];
    char invAmount[10];
    long amount;



    buyRows = countLines(buyFileName);
    invRows = countLines(currFileName);

    file = openFile(buyFileName,"r");
    invFile =fopen(currFileName,"r");
    rename(currFileName,"inv2.tmp");

    newInvFile = fopen(currFileName,"a");

    if (file == NULL){
        printf("Failed to open purchase file for reading %s\n",buyFileName);
        return;
    }

    for (i=0; i<buyRows; i++){

            name[0]='\0';//name in buy file
            code[0]='\0';
            quantity[0]='\0';
            cost[0]='\0';

            strcat(name,getNextWord(file));
            strcat(code,getNextWord(file));
            strcat(quantity,getNextWord(file));
            strcat(cost,getNextWord(file));

            for (j=0; j<invRows; j++){

                invName[0]='\0';
                invCode[0]='\0';
                invAmount[0]='\0';

                strcat(invName,getNextWord(file));
                strcat(invCode,getNextWord(file));
                strcat(invAmount,getNextWord(file));            

                if (equals(invCode,code)==1){//item exist, need to update
                    quant=atoi(quantity);
                    amount=atoi(invAmount);
                    amount+=quant;//the new amount;
                    buyCost=atoi(cost);

                    currStatus->balance-=buyCost;//update balance
                    saveToFile(invName,invCode,amount,currStatus->invF);
                }
                else{//new item in inv
                    amount=atoi(invAmount);
                    buyCost=atoi(cost);
                    currStatus->balance-=buyCost;//update balance
                    saveToFile(invName,invCode,amount,currStatus->invF);
                }
            }//for j
    }//for i
    remove("inv.tmp");
    fclose(file);
}//func
/*========================================================================================================*/


char* initDB(int* init,companyStatus *currStatus){
    char inv[101];
    char *invName="";
    char err[101];
    double bal;
    int res;
    printf("Please enter inventory file name: ");
    scanf("%s",&inv);
    printf("Please enter balance value: ");
    scanf("%lf",&bal);
    printf("Please enter error file name: ");
    scanf("%s",&err);
    res = initDataBase(inv,err,bal,currStatus);
    if (res == 0) {
        printf("Error in initializing database. Database is not initialized.\n");
        *init=0;
        currStatus->balance=0.0;
        invName=inv;
        return invName;
    }
    *init = 1;
    invName=inv;
    return invName;
}

void save(companyStatus *currStatus){
    char save[101];
    char err[101];
    printf("Please enter name of file for saving current inventory status: ");
    scanf(" %s",&save);
    printf("Please enter name of file for saving error status:");
    scanf("%s",&save);
    saveInventory(save,err,currStatus);
}

void sell(companyStatus *currStatus,char *invFileName){
    char sale[101];
    char* temp;
    printf("Please enter name of sale file: ");
    scanf(" %s",&sale);
    temp = "inv1.tmp";
    makeSale(sale,temp,currStatus,invFileName);
}

void buy(companyStatus *currStatus,char* invFileName){
    char buy[101];
    char* temp;
    printf("Please enter name of purchase file: ");
    scanf(" %s",&buy);
    temp = "inv2.tmp";
    makePurchase(buy,temp,currStatus,invFileName);
}

void go(companyStatus *status){
    char ch;
    char* invFileName;
    int choice=-1;
    int* init;
    invFileName=(char*)malloc(sizeof(char));
    invFileName[0]='\0';
    init = (int*)malloc(sizeof(int));
    *init=0;
    while (choice!=0){
        printf("Welcome to Chem-R-Us LTD database. Current company balance is %f.\nPlease select an option from ",status->balance);
        printf("the following menu\nPlease enter choice.\n0) Quit.\n1) Initialize company balance and inventory nad error report files.\n");
        printf("2) Save current Inventory and error status to files.\n");
        printf("3) Record a sale.\n4) Record a purchase.\n");
        scanf("%d",&choice);
        if (choice==1) {
            strcat(invFileName,initDB(init,status));
        }
        else if (choice==2){
            if (*init==0) printf("inventory not iniialized yet. Initialize before choosing any other option.\n");
            else save(status);}
        else if (choice==3){
            if (*init==0) printf("inventory not iniialized yet. Initialize before choosing any other option.\n");
            else sell(status,invFileName);}
        else if (choice==4){
            if (*init==0) printf("inventory not iniialized yet. Initialize before choosing any other option.\n");
            else buy(status,invFileName);}
     }
    return;
}


int main(){

    companyStatus *status = (companyStatus*)malloc(sizeof(companyStatus));

    status->balance=0.0;

    status->errF = (FILE*)malloc(sizeof(FILE));

    status->invF = (FILE*)malloc(sizeof(FILE));

    go(status);
    return 0;
}

Recommended Answers

All 2 Replies

link to the assignment :
http://www.cs.bgu.ac.il/~clang092/wiki.files/assignments/assignment6.pdf

Here's a link for you:
http://donate_to_my_bank_account.com.au/

Seriously mate, help us to help you. Rather than posting all that code and saying there is a compilation error, how about try telling us what the error is and then perhaps posting a snippet of the code that is giving you grief.

Also, when posting code, please use code tags!

Sheesh!

commented: Yes! +36
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.