Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   A Simple Inventory Program (http://www.go4expert.com/forums/simple-inventory-program-t3966/)

Peter_APIIT 21Apr2007 08:13

A Simple Inventory Program
 
Hello all programmer, i have a program where i don;t know how to code in array of struct pointer;

Below is my code:

Code:

// Inventory Program
// What is the correct method of array of struct pointer

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

void update();
void deleted();
void display();

struct Inventory
{
        int record;
        int quantity;
        float cost;
}hardware[10];//, *hardware_ptr

//hardware_ptr[0] = hardware;

int main(int argc, char *argv[])
{
        void (*functionptr[3])() = {update, deleted, display};
        // Declare functionptr pointed to three function
        int loop;
        int choice;
        printf("\n\t\t");

        for (loop=0;loop<45;loop++)
        {
                printf("-");
        }
        printf("\n\n\t\t          Welcome to Inventory System");
        printf("\n\n\t\t");
        for (loop=0;loop<45;loop++)
        {
                printf("-");
        }
        while(1)
        {
                printf("\n\n\n\t\t\t\tEnter a choice : ");
                scanf("%d", &choice);

                if (choice == 1)
                {
                        choice = 0;
                }
                else if (choice == 2)
                {
                        choice = 1;
                }
                else if ( choice == 3)
                {
                        choice = 2;
                }
                else
                {
                        perror("Invalid Choice");
                }
                // Advanced Techniques which make use of array of function pointer
                // Call the function using functionptr
                (*functionptr[choice])();
/*                switch(choice)
                {
                case 1:
                        {
                                update();
                        }
                case 2:
                        {
                                deleted();
                        }
                case 3:
                        {
                                display();
                        }
                default:
                        {
                                perror("Invalid Choice");
                        }
                }*/
        }
}
void update()
{
        FILE *fp;
        int loop;

        fp = fopen("C:\\Test.txt", "a");
        assert(fp!=NULL);
        if (fp!=NULL)
        {
                printf("Enter the record number,quantity and cost for the tool name");
                printf("\nRecord\t\tQuantity\t\tCost : \n");
                for (loop=0;loop<10;loop++)
                {       
                        scanf("%d%d%f", &hardware[loop].record, &hardware[loop].quantity, &hardware[loop].cost);
                        printf("\nRecord\t\tQuantity\t\tCost : \n");
                }

                fprintf(fp, "\nRecord\t\tTool Name\t\tQuantity\t\tCost");
                for (loop=0;loop<10;loop++)
                {
                        fprintf(fp, "%d%d%f", hardware[loop].record, hardware[loop].quantity, hardware[loop].cost);
                }       
        }
        else
        {
                perror("File cannot be opened");
        }
        fclose(fp);
}

void deleted()
{
        FILE *fp;
        int record_number, loop;

        fp = fopen("C:\\Test.txt", "a");
        assert(fp!=NULL);
        if (fp!=NULL)
        {
                printf("Enter a record number to delete : ");
                scanf("%d", &record_number);
               
                for (loop=0;loop<10;loop++)
                {
                        if (record_number == hardware[loop].record)
                        {
                                hardware[loop].record = '0';;
                                hardware[loop].quantity = '0';
                                hardware[loop].cost = '0';
                        }
                        else
                        {
                                perror("Invalid Choice");
                        }
                }
        }
        else
        {
                perror("File cannot be opened");
        }
        fclose(fp);
}
void display()
{
        FILE *fp;
        int loop;

        fp = fopen("C:\\Test.txt", "r");
        assert(fp!=NULL);
        if (fp!=NULL)
        {
                printf("\nrecord\t\tTool Name\t\tQuantity\t\tCost\n\n");
                for (loop=0;loop<10;loop++)
                {
                        printf("%d  %d  %f\n", hardware[loop].record, hardware[loop].quantity, hardware[loop].cost);
                }
        }
        else
        {
                perror("File canoot be opened");
        }
        fclose(fp);
}


I look forware for any help.

Thanks in advance for oyur help.

Your help is greatly appreciated by me an others.

shabbir 21Apr2007 08:43

Re: A Simple Inventory Program
 
Instead of putting all your code just help us to help you by having as relevant code as possible.

Peter_APIIT 21Apr2007 08:45

Re: A Simple Inventory Program
 
I just want to put all the information i need to post in order for the helper to understand.

shabbir 21Apr2007 11:20

Re: A Simple Inventory Program
 
Quote:

Originally Posted by Peter_APIIT
I just want to put all the information i need to post in order for the helper to understand.

But that way you want the helper to find the problem as well. You can always have the complete source as a reference but should point to the place where you think the problem could be and dont tell me its the complete program where you think the problem is.

DaWei 21Apr2007 21:20

Re: A Simple Inventory Program
 
Information is key to resolving problems. There is no substitute. That is why we use debuggers and printing or logging features: to find out the state of the program at a particular instant.

On the other hand, unrelated information stalls the process, overwhelms the mind. Here are some tips for asking a good question.

BE DESCRIPTIVE

I cannot stress this enough. We cannot possibly help you if you simply post a thread, copy and paste your whole code and say "it has errors what's wrong with it?". Keep this in mind:

1. Explain to us what your code DOES (or what it's supposed to do).

2. Explain to us what exactly the error is. What kind of error? Or is it just not doing what you want?

3. Remember that you know a lot more about your code than we do. Don't assume we know just what it does.

ONLY POST THE PART OF YOUR CODE THAT HAS PROBLEMS!

PLEASE do not post your entire code in a message. And if you have to use 2 messages that's a good indication that you're going to annoy people. Only post the part of your code where you have problems. That's it. Just a very small piece of it. It's not hard. Just find the part you think doesn't work and post that. WE DON'T WANT TO SEE YOUR ENTIRE CODE. If you post the entire code it's going to take us a lot longer to find the problem.

If you get an error on a certain LINE, tell us what's on that line and what's on the few lines above it!

It's not too helpful when you mention an error on a certain line but don't tell us what code is on that line. Because errors can sometimes be caused by lines of code above the one the error is mentioned on, please post a few lines above that line.

ONLY YOU KNOW WHAT "DOESN'T WORK" MEANS.

Please tell us what your code is supposed to do and why it "doesn't work". Or if you don't feel like explaining what the whole code is, at least tell us what the offending code is supposed to do. When you come in and say "it doesn't work" or just "it has errors", that means NOTHING. There could be a million things wrong and no one will know where to look.

REMEMBER, WHEN PEOPLE HELP YOU, THEY ARE DOING YOU A FAVOR

Regardless of how big your ego is, it is NOT someone else's privilege to debug Your code. It is not their privilege to have them help you. It is YOURS. Remember that when people help you they are doing YOU a favor. You are not doing them a favor by letting them see your incredible application. Be patient, help people out by posting good descriptions of what you need help with, and not snapping at people with garbage such as "if you aren't going to help don't waste my time replying".

Peter_APIIT 23Apr2007 11:39

Re: A Simple Inventory Program
 
My problem is how to declare array of structure pointer.

DaWei 23Apr2007 17:58

Re: A Simple Inventory Program
 
That part's easy:
Code:

struct Inventory
{
    int record;
    int quantity;
    float cost;
} hardware;

int main ()
{
    struct hardware *pHardware [10];
    ...

The problem, at this point, is that those pointers are meaningless. They have to be initialized to point at an actual hardware struct before you can use them. If you don't understand what I mean by that, then see the link in my signature concerning pointers.

A more readable way to write that (this is C, not C++) is:
Code:

typedef struct sInventory *pInventory;
typedef struct sInventory
{
    int record;
    int quantity;
    float cost;
} Inventory;

int main ()
{
    pInventory pHardware [10];
    ....

To generate the actual structs, you could set up a loop and use malloc to allocate the actual structs, one by one, and assign the values you receive from malloc to pHardware.
Code:

    for (i = 0; i < 10; i++)
    {
        pHardware [i] = malloc (sizeof (Inventory));
    }

In C, do not cast the return of malloc.

Peter_APIIT 24Apr2007 15:47

Re: A Simple Inventory Program
 
As i know, there is two methods to initialize the pointer which is after declration and by malloc.

I have two problem. The first problem is when i declared *hardware_ptr[10], *hardware[10] and i initialized it by this statement. hardware_ptr[0] = &hardware. The compiler return an error which is
cannot initialized an array with constant size.

I initialized the pointer using malloc and run without error but when i change the function pointer, the compiler give some syntax error where i cannot solved.

Below is my code:

Code:

// Inventory Program
// What is the correct method of array of struct pointer
// One restrictions of array of function pointer is
// the all function must have same retun value and passingvalue

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

void instructions();
void update(int);
void deleted(int);
void display(int);

struct Inventory
{
        int record;
        int quantity;
        float cost;
}hardware[10], **hardwareptr;


 
int main(int argc, char *argv[])
{
        int loop;
        int choice;
        int nrecords=3;
        void (*functionptr[3])(int) = {void update(int), void deleted(int), void display(int)};
        // Declare functionptr pointed to three function

        printf("\n\t\t");
        for (loop=0;loop<45;loop++)
        {
                printf("-");
        }
        printf("\n\n\t\t          Welcome to Inventory System");
        printf("\n\n\t\t");
        for (loop=0;loop<45;loop++)
        {
                printf("-");
        }
        while(1)
        {
                instructions();
                printf("\n\n\n\t\t\t\tEnter a choice : ");
                scanf("%d", &choice);

                if (choice == 1)
                {
                        choice = 0;
                }
                else if (choice == 2)
                {
                        choice = 1;
                }
                else if ( choice == 3)
                {
                        choice = 2;
                }
                else
                {
                        perror("Invalid Choice");
                }
                // Advanced Techniques which make use of array of function pointer
                // Call the function using functionptr
                (*functionptr[choice])(nrecords);

                // Function Call
/*                switch(choice)
                {
                case 1:
                        {
                                update();
                        }
                case 2:
                        {
                                deleted();
                        }
                case 3:
                        {
                                display();
                        }
                default:
                        {
                                perror("Invalid Choice");
                        }
                }*/
        }
}
// ------------------------------------------------------------
void instructions()
{
        printf("\n\n\t\t\t\t  Update - 1");
        printf("\n\t\t\t\t  Delete - 2");
        printf("\n\t\t\t\t  Display - 3");
}
// ------------------------------------------------------------
void update(int nrecords)
{
        FILE *fp;
        int loop;
        int col_loop = 3, totalrecords = 3;
        // Total records inside the Inventory structure

        fp = fopen("C:\\Test.txt", "a");
        assert(fp!=NULL);
        if (fp!=NULL)
        {
                printf("How many records : ");
                scanf("%d", &nrecords);
                printf("Enter the record number,quantity and cost for the tool name");
                printf("\nRecord\t\tQuantity\t\tCost : \n");

                hardwareptr = malloc(sizeof(**hardwareptr) * nrecords);

                for (loop=0;loop<nrecords;loop++)
                {
                        for (col_loop=0;col_loop<3;col_loop++)
                        {
                                hardwareptr[loop] = malloc(sizeof(*hardwareptr) * totalrecords);
                                if (hardwareptr[loop] == NULL)
                                {
                                        perror("Dynamic Memory Allocation fails");
                                }
                        }
                }
                if (hardwareptr == NULL)
                {
                        perror("Dynamic Memory Allcoation Fails");
                }
                else
                {
                        for (loop=0;loop<nrecords;loop++)
                        {       
                                scanf("%d%d%f", &hardwareptr[loop]->record, &hardwareptr[loop]->quantity, &hardwareptr[loop]->cost);
                                printf("\nRecord\t\tQuantity\t\tCost : \n");
                        }

                        printf("\n\nRecord\t\tQuantity\t\tCost\n");
                        fprintf(fp, "\nRecord\t\tTool Name\t\tQuantity\t\tCost");
                        for (loop=0;loop<nrecords;loop++)
                        {
                                printf("%d\t\t%d\t\t\t%f\n", hardwareptr[loop]->record, hardwareptr[loop]->quantity, hardwareptr[loop]->cost);
                                fprintf(fp, "%d%d%f", hardwareptr[loop]->record, hardwareptr[loop]->quantity, hardwareptr[loop]->cost);
                        }
                }
        }
        else
        {
                perror("File cannot be opened");
        }
        fclose(fp);
}
// ------------------------------------------------------------
void deleted(int nrecords)
{
        FILE *fp;
        int record_number, loop;

        fp = fopen("C:\\Test.txt", "a");
        assert(fp!=NULL);
        if (fp!=NULL)
        {
                printf("Enter a record number to delete : ");
                scanf("%d", &record_number);
               
                for (loop=0;loop<nrecords;loop++)
                {
                        if (record_number == hardwareptr[loop]->record)
                        {
                                hardwareptr[loop]->record = '0';
                                hardwareptr[loop]->quantity = '0';
                                hardwareptr[loop]->cost = '0';
                                fprintf(fp, "%d%d%f", hardwareptr[loop]->record, hardwareptr[loop]->quantity, hardwareptr[loop]->cost);
                        }
                        else
                        {
                                perror("Invalid Choice");
                        }
                }
        }
        else
        {
                perror("File cannot be opened");
        }
        fclose(fp);
}
// ------------------------------------------------------------
void display(int nrecords)
{
        FILE *fp;
        int loop;

        fp = fopen("C:\\Test.txt", "r");
        assert(fp!=NULL);
        if (fp!=NULL)
        {
                printf("\nrecord\t\tQuantity\t\tCost\n\n");
                for (loop;loop<nrecords;loop++)
                {
                        fscanf(fp, "%d%d%f", hardwareptr[loop]->record, hardwareptr[loop]->quantity, hardwareptr[loop]->cost);
                }
                for (loop=0;loop<nrecords;loop++)
                {
                        printf("%d  %d  %f\n", hardwareptr[loop]->record, hardwareptr[loop]->quantity, hardwareptr[loop]->cost);
                }
        }
        else
        {
                perror("File cannot be opened");
        }
        fclose(fp);
}
// ------------------------------------------------------------

Later, i will post this program for others to learn
I will remember your teaching.

Thanks for your help.

Your help is greatly appreciated by me and others.

DaWei 24Apr2007 17:34

Re: A Simple Inventory Program
 
Again, you are not asking your question in a good way.
Quote:

the compiler give some syntax error where i cannot solved.
PLEASE give the exact error message that the compiler produced!! PLEASE indicate the line which produced the error!!! PLEASE do not post the entirety of you code that is NOT producing the error!!!

Additionally, why do you do this:
Code:

                if (choice == 1)
                {
                        choice = 0;
                }
                else if (choice == 2)
                {
                        choice = 1;
                }
                else if ( choice == 3)
                {
                        choice = 2;
                }
                else

when each of those statements does the same thing, namely subtract 1 from choice. Use this:
Code:

    choice -= 1;
    if ((choice < 0) || (choice > 2)) perror ("Invalid choice");

Now, please review those posting guidelines I gave you, and please post information regarding your error appropriately. I am NOT going to go through all that code looking for a syntax error.

Peter_APIIT 25Apr2007 07:11

Re: A Simple Inventory Program
 
I bag your pardon. I will not repeat the mistake again.

Th code which give me error is

Code:

void (*functionptr[3])(int *) = {void update(int *), void deleted(int *), void display(int *)};
The error message is error C2059: syntax error : 'type' from MS VC++ 6.0

Thnaks for your help.

Your help is greatly appreciated by me and others.

Thnaks.


All times are GMT +5.5. The time now is 05:01.