This is simple program in C that show you how to create function input(scanf) to avoid an error when user input information:
Source Code
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<conio.h>
float inputf(){
int i=0,f=-1;
char result[50]="",key;
_setcursortype(2);
do{
key=getch();
if(isdigit(key)||(key=='.'&&f==-1)){
if(key=='.')
f=i;
result[i++]=key;
printf("%c",key);
}
if(key==8&&i>0){
result[--i]='\0';
if(i==f)
f=-1;
printf("\b \b");
}
}while((key!=13||i==0));
_setcursortype(0);
return atof(result);
}
void main(){
float number;
clrscr();
printf("Input:");
number=inputf();
gotoxy(34,13);printf("%f",number);
getch();
}
#include<ctype.h>
#include<stdlib.h>
#include<conio.h>
float inputf(){
int i=0,f=-1;
char result[50]="",key;
_setcursortype(2);
do{
key=getch();
if(isdigit(key)||(key=='.'&&f==-1)){
if(key=='.')
f=i;
result[i++]=key;
printf("%c",key);
}
if(key==8&&i>0){
result[--i]='\0';
if(i==f)
f=-1;
printf("\b \b");
}
}while((key!=13||i==0));
_setcursortype(0);
return atof(result);
}
void main(){
float number;
clrscr();
printf("Input:");
number=inputf();
gotoxy(34,13);printf("%f",number);
getch();
}
0 comments:
Post a Comment