Thread: Linked List problem..

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    12

    Exclamation Linked List problem..

    Dear guys,

    I'm facing some problem about the Name is not displaying as I expected..

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    
    int selection = 0; //Always return 0 after a function is executed
    
    
    struct productInfo
    {
        int categoryID;
        char categoryName[50];
        int productID;
        char productName[50];
        float productPrice;
        int productUnit;
        struct productInfo *nextPtr;
    };
    
    
    struct productInfo get;
    struct productInfo *tempStorage;
    struct productInfo *previousPtr = NULL;
    
    
    void mainMenu (void);
    void addItem (void);
    void deleteItem (void);
    void updateItem (void);
    void sortItem (void);
    void display (void);
    
    
    /*In this problem, you will need to create an inventory system
    that enable user to add, delete,
    update product category (example: id, name)
    product info (example: id, name, price, inventory level etc).*/
    
    
    int main()
    {
        do
        {
        mainMenu(); //Pick an option
        } while (selection == 0);
    }
    
    
    void mainMenu (void)
    {
        printf("~*~*~*~Welcome to the main menu~*~*~*~\n"
        " 1 to add a category or item\n"
        " 2 to update a category or item\n"
        " 3 to delete an category or item\n"
        " 4 to end\n"
        "?");
        scanf("%d", &selection);
    
    
        switch(selection)
        {
            case 1:
                addItem();
                //sortItem();
                //system("CLS");
                display();
                break;
            case 2:
                deleteItem();
                break;
            case 3:
                updateItem();
                break;
            default:
                printf("Re-enter");
                selection = 0;
        }
    }
    
    
    void addItem (void)
    {
        int tempCategoryID = 0;
        int tempProductID = 0;
    
    
        char tempCategoryName[50];
        char tempProductName[50];
    
    
        float tempProductPrice = 0.00;
        int tempProductUnit = 0;
        
        int decision = 0;
        
        fflush(stdin); //make sure all the buffered output is flushed
        printf("Please key in the Category ID :\n");
        scanf("%d", &tempCategoryID);
        
        fflush(stdin);
        printf("Please key in the Category Name :\n");
        scanf("%[^\n]", tempCategoryName);
        
        fflush(stdin);
        printf("Key in 1 if you have a product to store.\nKey in others number to skip.\n");
        scanf("%d", &decision);
        
        if (decision == 1) {
            fflush(stdin);
            printf("Please key in the Product ID :\n");
            scanf("&d", &tempProductID);
            
            fflush(stdin);
            printf("Please key in the Product Name :\n");
            scanf("%[^\n]", tempProductName);
            
            fflush(stdin);
            printf("Please key in the Product Price :\nRM");
            scanf("%f", &tempProductPrice);
            
            fflush(stdin);
            printf("Please key in the Product Unit :\n");
            scanf("%d", &tempProductUnit);
        } else {
            tempProductID = tempCategoryID*100;
            tempProductName[0] = '\0';
            tempProductPrice = 0.00;
            tempProductUnit = 0;
        }
    
    
        tempStorage = malloc(sizeof(struct productInfo));
    
    
        tempStorage -> categoryID = tempCategoryID;
        tempStorage -> categoryName[50] = tempCategoryName[50];
        tempStorage -> productID = tempProductID;
        tempStorage -> productName[50] = tempProductName[50];
        tempStorage -> productPrice = tempProductPrice;
        tempStorage -> productUnit = tempProductUnit;
        tempStorage -> nextPtr = previousPtr;
        previousPtr = tempStorage;
    }
    
    
    void display (void)
    {
    	struct productInfo *displayItem;
    	displayItem = previousPtr;
    	char checkName[50] = "";
    
    
    	while (displayItem != NULL)
    	{
    		if (displayItem -> categoryName == checkName)
    		{
    			printf("IDa : %d\n", displayItem -> productID);
    			printf("Name : %s\n", displayItem -> productName);
    			printf("Price : %.2f\n", displayItem -> productPrice);
    			printf("Unit : %d\n", displayItem -> productUnit);
    		} else {
    			printf("Category\n");
    			printf("IDb : %d\n", displayItem -> categoryID);
    			printf("Name : %s\n", displayItem -> categoryName); //no output
    			checkName[50] = displayItem -> categoryName[50];
    			printf("Product\n");
    
    
    			if (displayItem -> productName == "")
    			{
    				printf("No product");
    			} else {
    				printf("IDc : %d\n", displayItem -> productID); //output have problem
    				printf("Name : %s\n", displayItem -> productName); //no output
    				printf("Price : %.2f\n", displayItem -> productPrice);
    				printf("Unit : %d\n", displayItem -> productUnit);
    			}
    		}
    		displayItem = displayItem -> nextPtr;
    	}
    }
    
    
    void deleteItem (void)
    {
        printf("asdads\n");
    }
    
    
    void updateItem (void)
    {
        printf("asdads\n");
    }
    I found that, all about char has no output, why

    Regards,
    Milo.
    Last edited by Milo Juak MuTou; 12-06-2012 at 04:42 PM.

  2. #2
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    38
    Hi,

    Looking at your code, what I understand is that at the line n° 155 you test equality based on address of pointers and not their pointed values

    Code:
    if (displayItem -> categoryName == checkName)
    {
        printf("IDa : %d\n", displayItem -> productID);
        printf("Name : %s\n", displayItem -> productName);
        printf("Price : %.2f\n", displayItem -> productPrice);
        printf("Unit : %d\n", displayItem -> productUnit);
    } 
    else
    {
        printf("Category\n");
        printf("IDb : %d\n", displayItem -> categoryID);
        printf("Name : %s\n", displayItem -> categoryName); //in here, the name simply does not come out.. why?
    }
    So if the program flow is directed towards the else block, this means that:
    Code:
    displayItem -> categoryName != checkName
    Which is obviously true as they are two different pointers pointing to two different places, Before the while loop at the line 153 you declare checkName as the following
    Code:
    char checkName[50] = "";
    This will always lead you to the Else block with checkName as empty.

    Regards,
    Dariyoosh

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    12
    Hellow thanks for your respond,
    I believe the problem is not there, I think the problem is somewhere around :
    Code:
    	tempStorage -> categoryID = tempCategoryID;
    	tempStorage -> categoryName[50] = tempCategoryName[50]; //this part
    	tempStorage -> productID = tempProductID;
    	tempStorage -> productName[50] = tempProductName[50]; //this part
    	tempStorage -> productPrice = tempProductPrice;
    	tempStorage -> productUnit = tempProductUnit;
    	tempStorage -> nextPtr = previousPtr;
    	previousPtr = tempStorage;
    I did something wrong around here, thus making the string not passed.. sigh i'm still very confusing..

  4. #4
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    38
    I have never seen an assignment like this
    Code:
    tempStorage -> productName[50] = tempProductName[50]; //this part
    Are you sure that you compile this without any error? (Linux or Windows?)

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I get a couple errors when I compile your code:
    Code:
    $ make list
    gcc -Wall -Werror -ggdb3 -std=c99 -pedantic list.c -o list -lm -lpthread -lefence
    list.c: In function ‘addItem’:
    list.c:111:9: error: too many arguments for format [-Werror=format-extra-args]
    list.c: In function ‘display’:
    list.c:169:44: error: comparison with string literal results in unspecified behavior [-Werror=address]
    cc1: all warnings being treated as errors
    make: *** 
    [list] Error 1
    The first one (line 111) is from this:
    Code:
    scanf("&d", &tempProductID);
    That & should be a %.

    The second one, line 169:
    Code:
    if (displayItem -> productName == "")
    You can't compare strings with ==, you would normally use strcmp. However, if all you're checking is whether a string is empty, you can also check strlen(str) == 0, or if (str[0] == '\0'), which is even more efficient. You also have a very similar error on line 158, though the compiler can't catch it because you are comparing two string variables instead of a string literal.

    You have several places that exhibit undefined behavior (which is bad). For starters, everywhere you use the following results in undefined behavior. Read these links for an explanation and alternatives: FAQ > Why fflush(stdin) is wrong - Cprogramming.com and FAQ > Flush the input buffer - Cprogramming.com.
    Code:
    fflush(stdin);
    Also in your display function, you do this:
    Code:
    checkName[50] = displayItem -> categoryName[50];
    Both checkName and displayItem->categoryName are arrays of 50 elements, which means that you can only access indexes 0-49 (remember, arrays in C start at 0). So you are writing to memory you don't own which results in undefined behavior. For me, this manifests as your program crashing. For you it may manigest as strange output. That's the thing with undefined behavior, it can be anything at all (even formatting your hard drive, though this is highly unlikely). If you intend to copy whole strings, use strcpy.

    You also have similar problems other places, like the following lines in your addItem function:
    Code:
    tempStorage -> categoryName[50] = tempCategoryName[50];
    ...
    tempStorage -> productName[50] = tempProductName[50];
    Those should also be strcpy(tempStorage->categoryName, tempCategoryName), and similar for product.

    That's what I found so far, in a fairly quick read-through of your code. It should be enough to keep you busy for a while.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    12
    Thanks! I finally resolved my problem by using strcpy, I feel I'm stupid for making mistake like &d lol.. Thanks for pointing that out Learnt a lots of thing, this has been bugging me for whole midnight... again, thanks you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list problem
    By nynicue in forum C Programming
    Replies: 15
    Last Post: 07-27-2009, 02:57 PM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. Linked List Problem
    By Brew in forum C Programming
    Replies: 6
    Last Post: 03-22-2003, 02:39 AM
  4. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  5. Linked List problem
    By richmaz in forum C Programming
    Replies: 5
    Last Post: 04-10-2002, 07:29 PM