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