Thread: Help with assignment (Olympic medals)

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    5

    Help with assignment (Olympic medals)

    Code:
    country *add_country(struct country *array, char *name)
    {
    
      int i;
      for (i = 0; array.name != NULL; i++);
      struct country *newcountry = realloc(array, sizeof(struct country) * (i + 2));
      if (newcountry == NULL) {
        return NULL;                //allocating failed
      }
      newcountry.name = name;
      newcountry.medals.gold = 0;
      newcountry.medals.silver = 0;
      newcountry.medals.bronze = 0;
      newcountry[i + 1].name = NULL;
      return newcountry;
    }
    
    void printtable(struct country *a)
    {
      for (int i = 0; a.name != NULL; i++) {
        printf("%s %d %d %d\n", a.name, a.medals.gold, a.medals.silver,
               a.medals.bronze);
      }
    }
    
    int main()
    {
    
      struct country *array = malloc(sizeof(struct country));
      array[0].name = NULL;         //Initialize array
    
      char input[80];
      char c[20];
    
      printf("Enter country and medals\n");
      printf("\n");
    
      while (1) {
    
        fgets(input, sizeof(input), stdin);
    
        if (input[0] == 'A') {
          sscanf(input, "%*c %s", c);
          array = add_country(array, c);
          if (array == NULL) {
            printf("\nCreating country failed");
          }
    
    
        } else if (input[0] == 'L') {
          printtable(array);
    
        } else if (input[0] == 'Q') {
          free(array);
          printf("Exiting program.\n");
          break;
        }
      }
      return 0;
    }
    I'm building a program that calculates Olympic medals for different countries. First input is for Example 'A USA' which creates a new field. Second input is for example 'L' which prints the table 'USA 0 0 0'. Problem occurs however, when I want to input a second country. For example, if my inputs where to look like this:

    A USA
    A CANADA
    L

    It should print out:
    USA 0 0 0
    CANADA 0 0 0

    But the result i get is:
    CANADA 0 0 0
    CANADA 0 0 0

    I suspect that the second input somehow overwrites the first input (I dunno, beginner right here...)? If someone could help me with this it would be great!
    Last edited by Salem; 04-06-2020 at 05:51 AM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > array = add_country(array, c);
    Your function needs to make a copy of c, not just keep a pointer to it.

    Otherwise, as you observe, everything ends up pointing to the same string in memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    5
    Thank you so much!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating Olympic medals
    By Shaun123 in forum C Programming
    Replies: 11
    Last Post: 04-17-2018, 03:28 AM
  2. New Olympic games?
    By cpjust in forum General Discussions
    Replies: 14
    Last Post: 02-22-2010, 12:23 PM
  3. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  4. Beijing 2008 Olympic Mascots
    By Hermitsky in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-15-2005, 01:53 PM

Tags for this Thread