Hello,
I would like to ask how could I check if the input is right:
the program asks user to input a float from a keybord, but user inputs a char. Is there any funcion which can check that?
Thanks in advance.
Hello,
I would like to ask how could I check if the input is right:
the program asks user to input a float from a keybord, but user inputs a char. Is there any funcion which can check that?
Thanks in advance.
The most common way is to read a string with fgets, then convert to float using strtod, and check the end pointer argument to see where the function stopped parsing. If it stopped parsing right away (the start is the same as the end), then it means it's an invalid input.
You can see documentation for strtod.
Thanks for an idea, but therefore I've faced another problem. I decided to use isdigit to check if the inputed number is not a char and if it isn't-then convert it with atof or atoi.
But then i started to use these funcions, the for loops began working incorrectly: they are being repeated more times than the num value is, for example if num=3, each loop is repeated10 times and so on.
Do you have an idea how could I correct this to work properly?
Code:char t; for(i=0;i<num;i++) { char* p; printf("Enter string nr.%d\n",i+1); fgets(string[i], sizeof string[i], stdin); p = strchr(string[i],'\n'); if(p) *p = '\0'; } for(i=0;i<num;i++) { printf("Enter float nr.%d \n",i+1); fgets(&t, sizeof &t, stdin); if (isdigit(t)) floatt[i]=atof(&t); else {printf("Error");return 0;} } for(i=0;i<num;i++) { printf("Enter integer nr.%d \n",i+1); fgets(&t, sizeof &t, stdin); if (isdigit(t)) integer[i]=atoi(&t); else {printf("Error");return 0;} }
Last edited by CactusC; 04-07-2008 at 02:53 AM.
You are aware that there are valid floating point numbers that do not start with a digit:
Above are three examples of such numbers.Code:+1.2 -1.2 .01
By using strtod(), you can make a better check if the number is valid (and completely accepted - you then can decide if "123.4A" is a valid number or not).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I don't know how string is defined, but this is absolutely wrong:
When working with C-strings, you need arrays, not a single char. Never. All C-strings contains one byte for each character + one character for NULL. So when reading that char, fgets write the char + NULL, thus buffer overrun. The same for atoi - it also expects a NULL char, and does a buffer overrun looking for it.Code:fgets(&t, sizeof &t, stdin); if (isdigit(t)) floatt[i]=atof(&t);
http://cpwiki.sf.net/Buffer_overrun
But you seriously need some indenting lessons too. Here are some reasources:
http://cpwiki.sf.net/Indentation
http://cpwiki.sf.net/User:Elysia/Indentation
Than you. I've changed
toCode:char t;
and nows everything is working in a right wayCode:char t[100];
I would like to make a program, which does smth when one of the arrows (of keyboard) is pressed. So I would like to ask which funcion should I use (scanf, getch...?) to get the pressed arrow.
I did it.
How? What compiler are you using? If you are using Dev-C++, did you use getche()? (Just out of curiosity.)
By the way, that smiley that you keep using ( ) is not used when asking for help. Actually, it indicates sarcasm, which usually means you're annoyed or something -- which I don't think is what you want to convey.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
I'm using lcc-win32.
http://support.microsoft.com/kb/57888
P.S. Sorry for the smiley, it has a different meaning in our country