![]() |
|
#1
|
|||
|
|||
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:
Last edited by LuciWiz : 10-Mar-2005 at 13:53.
Reason: Please insert your C code between [c] & [/c] tags
|
|||
|
#2
|
|||
|
|||
|
One mistake i found immediately is in you typedef. Name is defined as a single character instead of char array.
CPP / C++ / C Code:
should be CPP / C++ / C Code:
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
|
|||
|
|||
|
Quote:
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
|
|||
|
|||
ThankyouMany 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:
Last edited by LuciWiz : 12-Mar-2005 at 04:37.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#5
|
||||
|
||||
|
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
|
|||
|
|||
ThankyouThankyou for all your help, its all done and working now :-P xxx
![]() |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Newbie problem with Apache | hellhammer | Apache Web Server Forum | 4 | 30-Aug-2004 09:01 |
| newbie problem with RegSetValueEx | w332 | C++ Forum | 2 | 16-Jul-2004 16:06 |
| newbie with pointer problems. | moltarim | C Programming Language | 1 | 14-May-2004 10:46 |
| Newbie Problem with cin | trs2988 | C++ Forum | 4 | 08-Feb-2004 13:05 |
| Code problem (a newbie question) | monkster87 | C++ Forum | 3 | 11-Aug-2003 13:46 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The