T
Tarique
Hello All.
Is the following good enough to be a safe user input routine?
What else should i do to improve it ?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ARRAYSIZE 100
/*Clear the input stream before any input */
int flushln(FILE *f) {
int ch;
while (('\n' != (ch = getc(f))) && (EOF != ch))
continue;
return ch;
}
char* get_data( void )
{
static char buffer[ARRAYSIZE];
printf("Enter Data : ");
/*flushln(stdin);*/
while( fgets( buffer , sizeof buffer , stdin ) == NULL )
{
puts("Enter a data");
flushln(stdin);
fgets( buffer , sizeof buffer , stdin );
}
buffer[ strlen(buffer)-1 ] = '\0';
return buffer;
}
int main(void)
{
int iValue;
float fValue;
double dValue;
char *cValue;
iValue = atoi( get_data() );
fValue = (float)atof( get_data() );
dValue = atof( get_data() );
cValue = get_data();
printf( "Integer :%d \n" , iValue );
printf( "Float :%f \n" , fValue );
printf( "Double :%f \n" , dValue );
printf( "String :%s \n" , cValue );
return EXIT_SUCCESS;
}
Is the following good enough to be a safe user input routine?
What else should i do to improve it ?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ARRAYSIZE 100
/*Clear the input stream before any input */
int flushln(FILE *f) {
int ch;
while (('\n' != (ch = getc(f))) && (EOF != ch))
continue;
return ch;
}
char* get_data( void )
{
static char buffer[ARRAYSIZE];
printf("Enter Data : ");
/*flushln(stdin);*/
while( fgets( buffer , sizeof buffer , stdin ) == NULL )
{
puts("Enter a data");
flushln(stdin);
fgets( buffer , sizeof buffer , stdin );
}
buffer[ strlen(buffer)-1 ] = '\0';
return buffer;
}
int main(void)
{
int iValue;
float fValue;
double dValue;
char *cValue;
iValue = atoi( get_data() );
fValue = (float)atof( get_data() );
dValue = atof( get_data() );
cValue = get_data();
printf( "Integer :%d \n" , iValue );
printf( "Float :%f \n" , fValue );
printf( "Double :%f \n" , dValue );
printf( "String :%s \n" , cValue );
return EXIT_SUCCESS;
}