Thread: not getting

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    not getting

    Code:
    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);
    
    
    if(choice==1)
    {printf("Please input the level");
    for(i=0;i<10;i++);
    if(strlen(level[0].level[i].levelname)==0)
    {gets(level[0].level[i].levelname);
    i+=12;
    }
    
    
    
    
    
    
    {printf("Please input the level description");
    for(i=0;i<10;i++)
    if(strlen(level[0].level[i].leveldescription)==0)
    {gets(level[0].level[i].leveldescription);
    i+=12;
    }
    }
    }
    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
    Location
    The edge of the known universe
    Posts
    39,659
    Well you should probably read this first.
    SourceForge.net: 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
    SourceForge.net: 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
    Location
    Denmark
    Posts
    964
    RTFM

    Cprogramming.com 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:

    Code:
    for(k=(8*choice3)-8;k<(8*choice3)-1;k++)
    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
    Posts
    8,868
    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