Thread: not getting

  1. #1
    Registered User
    Join Date
    Oct 2011

    not getting

    void createaddlevel(struct mapdescription level[],struct mapdescription interests[],struct mapdescription item[],struct mapdescription itemd[])
    { int i,choice,n,j,k,choice2,choice3;
        printf("Which do you want to create?");
    scanf("%d", &choice);
    {printf("Please input the level");
    {printf("Please input the level description");
    my code is not using the gets properly it just prints please input level and prints please input the level description.
    Last edited by needhelp1; 10-28-2011 at 07:38 AM. Reason: not clear

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well you should probably read this first. Indentation - cpwiki

    Then perhaps people will want to read your code, as opposed to clicking 'x' and moving onto the next post.

    Next on the list would be Gets - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    RTFM FAQ > Why gets() is bad / Buffer Overflows

    1. Indent your code properly.
    2. Describe what the intended behaviour of the code is, and what the actual behaviour of the code is.
    3. Use proper variable names and dont use magic-numbers.
    4. Try to write easy-to-read transparent code, stuff like this:

    is just utterly horrible.

    5. When you have done all of the above, come back and ask again.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #4
    Registered User
    Join Date
    Sep 2006
    On the input the level block of code, I don't understand why you're testing for a strlen() there, since the user is about to overwrite whatever was there before.

    No strlen() test, should be needed, yes? But there are 10 sub levels, and they some might have strings already in there?

    fgets(charArrayName, length of charArrayName-1, stdin);

    is a MUCH safer input function to use than gets(). With gets(), in a compiled language like C, you're just asking for trouble from the user - maybe innocently, maybe not. Either way, it can crash your program at the least, and do massive harm including taking total control of the system perhaps, at it's worst.

    You do have to remove the newline that fgets adds to the string, which is a bit of a nuisance sometimes, but it's so MUCH better that you have to use it. Makes no sense to leave your system open to a malicious user.
    Last edited by Adak; 10-28-2011 at 08:17 AM.

Popular pages Recent additions subscribe to a feed