?
2009-12-12 22:51:36 UTC
int x=0,Input=0;
char *Data="123456789";
char *Debug=malloc(Getstrlen(Debug)*1000); //Debugger variable
fprintf(stdout,"Enter an integer value:"); //Request input
fscanf(stdin,"%d",&Input);
//code checks for invalid input.
for(x;x<=Getstrlen(Data);) //int Getstrlen(char *String) return the length of Data.
{
if(Input==Data[x])
{
gets(Debug);//catch input overflow.
free(Debug);
system("cls"); //clears screen(not relevent to the question)
//Go back to request input.
}
x++;
}
//End sample code
Ok,if you've ever written C code, at some point you'd have liked or wanted to validate a user's input instead of asking a user to not enter something invalid(because its embarrasing and sucks).You'll know theres no input validation built into the C language and if you enter an alphabeth/character instead of an integer your program will crap out.Theres just no way to check if a user actually entered a character.Using functions from ctype.h in an if statement is no use.So,out of curiousity and love for the lang,I decided to brainstorm some solutions for this.Suffice to say,the above code actually works(Test it and use it in your apps if you want)YaY :D.Problem is:I dont know/remember why.Now,I know its pretty stupid and laughable to ask other coders why your own code/solution works but I need help understanding me own code:P. Please dont flame me for not commenting enough.LOL. Thanks in advance.