www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - check Input

reply dennis <dennisschneck web.de> writes:
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
next sibling parent Nafees <nafees.hassan outlook.com> writes:
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.

 thanks
How 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
prev sibling parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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.

 thanks
I 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
parent dennis <dennisschneck web.de> writes:
thank you all! i will try
Apr 17 2017