dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.57k forks source link

stdin.readLineSync(); #18725

Closed DartBot closed 9 years ago

DartBot commented 10 years ago

This issue was originally filed by jhiggi...@ridgeviewgroup.com


What steps will reproduce the problem?

  1. Create a simple console app that accepts user input using dart:io and the stdin.readLineSync() function.
  2. Test example code:

String getPlayerMove() { print("Would you like (R)ock, (P)aper, (S)cissors?"); String selection; selection = stdin.readLineSync().toUpperCase();

switch(selection) {   case "R":     return "Rock";     break;   case "P":     return "Paper";     break;   case "S":     return "Scissors";     break;   default: //if we get anything but R, P, or S     return "Quit";     break;   } }

3.

What is the expected output? What do you see instead?

Expect to be able to enter data and the rest of my code to process user input from the console. Instead the program exist with code zero.

What version of the product are you using? On what operating system?

Dart Dart Editor version 1.3.6.release (STABLE) Dart SDK version 1.3.6 OSX 10.9.2

Please provide any additional information below.

I have been able to replicate in variety of other of code snippets. Same code worked fine a week a go. Here is the full program: import 'dart:io'; import 'dart:math';

/ Get a player move via keyboard input   if the players does not enter a valid move   return "Quit" so that the main game loop   knows to end the game  */

String getPlayerMove() { print("Would you like (R)ock, (P)aper, (S)cissors?"); String selection; selection = stdin.readLineSync().toUpperCase();

switch(selection) {   case "R":     return "Rock";     break;   case "P":     return "Paper";     break;   case "S":     return "Scissors";     break;   default: //if we get anthing but R, P, or S     return "Quit";     break;   } }

/* Return a random number move in the form   of a string of either "Rock", "Paper", "Scissor"  */

String getComputerMove() {   Random rand = new Random();   int move = rand.nextInt(3); // gen a random int from 0 to 2      switch(move){     case 0:       return "Rock";       break;     case 1:       return "Paper";       break;     case 2:       return "Scissors";       break;     default:       break;   } }

/* Determine if the player or computer won by   comparing [playerMove] to [computerMove]  */

String whoWon(String playerMove, String computerMove) { //test if they are the same if so its a tie   if(playerMove == computerMove){     return "Tie";   } else if(playerMove == "Rock" && computerMove == "Scissors"){     return "You Win!";   } else if(playerMove == "Scissors" && computerMove == "Paper"){     return "You Win!";   } else if(playerMove == "Paper" && computerMove == "Rock"){     return "You Win!";   } else { // if it's not a tie and you did not win then the computer won     return "Computer Wins!";   } }   void main() {   while(true) { // main game loop - qusi infinite loop     print("Rock, Paper, Scissor Shoot!");     String playerMove = getPlayerMove();     if(playerMove == "Quit"){       return; // return the void function to exit program     }          print("You played $playerMove");     String computerMove = getComputerMove();     print("Computer played $computerMove");     print(whoWon(playerMove, computerMove));     }   }

lrhn commented 10 years ago

Added Area-IO, Triaged labels.

andersjohnsen commented 10 years ago

Hi,

I'm unable to reproduce this. I can play as expected, but I did note that if you mis-type, e.g. typing 'c', it will silently quit. Adding a

  print("Goodbye");

after

  if(playerMove == "Quit"){

made it a bit more unsurprising.


Set owner to @skabet. Added NeedsInfo label.

DartBot commented 10 years ago

This comment was originally written by jhiggi...@ridgeviewgroup.com


This is really strange. Well if it work for you it has to be on my end so obviously close this one out but I do not even get the opportunity to input data it just executes and exits the program with error code 0. Here is the output after I added you line of code. The only things I saw was line 35 in the String getComputerMove() block it says - "this function declares a return type of 'String', but does not end with a return statement" but that should not stop the program for asking me for input in line selection = stdin.readLineSync().toUpperCase();

/Applications/Dart/dart-sdk/bin/dart --enable-checked-mode --debug:51786 rockpaperscissors.dart Rock, Paper, Scissor Shoot! Would you like (R)ock, (P)aper, (S)cissors? Goodbye

andersjohnsen commented 10 years ago

I see, this is happening when run from the editor. It turns out that the editor writes a first line to stdin, with the arguments invoked:

  "C:[...]\DART-SDK\BIN\DART.EXE --ENABLE-CHECKED-MODE --DEBUG:50285 STDIN.DART"

And as this does not match your expected strings, it exits.

Reassigning to the editor.


Removed Area-IO label. Added Area-Editor, Triaged labels.

clayberg commented 10 years ago

cc @skabet. cc @keertip. Set owner to @devoncarew. Added this to the 1.5 milestone. Removed Priority-Unassigned label. Added Priority-Medium label.

andersjohnsen commented 10 years ago

Issue #18902 has been merged into this issue.

kasperl commented 10 years ago

I think medium priority is too low for this.

kasperl commented 10 years ago

The issue is caused by writing the command line into the console at process launch. Here's one possibly fix: https://codereview.chromium.org/299323004/.

DartBot commented 10 years ago

This comment was originally written by @butlermatt


Any chance of this landing as a bug fix release in stable (1.4) as opposed to waiting for 1.5 to hit stable? As mentioned in Comment #­7 it does deserve to be a higher priority for release when resolved as well as many/most documents/tutorials/etc rely on this working over running from CLI.

kasperl commented 10 years ago

Fixed in r36643. Merge request filed in issue #19030.


Added Fixed label.