Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdin.readLineSync(); #18725

Closed
DartBot opened this issue May 8, 2014 · 10 comments
Closed

stdin.readLineSync(); #18725

DartBot opened this issue May 8, 2014 · 10 comments
Assignees
Milestone

Comments

@DartBot
Copy link

DartBot commented May 8, 2014

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
Copy link
Member

lrhn commented May 9, 2014

Added Area-IO, Triaged labels.

@andersjohnsen
Copy link

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.

  • Anders

Set owner to @skabet.
Added NeedsInfo label.

@DartBot
Copy link
Author

DartBot commented May 9, 2014

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
Copy link

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
Copy link

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

@andersjohnsen
Copy link

Issue #18902 has been merged into this issue.

@kasperl
Copy link

kasperl commented May 26, 2014

I think medium priority is too low for this.

@kasperl
Copy link

kasperl commented May 26, 2014

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
Copy link
Author

DartBot commented May 26, 2014

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
Copy link

kasperl commented May 27, 2014

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


Added Fixed label.

@DartBot DartBot added this to the 1.5 milestone May 27, 2014
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants