-
Data validation
I'm writting a progrm in which I want to verify the user doesn't input incoherent data. When I code this:
Code:
#include<stdio.h>;
#include<conio.h>;
main(){
float var1;
clrscr();
scanf("%f",&var1);
printf("\n\n%f",var1);
}
What happens is, if the user enters a valid float value I get the same float value on the output (duh). If he enters a character or a string I always get the same output whatever the input, that is: 149709439917360000.0000 . My question is, how does the computer get this value? Does it convert the characters into ASCII and multiply them or what? :confused: Thank you...
-
scanf() will convert all keys typed to float until the first non-digit (and +/- keys) key is typed. So if you type "12.34abc", scanf() will stop converting when it sees the letter 'a' was typed. The rest of the keys remain in the keyboard.
-
One half there.
Ok, so if I type 12.34abc var1 gets the value 12.34. Check. Now, if I type in abc12.34 how does var1 come to get the value 1443268871235100000.0000 whatever....?
-
In that case, scanf() will not change the value of var1 -- the number you posted appears because you didn't initialize the varaible with anything. Set it to 0 when declared and you will see the difference. YOu also should check the return value of scanf() to see how many variables were processed or if there was a conversion error. In your example, scanf will most likely return 0 because no variables were converted.
-
You shouldn't have semicolons after #includes.