GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Mar-2005, 12:33
robsmith robsmith is offline
New Member
 
Join Date: Mar 2005
Posts: 11
robsmith is on a distinguished road
Unhappy

Please help! Newbie pointer problem!


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:

CPP / C++ / C 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);
}
}
Last edited by LuciWiz : 10-Mar-2005 at 12:53. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 10-Mar-2005, 12:57
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
One mistake i found immediately is in you typedef. Name is defined as a single character instead of char array.

CPP / C++ / C Code:
typedef struct competitor{
    char name;           <<<<<<<<<<Incorrect
    int comp_number;
    int river_fish;
    int sea_fish;
    int fly_fish;
  } data  

should be

CPP / C++ / C Code:
typedef struct competitor{
    char name[50];        <<<<<<<<< Correct
    int comp_number;
    int river_fish;
    int sea_fish;
    int fly_fish;
  } data  

Well, youare already doing a printing job there. All you need to do is move it in a function and pass the stuffed structure to that function as an argument.
  #3  
Old 10-Mar-2005, 13:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,627
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by robsmith
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

..

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

In testing your program, in addition to name mangling caused by the mistake that nkhambel pointed out, did you notice that you get the wrong answer?

What is the total number of ounces for 4 stone 5 pound 6 ounces? What does your program say?

Now, if you want to be able to print out the total weight in stones, pounds and ounces for each category, there are a couple of ways of doing it:

1. Make your competitor struct have separate members for stones, pounds, ounces.

If you aren't allowed to change the struct (or you don't want to), then you can do this:

2. Take the total number of ounces (after you fix the bug so that you can calculate it correctly) and use a combination of division and modulus operations to extract the stone, pound, and ounce quantities.

This is more elegant, makes better use of computer storage (as if that could be an issue), and can teach you a little about integer arithmetic in C.



Regards,

Dave
  #4  
Old 10-Mar-2005, 15:12
robsmith robsmith is offline
New Member
 
Join Date: Mar 2005
Posts: 11
robsmith is on a distinguished road
Thumbs up

Thankyou


Many thanks for all your help, you were incredibly helpful!

The program is now working effectively and outputting the weight converted back into stones, pounds and ounces.
Now I need 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:

CPP / C++ / C 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]);
  }
}
Last edited by LuciWiz : 12-Mar-2005 at 03:37. Reason: Please insert your C code between [c] & [/c] tags
  #5  
Old 11-Mar-2005, 23:01
dabigmooish's Avatar
dabigmooish dabigmooish is offline
Member
 
Join Date: May 2004
Location: Baltimore (middle of Canton)
Posts: 165
dabigmooish will become famous soon enough
First when you post code please put it between [ c ] and [ /c ] tags (minus the spaces) that makes it much easier to read for everyone. Now then, If I understand what your asking, you need to Sort the array of struct's into order with the first element being the fisherman who caught the most lb's of fish and the last being the fisherman with the least lb's of fish. This is actually much easier then it sounds. All you have to do is run your array through a sorting algorithm. Here's a good article on arrays. Sorting/searching is at that bottom. Just make two temp variables, one to hold array[0].river_fish + array[0].sea_fish + array[0].fly_fish and the other to hold array[1].river_fish + array[1].sea_fish + array[1].fly_fish. Then use those temparary variables to sort through the array. Post back if you still need some help.
__________________
"To argue with a person who has renounced the use of reason is like administering medicine to the dead."
-Thomas Paine
www.sullivan-county.com/deism.htm
  #6  
Old 12-Mar-2005, 04:48
robsmith robsmith is offline
New Member
 
Join Date: Mar 2005
Posts: 11
robsmith is on a distinguished road

Thankyou


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

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie problem with Apache hellhammer Apache Web Server Forum 4 30-Aug-2004 08:01
newbie problem with RegSetValueEx w332 CPP / C++ Forum 2 16-Jul-2004 15:06
newbie with pointer problems. moltarim C Programming Language 1 14-May-2004 09:46
Newbie Problem with cin trs2988 CPP / C++ Forum 4 08-Feb-2004 12:05
Code problem (a newbie question) monkster87 CPP / C++ Forum 3 11-Aug-2003 12:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:01.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.