Please help! Newbie pointer problem1

This is a discussion on Please help! Newbie pointer problem1 within the C Programming forums, part of the General Programming Boards category; I am new to C, and have defined a structure to hold data for a competitor in a fishing competition. ...

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

    Unhappy Please help! Newbie pointer problem1

    I am new to C, and have defined a structure to hold data for a competitor in a fishing competition.

    The program allows a user to input data like this:


    Please enter number of competitors:
    1

    Please enter name (separated by underscores) of competitor # 1:
    Sam

    Please enter weight of Fish from the River Competition (Stones Pounds Ounces) for competitor # 1:
    4 5 6

    Please enter weight of Fish from the Sea Competition (Stones Pounds Ounces) for competitor # 1:
    4 5 6

    Please enter weight of Fish from the Fly Competition (Stones Pounds Ounces) for competitor # 1:
    4 5 6

    It then stores then converts the amounts from stones pounds and ounces to just ounces and outputs:

    =====================================

    Name: sam
    Competitor Number: 1
    River Fishing: 1190
    Sea Fishing: 1190
    Fly Fishing: 1190

    I now need to create a function which has a single parameter, namely a pointer to a single competitor structure, which prints out the data in that structure in the exact format as shown below:

    NAME competitor number river fishing sea fishing fly fishing total weight
    ================================================== ======================================
    Helen_Fuell 7 1St 13Pd 10Oz 4St 7Pd 15Oz 2St 5Pd 7Oz 8St 13Pd 0Oz
    Bert_Hill 3 0St 12Pd 9Oz 1St 11Pd 5Oz 1St 10Pd 4Oz 4St 6Pd 2Oz

    and so on for the other six competitors.

    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, lbs, st, oz, x = 0; data *record;
    
      printf("\n************************ Welcome To The Fishing Competition Program! *****************************\n\n");
    
      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("\nPlease enter name (separated by underscores) of competitor # %d:\n", count+1);
        scanf("%s", &record[count].name);
        printf("\nPlease enter weight of Fish from the River Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
        scanf("%d %d %d", &lbs, &st, &oz);
        record[count].river_fish = (((st*14)+lbs)*16)+oz;
    
        printf("\nPlease enter weight of Fish from the Sea Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
        scanf("%d %d %d", &lbs, &st, &oz);
        record[count].sea_fish = (((st*14)+lbs)*16)+oz;
    
        printf("\nPlease enter weight of Fish from the Fly Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
        scanf("%d %d %d", &lbs, &st, &oz);
        record[count].fly_fish=(((st*14)+lbs)*16)+oz;
        /* Creates competitor number, initialises it as 1, then increments by 1 each time */
        record[count].comp_number = count +1;
      }
    
      for(count = 0; count <= (n-1); count++){
    	 printf("\n=====================================\n\n");
    	 printf("Name: %s\n", &record[count].name);
    	 printf("Competitor Number: %d\n", record[count].comp_number);
    	 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 x

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's a prototype for your function:
    Code:
    void print_competitor(data *competitor)
    > char name;
    This name will only hold one character. You probably want a string array:
    char name[30];

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

    Sorting

    Many thanks for your help, you were right.
    The program is now working effectively and outputting the weight converted back into stones, pounds and ounces.
    Now I'm trying to work out how to calculate the total weight for each competitor and write a function which can sort the data in the array so that the competitor with the greatest weight of fish is sorted into element 0, the competitor with the least weight of fish is sorted into element 7, and the others are sorted appropriately into the places in between. The function MUST have a single parameter which is a pointer to the array of structures. any ideas?....

    here's the new code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Field Definitions */
    typedef struct competitor{
    char name[50];
    int comp_number;
    int river_fish;
    int sea_fish;
    int fly_fish;
    } data ;
    
    /*Method to print out structure, using pointer 'entry'*/
    void print_entry(struct competitor * entry){
    int lbs, st, oz, lbs1, st1, oz1, lbs2, st2, oz2; /*Temporary variables to used to convert total ounces bact to lb's, st's, oz's*/
    
    st=(entry->river_fish/224);
    lbs=((entry->river_fish%224)/16);
    oz=((entry->river_fish%224)%16);
    
    st1=(entry->sea_fish/224);
    lbs1=((entry->sea_fish%224)/16);
    oz1=((entry->sea_fish%224)%16);
    
    st2=(entry->fly_fish/224);
    lbs2=((entry->fly_fish%224)/16);
    oz2=((entry->fly_fish%224)%16);
    
    /* Print out formatted data from pointer 'entry' */
    printf("%s \t\t %d \t\t %d %d %d \t\t %d %d %d\t\t %d %d %d\n",
    entry->name, entry->comp_number, lbs,st,oz, lbs1, st1, oz1, lbs2, st2, oz2);
    printf("\n");
    }
    
    main(){
    int n, count, lbs, st, oz, x = 0; data *record;
    
    printf("\n************************ Welcome To The Fishing Competition Program! *****************************\n\n");
    
    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("\nPlease enter name (separated by underscores) of competitor # %d:\n", count+1);
    scanf("%s", &record[count].name);
    printf("\nPlease enter weight of Fish from the River Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
    scanf("%d %d %d", &lbs, &st, &oz); /* Add values to temporary variables */
    record[count].river_fish = (((st*14)+lbs)*16)+oz; /* Formula to convert total weight in ounces and store in array */
    
    printf("\nPlease enter weight of Fish from the Sea Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
    scanf("%d %d %d", &lbs, &st, &oz);
    record[count].sea_fish = (((st*14)+lbs)*16)+oz;
    
    printf("\nPlease enter weight of Fish from the Fly Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
    scanf("%d %d %d", &lbs, &st, &oz);
    record[count].fly_fish=(((st*14)+lbs)*16)+oz;
    /* Creates competitor number, initialises it as 1, then increments by 1 each time */
    record[count].comp_number = count +1;
    }
    
    printf("\n\nNAME competitor number river fishing sea fishing fly fishing total weight\n");
    printf("============================================= ===========================================\n");
    
    for(count = 0; count <= (n-1); count++){
    print_entry(&record[count]);
    }
    }
    Many thanks x

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Since you have the number of entries (n) in main(), I would declare the function with two parameters (if you are allowed, which maybe from the MUST you are not):
    Code:
    void sort(struct competitor *entry, int n)
    Anyway, if there are exactly eight entries, I guess you could hard code that, and just use one parameter, but the above is better.

    Then do the sort you've been taught. You can access the array just like you did in your original main() before you modified it with the print() function. After you're done, in main() you can use the same print() function to print the entries again, or do the sort() before you print.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also to conform with the latest standard, main should be declared like this:
    Code:
    int main(void)
    And you should always return a value at the end of main:
    Code:
    return 0;
    Or:
    Code:
    return EXIT_SUCCESS;

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

    Thankyou

    Thankyou for all your help, its all done and working now xxx

  7. #7
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Smile Solved!!!!!!!!!!!

    hey robsmith
    i debugged your program in a Turbo C compiler
    nice program
    i introduced a string in the structure
    and made the function return a value.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<conio.h>
    
    /* Field Definitions */
      typedef struct competitor{
        char name[10];
        int comp_number;
        int river_fish;
        int sea_fish;
        int fly_fish;
      } data	;
    
    main()
    {
      int n, count, lbs, st, oz, x = 0; data *record;
      clrscr();
      printf("\n************************ Welcome To The Fishing Competition Program! *****************************\n\n");
    
      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("\nPlease enter name (separated by underscores) of competitor # %d:\n", count+1);
        scanf("%s", &record[count].name);
        printf("\nPlease enter weight of Fish from the River Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
        scanf("%d %d %d", &lbs, &st, &oz);
        record[count].river_fish = (((st*14)+lbs)*16)+oz;
    
        printf("\nPlease enter weight of Fish from the Sea Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
        scanf("%d %d %d", &lbs, &st, &oz);
        record[count].sea_fish = (((st*14)+lbs)*16)+oz;
    
        printf("\nPlease enter weight of Fish from the Fly Competition (Stones Pounds Ounces) for competitor # %d:\n", count+1);
        scanf("%d %d %d", &lbs, &st, &oz);
        record[count].fly_fish=(((st*14)+lbs)*16)+oz;
        /* Creates competitor number, initialises it as 1, then increments by 1 each time */
        record[count].comp_number = count +1;
      }
    
      for(count = 0; count <= (n-1); count++){
    	 printf("\n=====================================\n\n");
    	 printf("Name: %s\n", &record[count].name);
    	 printf("Competitor Number: %d\n", record[count].comp_number);
    	 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);
    
    }
    getch();
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  3. Newbie Pointer question
    By kook44 in forum C++ Programming
    Replies: 5
    Last Post: 01-02-2006, 01:07 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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