where to get the user input in vs code? using dart - dart

I am building a dart program to get the user input. when running the program using the run button I get the output in the output window but I cannot type in it. tried using the terminal to run the program and it worked well. so I want to know what is the right way to take the input from the user?
import 'dart:io';
void main() {
print("enter your name: ");
var name = stdin.readLineSync();
print("hello mrs ${name}");
}

From file -> Preferences -> Settings
search for "Dart: Cli Console"
Dart: Cli Console
Then change the drop-down menu into -terminal-
now you can run again your code and check
you can type in the terminal and your code will work.

In fact the right way using terminal as you did. It's not a fail. It's just how it works.

The answer regarding using the correct vs-code console is correct but you should also check out the dcli package and it's ask function.
Disclaimer: I'm the author of dcli.

Related

dart-define not working when running a standalone Dart program

I have a single file Dart program - let's say main.dart. I'm trying to provide some compile time environment values to it using --dart-define=env=env_value but in the Dart program, I'm always getting the default values.
This is what my Dart program looks like
void main() {
const myValue = const String.fromEnvironment("MY_VALUE", defaultValue: "DEFAULT");
print('My value: $myValue'); // Always prints "DEFAULT"
}
This is the command I'm using to run my program
dart main.dart --dart-define=MY_VALUE=SOME_VALUE
Now, when I include the exact same code from above in a Flutter app and run it with the below command, everything seems to work as expecetd but for some reason the above program always prints DEFAULT as the output on console.
flutter run --dart-define=MY_VALUE=SOME_VALUE
Is there something I'm missing when it comes to providing these values in a Dart program? I'm running macOS if that helps in any way.
If you type:
dart --help --verbose
It will give you the list of supported flags.
Usage: dart [<vm-flags>] <dart-script-file> [<script-arguments>]
Executes the Dart script <dart-script-file> with the given list of <script-arguments>.
Supported options:
...
--define=<key>=<value> or -D<key>=<value>
Define an environment declaration. To specify multiple declarations,
use multiple instances of this option.
...
So it appears that the flag you want is --define or -D, rather than --dart-define. Also note that this is considered a "vm-flag" and must come before the file name in order to work.
Therefore the following command should work:
dart --define=MY_VALUE=SOME_VALUE main.dart

Running `.dart` files does not show logging messages

I just cannot figure out how to show logging messages when running dart files from terminal.
Example example.dart:
import 'dart:developer';
void main() {
print('print');
log('log');
}
Expected output:
print
log
Actual output:
print
I tried calling dart example.dart, dart run --all example.dart, dart run --verbosity=all example.dart, and different values instead of all (info, ...).
But non of these produced any helpful error messages let alone the out print I expect.
APIs from dart:developer (such as log) are intended to interact with debugging tools:
Interact with developer tools such as the debugger and inspector.
It's not explicit from the documentation for log, but I'd expect it to send a log message only to an attached debugger, not to the console.
If you want logging output that is independent of a debugger, use package:logging and add a listener that calls print, as shown by the example.

Debug DART output not printing

Output display user input integer, instead of Even or Odd
import 'dart:io';
void main() {
print('Enter your number: ');
int n = int.parse(stdin.readLineSync()!);
var result = n % 2 == 0 ? "Even" : "Odd";
print('Your number is : $result');
}
The problem is that your program is running inside the "Debug Console" in VS Code. The following explanation can be found in the Dart settings in VS Code:
The Debug Console has more functionality because the process is controlled by the debug adapter, but is unable to accept input from the user via stdin.
You can change this by going into File -> Preferences -> Settings. Here you go into Extensions -> Dart & Flutter. If you scroll down you can find "Dart: Cli Console". You can also just search for "Dart Cli console":
Instead of "debugConsole" set this to "terminal". Try start your program again and it should now be running inside the "Terminal" tab instead and you should be able to interact with your program and provide data to it though keyboard inputs.

"** exception error: undefined function add:addfunc/0 in Erlang "

I'm trying to execute a simple erlang program of adding two numbers.
I'm trying to do this in Eclipse on Ubuntu 10.04 LTS.
When i execute this program, I'm getting the error as shown below:
** exception error: undefined function add:addfunc/0
How do i go about solving this error? Thanks in advance.
This program when executed in the erlang shell is working fine. But when it comes to eclipse it's giving me this error. Not this, any program for that matter is giving me the similar error. Guess I would be missing something about the eclipse configuration.
EDIT:
Anyways, This is the sample add program,
-module(add).
-export([addfunc/0]).
addfunc() ->
5 + 6.
This message tells you that module add doesn't have an exported function addfunc/0.
Ensure the function you want to be called has exactly that name, doesn't expect any
parameters, is
exported, the module is
compiled, the search path includes the compiled beam file and that there is no module clashes using code:clash()
Update
It's not clear how erlide (eclipse erlang plug-in you seem to use) compiles and runs a program. Try to compile source using erlc or inside erl shell. That way you'll have much easier controllable environment and you'll better understand what's going on.
I got exactly the same problem -for a tail recursive fibonacci function- below:
-module(math2).
-export([fibonacci/1]).
fibonacci(0) -> 0;
fibonacci(1) -> 1;
fibonacci(M) -> fibonacci(M-1) + fibonacci(M-2).
In the end, had realized that this is a compile-time exception. Then, have opened a new tab on my shell and tried with erlc, instead of erl.
$ erlc math2.erl
Now I am also able to see math2.beam file created.
Called fibonacci with 10:
4> math2:fibonacci(10).
55
and it worked!
I think you have not compiled the code and you are trying to run the program.
In eclipse, using the "Run" icon, trigger the run; which will get you to the erl shell in the console window.
There you do -
cd("C:\Learning_ERL\src").
And you should see output like-
(Learning-ERL#DALAKSHM-MNFSM)7> cd("C:\Learning_ERL\src").
c:/Learning_ERL/src
ok
Then compile the code -
c(add)
you should see something like this on the erl shell-
(Learning-ERL#DALAKSHM-MNFSM)10> c(add).
{ok,add}
Now you should be seeing a new file called - add.beam in the same directory as that of your erl source file - add.erl
add.beam is a bytecode file
Now you should be able to run the program without any error
How do you try to execute your code?
In your editor, right-click and choose "Run as"->"Erlang application". The VM that is launched will have your project loaded automatically and when editing/saving a file it will get reloaded. When launching, a console appears and you can call your code from there.
If it still doesn't work, what message do you get for m(add).?

Using pfccomp or pint to run Pascal-FC programs

I'm using the Pascal FC implementation for Windows Vista found on http://www-users.cs.york.ac.uk/burns/pf.html
I'm trying to run the dining philophers problem found on the link but I don't get how to make the compiler work.
Screenshot
In the screen shot, the program appears to be waiting for you to enter the name of the file to use for the compiler output. Enter a file name.
Better yet, use the pfc.bat command and let it choose the output names for you. The batch file will also run the program automatically after it has been compiled. At the command prompt, run the command like this:
C:\pascalfc-vista> pfc philchan.pas

Resources