Thread: Please help! Beginner stuck on table and array problem!

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    11

    Unhappy Please help! Beginner stuck on table and array problem!

    Hi I am new to C programming and I have to produce a C program which holds data for competitors in a competition. The structure must have 5 fields, one to hold the competitor's name, one to hold a competitor number (which you allocate automatically as you read in the data for each competitor, use 1 for the first competitor, 2 for the second and so on), one to hold the river fish weight converted to ounces, one to hold the sea fish weight converted to ounces and a final field to hold the weight of fish caught in the fly fishing competition also converted to ounces.

    I must also define an array with 8 elements, each of which is a structure as defined above.

    The input should be exactly like this:
    Competitor's name: Fred_Bloggs
    Weight of Fish from the River Competition (Stones Pounds Ounces): 1 2 9
    Weight of Fish from the Sea Competition (Stones Pounds Ounces): 4 10 7
    Weight of Fish from the Fly Competition (Stones Pounds Ounces): 0 8 5

    and the output should be outputted into a table.

    Here is my code so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Field Definitions */
      typedef struct competitor {
        char name;
        int comp_number;
        int river_fish;
        int sea_fish;
        int fly_fish;
      } data	;
    
    
    main(){
      int n, count, x = 0; data *record;
    
      printf("Please enter number of competitors:\n");
      scanf("%d", &n);
    
      /* Creates dynamic array*/
      record = (data *)malloc(n * sizeof(data));
    
      for(count = 0; count <= (n-1); count++){
        printf("Please enter name competitor # %d\n", count+1);
        scanf("%s", &record[count].name);
        printf("Please enter weight of Fish from the River Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);
        scanf("%d", &record[count].river_fish);
        printf("Please enter weight of Fish from the Sea Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);
        scanf("%d", &record[count].sea_fish);
        printf("Please enter weight of Fish from the Fly Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);
        scanf("%d", &record[count].fly_fish);
      }
    
      for(count = 0; count <= (n-1); count++){
    { int i,j ;
      const i_limit = 8, j_limit = 5;
    
            do {
                    printf("row %d:", i);
    
    		do {
                            printf(" %3d", j);
                            j = j+1;
    			} while (j < j_limit);
    
                    		printf("\n");
    
                    		i++;
            		} while  ( i <=i_limit) ;
    }
    
    
    	 printf("Name: %s\n", (&record[count].name));
    	 printf("River Fishing: %d\n", (&record[count].river_fish));
    	 printf("Sea Fishing: %d\n", (&record[count].sea_fish));
    	 printf("Fly Fishing: %d\n", (&record[count].fly_fish));
        }
    
    
    
    }
    Please help, I would be eternally grateful! x

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    hi.
    man this error is in pointer that you are accessing by means of operator unary "&".
    In this part of code, pay attention and write again:
    Code:
    for(count = 0; count <= (n-1); count++){
        printf("Please enter name competitor # %d\n", count+1);
        scanf("%s", record[count]->name);
        printf("Please enter weight of Fish from the River Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);
        scanf("%d", record[count]->river_fish);
        printf("Please enter weight of Fish from the Sea Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);
        scanf("%d", record[count]->sea_fish);
        printf("Please enter weight of Fish from the Fly Competition (Stones Pounds Ounces) for competitor # %d\n", count+1);
        scanf("%d", record[count]->fly_fish);
      }
    when work with pointers to structures use every operator "->" that means pointer for one field of struct.

    Wait to have help u!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > typedef struct competitor {
    > char name;
    If you want a string (as you do), then you need a char array
    char name[50];

    > record = (data *)malloc(n * sizeof(data));
    Don't cast malloc in C.
    If you remove the cast and you're still getting errors, then you're probably compiling C++.

    > for(count = 0; count <= (n-1); count++)
    The common idiom for accessing all the elements of an array is
    for(count = 0; count < n; count++)

    > when work with pointers to structures use every operator "->" that means pointer for one field of struct.
    Actually, the OP had it right, and you've got it wrong.
    &foo->bar
    is identical to
    &foo[0].bar
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using Cursor to move an array into a new table
    By cjohnman in forum C Programming
    Replies: 3
    Last Post: 04-17-2008, 11:34 AM
  2. Placing array in temp table
    By cjohnman in forum C Programming
    Replies: 3
    Last Post: 04-16-2008, 12:53 PM
  3. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  4. Replies: 1
    Last Post: 12-07-2005, 10:23 PM
  5. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM