Thread: Crazy stuff can happen :S

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

    Crazy stuff can happen :S

    Ok the program is done but when it runs and u start give what it asks
    Example:

    How many athletes; 2 (we give 2)
    ***Give Athletes Data:
    Lastname: Messi
    First Jump: 5.6
    etc....

    Instead of the above it asks for:

    How many athletes; 2 (we give 2)
    ***Give Athletes Data:
    Lastname: First Jump: 5.6
    etc....
    IT SKIPS the first lastname but it asks u for the second properly
    What the heck is that ^^




    Code:
    #include <cstdlib> 
    #include <iostream> 
    
    using namespace std; 
    struct athlete 
    { 
        char lastname[40]; 
        float p1,p2,p3,p4; 
    }; 
    
    
    void show_athletes(athlete A[], int MAX_ATHLETES); 
    int main(int argc, char *argv[]){ 
        int MAX_ATHLETES;
        cout << "How many Athletes; ";
        cin >> MAX_ATHLETES;
        athlete* A = new athlete[MAX_ATHLETES];
        int i;
        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,MAX_ATHLETES); 
         system("PAUSE"); 
        return EXIT_SUCCESS; 
    } 
    void show_athletes(athlete A[],int MAX_ATHLETES){  
        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);
              printf("Ta almata tou einai:\n%f\n%f\n%f\n%f\n",A[i].p1,A[i].p2,A[i].p3,A[i].p4); }
        } 
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're including iostream, then you don't have scanf and printf and gets, you have >> and << and getline, so this shouldn't (strictly) be compiling at all. Use << and >> and getline.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    8
    i just changed the
    Code:
    gets(A[i].lastname)
    with
    Code:
    cin >> A[i].lastname
    and worked perfectly
    Thnx for help

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int main(int argc, char *argv[]){ 
        int MAX_ATHLETES;
        cout << "How many Athletes; ";
        cin >> MAX_ATHLETES;
        athlete* A = new athlete[MAX_ATHLETES];
        int i;
        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);
    What is happening is the cin >>MAX_ATHLETES call reads the number and stores it but leaves the newline character in the input stream. When you reach the gets call, it sees the remaining newline and interprets this as the user (you) pressing the enter key to terminate input. The newline char is consumed by the gets call leaving the input stream now empty and storing nothing in the lastname field and the program proceeds past this point which you instead expected it to stop at. The subsequent scanf call blocks (waits for input) until a value is entered and the newline is once again pressed.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Excellent explanation.

    i have had this annoying problem before when doing C last year .

    you have to use getchar() or getch() to remove the newline left in the input stream

    Code:
           while(getchar()!='\n') ; // will flush the buffer as opposed to fflush(stdin)(wrong!)
    works as well.
    You ended that sentence with a preposition...Bastard!

  6. #6
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    @up - if you're talking about the "getch()" from conio.h (old Borland header), then it will not read the character from the standard input stream. It will wait for the "real" keystroke using system dependent features.

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    oh right! Thanks for letting me know! :P
    You ended that sentence with a preposition...Bastard!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    gets should be std::getline, printf should be std::cout, scanf should be std::cin.
    You should stick to either C or C++, not a mix of both.
    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.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    And of course...
    Never use gets under any circumstances. Use fgets instead.

    (which Elysia would normally tell you about, but forgot today)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Quote Originally Posted by Elysia View Post
    gets should be std::getline, printf should be std::cout, scanf should be std::cin.
    You should stick to either C or C++, not a mix of both.
    Amen... I don't understand why everyone insists on using these complex, time-consuming processes when you can simply use
    Code:
    cout << "Last name: ";
    cin >> lastname;
    cout << "First Jump: ";
    cin >> firstjump;
    and etc

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    amen nada, its called backward compatability mate, and though i cant neccesarialy include myself, knowledge of the rawer roots of the language lend insight and understanding- walking before running, if you know latin you can get a handle on many modern languages- backward compatability.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  12. #12
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    i learnt C first, and sometimes you can't automatically help but type a printf statement..
    I don't know whether it is my lack of experience with C++ but there are times where I have no choice but to use C code.
    You ended that sentence with a preposition...Bastard!

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    slainte eman, all the best. my point is though elysia speaks with authority, and champions c++ due to that depth of knowledge and familiarity with the idiom-so amen wot?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  14. #14
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    haha, Slainte?
    well done on your research, lol!
    Hopefully I gain that same amount of depth..which seems rather bleak at this stage
    You ended that sentence with a preposition...Bastard!

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Eman View Post
    i learnt C first, and sometimes you can't automatically help but type a printf statement..
    I don't know whether it is my lack of experience with C++ but there are times where I have no choice but to use C code.
    This is understandable. But as you continue learning a new language, it will grow on you and you will be able to forget those old C horrible, unsecure functions. Just keep practicing!

    Quote Originally Posted by iMalc View Post
    (which Elysia would normally tell you about, but forgot today)
    Actually, I would had this been C, but it isn't. There is no reason to use gets/fgets in C++ (unless you have a very particular requirement, but I assume this isn't the case here).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What would happen on the job?
    By Syscal in forum C Programming
    Replies: 15
    Last Post: 09-07-2010, 06:25 PM
  2. magic could happen on recvfrom
    By -EquinoX- in forum C Programming
    Replies: 22
    Last Post: 03-27-2009, 06:06 PM
  3. Is what I think is gonna happen gonna happen
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2009, 10:34 PM
  4. crazy
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 08-31-2002, 08:54 PM
  5. Your stuff
    By smog890 in forum C Programming
    Replies: 6
    Last Post: 06-13-2002, 11:50 PM