Thread: problem with gets

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    1

    Question problem with gets

    hi
    i wroth this program :
    as soon as i change gets to scanf in /*/line it works properly otherwise it does not work!!
    i woner why??
    the problem is with the second gets as if it does not works
    i have to use gets because i need to read in a string instead of a word!
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    typedef struct{
    char name[30];
    float mark;
    }person;

    main(){

    person m1;
    person n1;
    int flag=0,counter=0;

    puts("enter the first name");
    gets(m1.name);
    puts("enter the correspondence mark");
    flag=scanf("%f",&m1.mark);

    while (flag!=EOF){
    counter++;
    printf("enter the next name\n");
    /*/ gets(n1.name);
    printf("enter the correspondence mark\n");
    flag=scanf("%f",&n1.mark);
    if(n1.mark>m1.mark)
    m1=n1;
    }
    if(counter)
    printf("the best student is %s and his mark is %.2f",m1.name,m1.mark);
    else
    puts("you did not enter any record");
    }
    Last edited by bsimaie; 04-16-2003 at 09:13 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    For God's sake use code tags!

    What doesn't work?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    because your previous scanf() more than likely leaves a newline character. scanf and gets have problems with each other, you should only really use one, and if you choose to use gets, use fgets() so you dont have buffer overflow. Read the faq.
    The keyboard is the standard device used to cause computer errors!

  4. #4
    .........
    Join Date
    Nov 2002
    Posts
    303
    What stumon said, your compiler should warn you about gets, I know mine does. This is what it says when I try to use gets lol.
    Code:
    In function `main':
    : the `gets' function is dangerous and should not be used.

  5. #5
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Yea, this is what mine says:
    Code:
    In function 'main':
    : Don't f'n use that damn 'gets' function or I [the compiler] will crash your computer.
    j/k
    The keyboard is the standard device used to cause computer errors!

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    stumon.. i think that is what all compiler should say about gets and puts.

    And do it. But then we do have an example of that all ready (read windows)

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's nothing wrong with puts. It simply sends a string to the standard output device with a trailing newline. There are no buffer related bugs (or any realy for that matter).

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    I know I just dont like puts().

    Its just that I got used to the various fputs() and such rather then the generic puts().

    That way you know where its going anyways.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by squid
    I know I just dont like puts().

    Its just that I got used to the various fputs() and such rather then the generic puts().

    That way you know where its going anyways.
    Agreed, however, I was just was clarifying for those who aren't aware. It's best not to mislead, even if unintentional.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    Ahh Ok.. Didnt think of that.. Work is killing my brain. Hopefully I will get them to give me a compiler soon. I told them Either I will need perl or Visual C++ for this DB project Im working on.

    Well a website/DB/Message Board/Make my managers feel all funny in side

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM