+ 1

Can someone help me with a simple C request?

I'm trying to make a simple "short a, b;" code where you enter two numbers and the program tells you what you entered. Quite simple, but I can't make a "fail safe". I want the program to make a "printf: you don't respect the rules" if someone entered characters instead of numbers. I didn't succeed with if / else and I'm looking for help here.

19th Jan 2020, 3:34 PM
Vadean Alex
Vadean Alex - avatar
7 ответов
+ 4
Best maybe then to take the input as string and convert it to real numbers later. Then you can do tests based on the ASCII order of letters (google that), for example: for(int i=o; my_string[i]; ++i) if(my_string[i]<'0' || my_string[i]>'9') printf("Only numbers please!") ;
19th Jan 2020, 3:48 PM
HonFu
HonFu - avatar
+ 3
Vadean Alex there is no short for floating point values.... There are only three ways for decalaring floating point value i.e., float,double and long double
21st Jan 2020, 12:14 PM
Roopam Maurya
Roopam Maurya - avatar
+ 2
Ah! I see. Thank you very much 💞
19th Jan 2020, 4:18 PM
Vadean Alex
Vadean Alex - avatar
+ 2
It worked! Thanks man. Feeling quite confident that I managed to make a simple calculator. Can't wait to see what else I can create. Thank you all for your help.
20th Jan 2020, 2:51 PM
Vadean Alex
Vadean Alex - avatar
+ 1
Martin Taylor, do you think you can give a small example of the code with your method? I tried it, but it doesn't work and I think it's because I'm working with float numbers, but not sure..
19th Jan 2020, 11:57 PM
Vadean Alex
Vadean Alex - avatar