Structure Problem, Athletes in Daner!!!

This is a discussion on Structure Problem, Athletes in Daner!!! within the C++ Programming forums, part of the General Programming Boards category; We have a structure with the following data Code: struct athlete { char lastname[40]; float p1,p2,p3,p4; }; that corespond to ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    8

    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. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    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.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    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. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    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.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    No we haven't done vectors and i don't think we will ^^ (Oi apergies ftaine :P)
    Any other way to do it?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then you may have to settle for an array of structs, assuming you know how many there are going to be.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,545
    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).
    Last edited by Elysia; 12-20-2010 at 09:18 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Quote Originally Posted by Sipher View Post
    Your implementation of find_athlete is ok, but it won't work for you.
    Then what is so OK about it?

    Quote Originally Posted by stam View Post
    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. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,624
    Quote Originally Posted by stam View Post
    Oi apergies ftaine :P
    Ελληνάρας, έ; Χαίρομαι που σ' αυτό το φόρουμ γινόμαστε πολλοί. Προσπάθησα τόσες φορές να σου στείλω προσωπικό αλλά δεν θέλει! Εδώ όλοι με έχουν βοηθήσει πάρα πολύ, δεν θα το μετανιώσεις αν μείνεις!

    Sorry, but i wanted to talk to him and neither his visitor nor private messages work!
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    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 subscribe to a feed

Similar Threads

  1. poiner to structure problem
    By bluetxxth in forum C Programming
    Replies: 9
    Last Post: 02-25-2010, 03:56 PM
  2. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 09:53 AM
  3. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 02:30 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 01:53 PM

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