# Thread: Structure Problem, Athletes in Daner!!!

1. ## Structure Problem, Athletes in Daner!!!

We have a structure with the following data
Code:
```struct athlete
{
char lastname[40];
float p1,p2,p3,p4;
};```
that corespond to athletes of "Long Jump" that have been done in a competiton.
You must write a program that reads from the keyboard their last name and how far they jumped and save them in a struct.After that the user must be able to print the athletes that jumped more than 7,50(meters).
That insertion of data and the print of them must be done with functions.
The print must include the athlete and all his jumps.
Attention!
If we give negative number or number bigger than 9.50(meters) MUST NOT be accepted
and we repeat the insertion until we have 4 jumps.We accept 0(zero) as a number and means that the jump has been done but was Wrong(failed).

I did my best to translate it, i hope u guys can understand what must be done
I just can't understand structures...
if u know any god tutorial that includes the things i have to do would be great.
I need help fast with this
if u can't understand something let me know

2. What don't you understand? p1, p2, p3, p4 are the four attempts of the athlete.

All the other problem's conditions are pretty straightforward.

3. Here is what i have done so far...
Beacuse i couldnt put the read section in fuction i leave it in main.(I need to functionize it.. HELP!!!)
I did a search (i think :P) in a function that read all the Jumps and it suppose to call it in main and print the Athletes with jumps longer than 7.50
But can't make it work...
And last i have to make in "if" if someone gives negative jump.

Code:
```#include <cstdlib>
#include <iostream>
#define MAX_ATHLETES 4

using namespace std;
struct athlete
{
char lastname[40];
float p1,p2,p3,p4;
};

athlete* find_athlete(athlete A[],float p1,p2,p3,p4);
int main(int argc, char *argv[])
{
int i;
athlete A[MAX_ATHLETES];
athlete* ptr;
for(i=0;i<MAX_ATHLETES;i++){
fflush(stdin);
printf("\n*** Give Athletes Data: \n");
printf("Lastname: ");
gets(A[i].lastname);
printf("First Jump: ");
scanf("%f",&A[i].p1);
printf("Second Jump: ");
scanf("%f",&A[i].p2);
printf("Third Jump: ");
scanf("%f",&A[i].p3);
printf("Fourth Jump: ");
scanf("%f",&A[i].p4);
}
printf("Press Enter to Show Athletes with Jumps longer that 750");

system("PAUSE");
return EXIT_SUCCESS;
}
athlete* find_athlete(athlete A[],float p1,p2,p3,p4);
{
int i;
for(i=0;i<MAX_ATHLETES; i++){
if (A[i].p1,p2,p3,p4 > 750)
return &A[i];
}
return NULL;
}```

4. Your implementation of find_athlete is ok, but it won't work for you. Remember that you want to get all the athletes, not just one! You'll need an STL vector. Do you know how to use that?

Well, what are you waiting for? Add that if statement after each float input.

5. No we haven't done vectors and i don't think we will ^^ (Oi apergies ftaine :P)
Any other way to do it?

6. Then you may have to settle for an array of structs, assuming you know how many there are going to be.

7. NEVER USE GETS!
SourceForge.net: Gets - cpwiki
Don't flush stdin: SourceForge.net: Fflush - cpwiki

Now, what are you programming in? C or C++? This is pure C (well, almost).

8. Originally Posted by Sipher
Your implementation of find_athlete is ok, but it won't work for you.
Then what is so OK about it?

Originally Posted by stam
Code:
```#include <cstdlib>
#include <iostream>
#define MAX_ATHLETES 4

using namespace std;
struct athlete
{
char lastname[40];
float p1,p2,p3,p4;
};

athlete* find_athlete(athlete A[],float p1,p2,p3,p4);
{
int i;
for(i=0;i<MAX_ATHLETES; i++){
if (A[i].p1,p2,p3,p4 > 750)
return &A[i];
}
return NULL;
}```
OK so this line here
Code:
`       if (A[i].p1,p2,p3,p4 > 750)`
has the effect of comparing
Code:
`if (p4 > 750)`
which is definitely not enough. The comma operator is just a sequence point like ; it has no logical operation to compare anything to 750, or any other number. You want to make up a Boolean statement with one of the logical operators. Most likely AND (&&) or OR (||) depending on what you write.

To make sure that the attempts are in the range for a long jump, you should reject inputs that are outside the valid range. That makes the input more difficult, but you can handle it.
Code:
```while there are more attempts to do:
prompt for a jump
if attempt is in range, then:
assign the current attempt to the structure
move on to the next attempt
end while```
If you have to, break it down in English more until you understand what you need to write in C.

Also, read the FAQ on fflush(stdin);

9. Originally Posted by stam
Oi apergies ftaine :P
Ελληνάρας, έ; Χαίρομαι που σ' αυτό το φόρουμ γινόμαστε πολλοί. Προσπάθησα τόσες φορές να σου στείλω προσωπικό αλλά δεν θέλει! Εδώ όλοι με έχουν βοηθήσει πάρα πολύ, δεν θα το μετανιώσεις αν μείνεις!

Sorry, but i wanted to talk to him and neither his visitor nor private messages work!

10. Ok guys thnx for help, till now i have done this:

Code:
```#include <cstdlib>
#include <iostream>
#define MAX_ATHLETES 4

using namespace std;
struct athlete
{
char lastname[40];
float p1,p2,p3,p4;
};

void show_athletes(athlete A[]);
int main(int argc, char *argv[]){
int i;
athlete A[MAX_ATHLETES];
for(i=0;i<MAX_ATHLETES;i++){
printf("\n*** Give Athletes Data: \n");
printf("Lastname: ");
gets(A[i].lastname);
do{
printf("First Jump: ");
scanf("%f",&A[i].p1);getchar();
}while(A[i].p1<0);
do{
printf("Second Jump: ");
scanf("%f",&A[i].p2);getchar();
}while(A[i].p2<0);
do{
printf("Third Jump: ");
scanf("%f",&A[i].p3);getchar();
}while(A[i].p3<0);
do{
printf("Fourth Jump: ");
scanf("%f",&A[i].p4);getchar();
}while(A[i].p4<0);
}
printf("Press Enter to Show Athletes with Jumps longer than 7.50");
getchar();
show_athletes(A);
system("PAUSE");
return EXIT_SUCCESS;
}
void show_athletes(athlete A[]){
int i;
for(i=0;i<MAX_ATHLETES; i++){
if (A[i].p1>7.5 || A[i].p2>7.5 || A[i].p3>7.5 || A[i].p4>7.5)
printf("The Athlete:%40s Jumped longer than 7.50\n",A[i].lastname);
}
}```

It only show the Athletes that jumped longer than 750 without showing all there jumps...
Any help.

@Sipher
Twra pou to vrika ennoeite pws tha meinw :P

Popular pages Recent additions