-
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.
-
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
-
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.
-
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.