Simple structure question

This is a discussion on Simple structure question within the C Programming forums, part of the General Programming Boards category; Write a C program that scans data to fill the variable competition declared below and then displays the contents of ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    15

    Simple structure question

    Write a C program that scans data to fill the variable competition declared below and then displays the contents of the structure with suitable labels.

    Code:
    #define STR_LENGTH 20
    
    typedef struct
    {
    char event[STR_LENGTH], 
    entrant[STR_LENGTH], 
    country[STR_LENGTH];
    int place;
    } olympic_t;
    
    
    olympic_t competition;
    This is a fairly short question, and I am not so good with structures especially when printing them. So please do not tell me to DIY.

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    I will give u a starting point entrant and country both need data types.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    15
    This is my attempt.

    Code:
    int scan_olympic(olympic_t *competition)
    {
    int result;
    result = scanf("%c%c%c%d", competition->event, competition->entrant. competition->country, &competition->place);
    
    if (result == 10) /* To check if there is error */
    result = 1;
    else if (result != EOF)
    result = 0;
    
    return result;
    }
    I do not know how to print the structure competition.

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    407
    I am not so good with structures
    Practice will get you there just think of a large container for small containers. Like a brief case could store clothes, its brand, razor, deodorant, shampoo. Well if your program has 100 briefcases you would have to also declare all the things in it over and over. That would be 500 declarations. But if you could make container that was already assumed to have those items it would save a ton of time. This is your structure. You already showed you somewhat understand the format for the declaration but you aren't fully clear on the rules.

    Code:
    typedef struct{ // declare the structure .. big container
    datatype VariableName; // declare its variables.. small container
    }StructureName_t; // name of the large containter
    
    // the above wont compile so don't try
    // so to declare the above example of a briefcase
    
    typedef struct{
      char Brand[20];
      int NumOfClothes;
      char Razor[20];
      char Shampoo[20];
      bool Deodorant;
    }Suitcase_t;
    
    // now inside your functions you can declare the structure type
    Suitcase_t MySuitcase;
    // notice the same format?  The Suitcase_t is the datatype
    // MySuitcase is the variable name
    // now you can acess elements of the suitcase using a period
    MySuitcase.NumOfClothes = 32;
    // that would save 32 into the number of clothes position
    // you can also compare or output these variables like any other variable
    if(!MySuitcase.Deodorant && YourSuitcase.Deodorant)
      BorrowDeodorant();
    Test it out! Try simple datatypes of your own and do simple things. As your understanding becomes better the harder problems will suddenly seem simple.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    result = scanf("%c%c%c%d", competition->event, competition->entrant. competition->country, &competition->place);
    scanf() returns the number of succesfully read items. In your case that's 4 not 10.
    Are you sure that "event", "entrant" and "country" are only one character each?

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C# structure question
    By sketch in forum C# Programming
    Replies: 4
    Last Post: 09-14-2007, 05:29 PM
  2. simple structure/linked list question
    By niponki in forum C Programming
    Replies: 16
    Last Post: 08-15-2005, 12:13 AM
  3. I need help with sorting a simple structure please
    By AlmostBeginner in forum C Programming
    Replies: 2
    Last Post: 04-11-2003, 04:01 AM
  4. Simple program structure.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-15-2002, 05:36 AM
  5. Simple structure + pointer question
    By gogo in forum C Programming
    Replies: 4
    Last Post: 10-25-2001, 08:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21