Solved

Function Code Need.

Posted on 2006-11-30
3
264 Views
Last Modified: 2010-04-15
Problem Describtion:  suppose you own an orange juice distributorship that sells Columbia Gorge(ID number 1), Florida Natural(ID number 2), Tropicana(Id number 3), Minute Maid(ID number 4), and Odwalla(ID number 5)

Write a menu driven program that manages a collection of items the Menu commands will be:
A....Add a new Entry
D...Delete an Entry
E...Edit an Entry
P....Display the Inventory
S...Save Current Inventory to a file.
C...Clear the entire inventory
L....Load sales records from a file
Q...Quit

MOST OF THE CODE IS PROVIDED I NEED THE CODE FOR THE FUNCTIONS


//finished.c implementation of user defined structure bev_t
//to run a menu to add a bev-t, delete a bev_t, display the list, clear list
//save list to file, load list from file, edit one item by selecting the field

//*******************************
//this program assumes all incorrect input from the user
//*******************************



#include<stdio.h> //stndard I/O
#include<string.h> //c style string functions
#include <ctype.h> //toupper/ tolower character functions
#define SIZE 30
#define MAX 100


//user defined struct bev_t
typedef struct{
        char brand[SIZE];
        char company[SIZE];
        int inStock;
        char lastShipDate[SIZE];
        double cost;
        double price;
} orange_t;


//function prototypes

//runs the menu returns char selection
char menu( );

//add an entry to the array of bev_t increase size by one
void AddItem(orange_t or[], int *size);

//delete an entry move last item in list to location of entry
//decrease size by one
void DeleteItem(orange_t or[], int *size, int location);

//edit an entry in the bev_t array
void EditItem(orange_t or[], int location);

//select which field to edit in an entry
//returns the selection
int EditMenu( );

//display the entire list
void DisplayInventory(orange_t or[], int size);

//save the list to a file
void SaveInventory(orange_t or[], int size);

//clear the list - set size to 0
void ClearInventory(orange_t or[], int *size);

//load the list of bev-t entries from a file
void LoadSalesRecords(orange_t or[], int *size);

//find an item in the list
//returns the array subscript location of the item
int FindItem(orange_t or[], int size);

//display one bev_t entry in the list
void DisplayItem(orange_t or[], int location);

//hard code five entries into the list
void HardCodeEntries(orange_t entry[], int *size);

//convert a word to all upper case characters
char* BrandUpperCase(char word[]);


int main( )
{
        orange_t brand[MAX];

        char trash;
        char selection;

        int size =0;
        int location;

        HardCodeEntries(brand, &size);

        selection =menu( );
        while((selection!='Q')||(selection!='q'))
        {
                if ((selection == 'Q')||(selection =='q'))
                {
                        printf("\nGOODBYE in while\n");
                        return 0;
                }
                if ((selection == 'A')||(selection =='a'))
                {
                        printf("\nADD ENTRY\n");
                        AddItem(brand, &size);
                }
                else if ((selection == 'D')||(selection =='d'))
                {
                        printf("\nDELETE ENTRY\n");
                        location = FindItem(brand,size);
                        if(location == -1)
                        {
                                printf("Item not found, Nothing will be deleted");
                        }
                        else
                        {
                               
                                printf("\nLOCATION: %d\n",location);
                                DeleteItem(brand, &size, location);
                        }
                       
                }
                else if ((selection == 'E')||(selection =='e'))
                {
                        printf("\nEDIT\n");

                        location = FindItem(brand,size);
                        if(location == -1)
                        {
                                printf("Item not found, Nothing will be edited");
                        }
                        else
                        {
                               
                                printf("\nLOCATION: %d\n",location);
                                EditItem(brand, location);
                        }

               
                }
                else if ((selection == 'P')||(selection =='p'))
                {
                        printf("\nDISPLAY INVENTORY\n");
                        DisplayInventory(brand, size);
                }
                else if ((selection == 'S')||(selection =='s'))
                {
                        printf("\nSAVE\n");
                        SaveInventory(brand,size);
                }
                else if ((selection == 'C')||(selection =='c'))
                {
                        printf("\nCLEAR\n");
                        ClearInventory(brand,&size);
                }
                else if ((selection == 'L')||(selection =='l'))
                {
                        printf("\nLOAD\n");
                        LoadSalesRecords(brand, &size);
                }
                else
                {
                        printf("\nEntry not recognized\n");
                }
                scanf("%c",&trash);
                selection = menu( );
        }

        printf("\nGOODBYE\n");
        return 0;
}

void DisplayInventory(orange_t or[], int size)
{
        int i=0;

        for(i=0;i<size;i++)
        {
                printf("\n");
                printf("brand:\t\t%s\ncompany:\t%s\nin stock:\t%d\n",
or[i].brand,or[i].company,or[i].inStock);
                printf("ship
date:\t%s\ncost:\t\t%.2f\nprice:\t\t%.2f\n\n",or[i].lastShipDate,or[i].cost,or[i].price);
        }
}


char menu( )
{
        char choice;

        printf("Enter selection: \t(A) Add an entry\n");
        printf("\t\t\t(D) Delete an entry\n");
        printf("\t\t\t(E) Edit an entry\n");
        printf("\t\t\t(P) Display Inventory\n");
        printf("\t\t\t(S) Save current inventory to a file\n");
        printf("\t\t\t(C) Clear entire inventory\n");
        printf("\t\t\t(L) Load sales records from a file\n");
        printf("\t\t\t(Q) QUIT\n");

        scanf("%c",&choice);
               
        return choice;
}

void AddItem(orange_t or[], int *size)
{
        //prompt the user to input a brand,company,amount in stock,last ship date,cost and
price

}

char* BrandUpperCase(char word[])
{
        int wordSize;
        int i;
        wordSize = strlen(word);

        for(i = 0; i<wordSize; i++)
        {
                word[i] = toupper(word[i]);

        }
        printf ("\n Brand in al Caps: %s\n\n", word);
        return word;
       
}


void DeleteItem(orange_t or[], int *size, int location)
{
        //move the last element in the array to the location you want to delete
        //derement array size
}



void EditItem(orange_t or[],int location)
{
        int editField;

        printf("Here is the entry to edit:\n");
        DisplayItem(or,location);

        editField = EditMenu();

        if(editField == 1)
        {
                printf("\nEnter NEW brand: ");
                scanf("%s", or[location].brand);

                BrandUpperCase(or[location].brand);
        }
        else if(editField==2)
        {
                printf("\nEnter company: ");
                scanf("%s", or[location].company);
        }
        else if(editField==3)
        {
                printf("\nEnter amount in stock: ");
                scanf("%d", &or[location].inStock);
        }
        else if(editField==4)
        {
                printf("\nEnter last ship date: ");
                scanf("%s", or[location].lastShipDate);
        }
        else if(editField==5)
        {
                printf("\nEnter cost: ");
                scanf("%lf", &or[location].cost);
        }
        else if(editField==6)
        {
                printf("\nEnter price: ");
                scanf("%lf", &or[location].price);
        }

        else
        {
                printf("\nField selection not recognized\n");
        }


       
}

int EditMenu( )
{
        int select;
        printf("\nSelect the field to edit:\n");
        printf("1 to edit brand\n");
        printf("2 to edit company\n");
        printf("3 to edit amount in stock\n");
        printf("4 to edit last ship date\n");
        printf("5 to edit cost\n");
        printf("6 to edit price\n");

        scanf("%d", &select);
        return select;
}






void DisplayItem(orange_t or[], int i)
{
        printf("\n");
        printf("brand:\t\t%s\ncompany:\t%s\nin stock:\t%d\n",
or[i].brand,or[i].company,or[i].inStock);
        printf("ship
date:\t%s\ncost:\t\t%.2f\nprice:\t\t%.2f\n\n",or[i].lastShipDate,or[i].cost,or[i].price);
}


void SaveInventory(orange_t or[], int size)
{
        FILE * outp;
        if((outp = fopen("out.txt","w"))==0)
        {
                printf("\ncannot open file\n");
                return;
        }

        fprintf(outp,"%d", size);
        fwrite(or, sizeof(orange_t),size,outp);
        fclose(outp);

}



void ClearInventory(orange_t or[],int*size)
{
        *size = 0;
}



void LoadSalesRecords(orange_t or[], int*size)
{
//this function is supposed to retrive arraya elments from file
}


int FindItem(orange_t or[], int size)
{
        //search for the brand in the array, use strcmp functtion in a loop
        //return -1 if element not fond(no match)
       

}

void HardCodeEntries(orange_t entry[], int *size)
{
        //for charater arryas you need to use string copy function
         strcpy(entry[0].brand,"COLUMBIA");
         strcpy(entry[0].lastShipDate,"11/09/06");
         entry[0].cost=5.00;
         strcpy(entry[0].company,"Columbia Gorge Fruit Co");
         entry[0].price=6.00;
         entry[0].inStock=59;

        *size +=1;

         strcpy(entry[1].brand,"FLORIDA");
         strcpy(entry[1].lastShipDate,"11/09/06");
         entry[1].cost=5.00;
         strcpy(entry[1].company,"Floridas Natural LLC");
         entry[1].price=6.00;
         entry[1].inStock=59;

        *size +=1;

         strcpy(entry[2].brand,"TROPICANA");
         strcpy(entry[2].lastShipDate,"11/09/06");
         entry[2].cost=5.00;
         strcpy(entry[2].company,"PepsiCo");
         entry[2].price=6.00;
         entry[2].inStock=59;

        *size +=1;

         strcpy(entry[3].brand,"MINUTE");
         strcpy(entry[3].lastShipDate,"11/09/06");
         entry[3].cost=5.00;
         strcpy(entry[3].company,"Coca-Cola");
         entry[3].price=6.00;
         entry[3].inStock=59;
       
        *size +=1;

         strcpy(entry[4].brand,"ODWALLA");
         strcpy(entry[4].lastShipDate,"11/09/06");
         entry[4].cost=5.00;
         strcpy(entry[4].company,"Odwalla");
         entry[4].price=6.00;
         entry[4].inStock=59;

        *size +=1;

 
         
}
0
Comment
Question by:bestman711
  • 2
3 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 480 total points
Comment Utility

Howdy....


Check the original thread.  I've posted an example there.


Kent
0
 

Author Comment

by:bestman711
Comment Utility
I need help with the delete and find function.   Can you help....i really don't have much time
0
 

Author Comment

by:bestman711
Comment Utility
I need help with the delete and find function.   Can you help....i really don't have much time
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now