digitalmars.D.learn - check Input
- dennis (5/5) Apr 17 2017 Hi,
- Nafees (30/36) Apr 17 2017 How I would do it:
- Nicholas Wilson (6/12) Apr 17 2017 I will point you to Ali's book (free), it goes through the basics
- dennis (1/1) Apr 17 2017 thank you all! i will try
Hi, try to build a little programm, but need to know how to check the input. For example: input only numbers: 0 - 9 but 1.5 for example is ok. thanks
Apr 17 2017
On Monday, 17 April 2017 at 11:51:45 UTC, dennis wrote:Hi, try to build a little programm, but need to know how to check the input. For example: input only numbers: 0 - 9 but 1.5 for example is ok. thanksHow I would do it: Run a loop, checking if the characters in input are within the range (i.e 0-9 and '.' character) <code> import std.stdio; import std.conv : to; import std.algorithm : canFind; private bool isNum(string s){ bool r=true; uinteger i; for (i=0;i<s.length;i++){ if (!"0123456789.".canFind(s[i])){ r = false; break; } } return r; } void main(){ string input = readln; if (input.isNum){ float f = to!float(input); if (f > 0 && f < 9){ writeln("Number is in range"); }else{ writeln("Out of range"); } } }
Apr 17 2017
On Monday, 17 April 2017 at 11:51:45 UTC, dennis wrote:Hi, try to build a little programm, but need to know how to check the input. For example: input only numbers: 0 - 9 but 1.5 for example is ok. thanksI will point you to Ali's book (free), it goes through the basics of input and output. http://ddili.org/ders/d.en/index.html It is better for you to discover, however don't be afraid to ask if you get stuck.
Apr 17 2017