A Simple Inventory Program

Discussion in 'C' started by Peter_APIIT, Apr 21, 2007.

  1. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Instead of putting all your code just help us to help you by having as relevant code as possible.
     
  3. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    I just want to put all the information i need to post in order for the helper to understand.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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".
     
  6. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    My problem is how to declare array of structure pointer.
     
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  8. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    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.
     
  9. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Again, you are not asking your question in a good way.
    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.
     
  10. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    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.
     
  11. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You can not assign function addresses like this:
    Code:
        void (*functionptr[3])(int *) = {void update(int *), void deleted(int *), void display(int *)};
    
    "void update (int *)" looks like a declaration, not an address.
    "update (int *)" looks like a messed up invocation, not an address.
    Use the following form:
    Code:
        void (*functionptr[3])(int) = {update, deleted, display};
    
    You should not be using VC++6.0 unless there is an absolutely compelling reason to do so (your tutor has threatened to shoot you, or something). VC++6.0 was written BEFORE the C++ standard was adopted. It is non-compliant in many respects. If you use it, you will have to disregard many of the things that you have been told about producing robust and compliant code.

    VC++2005 Express Edition is free for the download. If you want a CD, you only have to pay shipping. Another free compiler is Dev-Cpp, from Bloodshed. It uses MinGW, a gcc/g++ port to Windows. Neither is fully compliant with the standard (few compilers are), but they are much, much better than VC++6.0.
     
  12. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    From my knowledge, one can assign name of array name to a pointer such as
    Code:
    
    int *ptr;
    int array[3];
    ptr = array;            // or ptr = &array; -- Array name is [URL=http://www.go4expert.com/articles/difference-constant-pointers-pointer-t26816/]constant pointer[/URL];
    
    The same concept applied to function pointer. 
    Name of function is constant pointer to that function. 
    The correct methods is 
    void (*functionptr[3])(int *) = {update, deleted, display};
    and not
    void (*functionptr[3](int *) = {update(), deleted(), dispaly()};  
    
    I just guess. I don't know whether it is correct.

    By the way, i do not understand this two different code.
    Code:
    int *ptr;
    int number = 1;
    ptr = &number;
    
    and

    Code:
    int number = 2;
    int *ptr = number;
    
    * and -> is deference operator - to get the value
    & is to get the address.

    Sorry for my stupidness.

    Thanks for your help.
    I will post this program afterwards in order to help someone to understand function pointer, array of structure pointer and file operation.

    Your help is greatly appreciated by me and others.
     
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Code:
    int *ptr; // Declaring a pointer which may point to any undefined location
    int number = 1; // Declaring a variable and assigning the value 1 to it
    ptr = &number; // Assigning the address of the variable number to the pointer ptr
    
    See my comments after each line.

    Code:
    int number = 2; // Declaring a variable and assigning the value 1 to it
    int *ptr = number; // This should be int *ptr = &number; which will have the same effect as above 3rd ststement
    
    See my comments after each line.

    Now I would suggest you create seperate thread for each of your query and having the same query in the same thread has 2 disadvantage.

    1. some queries may get unnoticed.
    2. Other people searching will not be helped and so may ask the same question over and over again.
     
  14. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    One can assign an array name to a pointer (myPtr = myArray) simply because the standard says that the compiler must accept that and make the conversion. It is equivalent to "myPtr = &myArray [0]". Note that this yields some apparent equivalencies in usage, however myPtr and myArray are NOT the same. When the two are dereferenced (myPtr [3] and myArray [3]), different machine code is emitted in order to produce the value in element 3. See the tutorial in my signature, the section WHAT IS NOT A POINTER, for more.
     
  15. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Thanks for your help.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice