Thread: Help with Structures

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    24

    Help with Structures

    Alrighty...here's my problem...i have to get data entered from a user and insert it into a structure...i ask the user to give me a operation code (i = insert), i go to the insert function create my structure and ask the user for the data...but how do i store it into the structure ? can i try to store directly in scanf ?

    thanks here's my code
    Code:
    #include <stdio.h>
    
    int main()
    {
    
    	char operCode;
    
    	printf("Enter operation code: ");
    
    	scanf("%c", &operCode);
    
    	if (operCode == 'i')
    	{
    		insertPart();
    	}
    /*
    	if (operCode == 's')
    	{
    		searchPart();
    	}
    
    	if (operCode == 'u')
    	{
    		updatePart();
    	}
    
    	if (operCode == 'p')
    	{
    		printPart();
    	}
    */
    
    	if (operCode == 'q')
    	{
    		printf("Program Terminated !\n");
    	}
    }
    
    int insertPart()
    {
    
    	int part = 0;
    	char name;
    	int quan = 0;
    
    	struct comp_part
    	{
    		int partNum;
    		char partName[15];
    		int quantity;
    	};
    
    	printf("Enter part number: ");
    	scanf("%i", &part);
    
    	printf("Enter part name: ");
    	scanf("%c", &name);
    
    	printf("Enter quantity on hand: ");
    	scanf("%i", &quan);
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Maybe like this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct foo{
     char quux[10];
     int mynumby;
    }foo;
    
    
    int main(){
     struct foo mystruct;
    
     printf("Let's input some info on the struct\n");
     printf("Give a name with at most 10 chars and a number\n");
     //Scanf is evil but I'm lazy!
     scanf("%s",mystruct.quux);
     printf("\n");
     scanf("%d",&mystruct.mynumby);
    
     printf("%s %d\n",mystruct.quux,mystruct.mynumby);
     return 0;
    
    }
    Any descent C book covers this =/

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Why bother with the typedef if you're going to precede the identifier with the keyword struct, anyway?
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by SlyMaelstrom
    Why bother with the typedef if you're going to precede the identifier with the keyword struct, anyway?
    Maybe cause it is 3 AM and I was partying?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Must have been some party.

    I know I like to surf message boards when I'm having a wild time, too. Oh, and that applies if you were talking about drugs or sex...
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM