My programs issues

pascal111 judas.the.messiah.111 at gmail.com
Wed Aug 10 14:08:59 UTC 2022


On Wednesday, 10 August 2022 at 14:03:53 UTC, Andrey Zherikov 
wrote:
> On Wednesday, 10 August 2022 at 13:34:53 UTC, pascal111 wrote:
>> So, the program will be like this:
>
> Is this clearer?
>
> ```d
>
> import std.stdio;
> import std.algorithm;
> import std.range;
>
> double getNumber()
> {
>     double x;
>
>     write("Enter a number: ");
>     readf(" %s\n", &x);
>     writeln;
>
>     return x;
> }
>
> int getChoice()
> {
>     while(true)
>     {
>         writeln("1) addition 2) subtraction 3) multiplication 
> 4) division 0) exit -1) new");
>         ": ".write;
>
>         int choice;
>         readf(" %s\n", &choice);
>
>         if(only(1,2,3,4,0,-1).canFind(choice))
>             return choice;
>     }
> }
>
> int main()
> {
>     double x, y;
>
>     y = getNumber();
>
>     while(true)
>     {
>         int choice = getChoice();
>
>         if(choice==0)
>             return 0;
>         else if(choice==-1)
> 			y = getNumber();
>         else
>         {
>             write("Enter a number: ");
>             readf(" %s\n", &x);
>
>             switch(choice){
>
>                 case 1: y+=x; break;
>                 case 2: y-=x; break;
>                 case 3: y*=x; break;
>                 case 4: y/=x; break;
>                 default: break;}
>
>             y.writeln;
>         }
>     }
> }
> ```

This version has modern features, it's 1) functional 2) goto-less.


More information about the Digitalmars-d-learn mailing list