Yes or No Options

CraigDillabaugh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 27 10:30:03 PDT 2015


On Monday, 27 July 2015 at 17:21:33 UTC, Anonymous wrote:
> On Monday, 27 July 2015 at 16:48:00 UTC, Alex wrote:
>> Okay. By pure trying I found out what I did wrong:
>>
>> Apparently by typing Y I entered the shift key. Could that 
>> have been the problem?
>> I changed it to a small y and it at least jumped back to the 
>> commandline instead of just being stuck.
>>
>> And by changing:
>>
>> writeln("Do you want to play again? Y/N?");
>> 	readln(yesno);
>> 	if (yesno == "y") {
>> 		writeln("Yeah!");
>> 	}
>>
>> to:
>>
>> writeln("Do you want to play again? Y/N?");
>> 	readln(yesno);
>> 	if (yesno != "y") {
>> 		writeln("Yeah!");
>> 	}
>>
>> So instead of ==   I used  !=
>>
>> Now it works. But I still do not know why..
>
> Check out what is the length of yesno after you do your readln.
>
> Ex. writeln(yesno.length)
>
> std.string.chomp may help.

Also, notice in Namespace's answer above the use of:

  if (yesno.toLower() != "y")

This ensures that whether the user typed 'Y' or 'y' then check 
works properly.  Which is likely what the user expects.

The 'Shift' key does not add any new symbols to the string (it 
only modifies what symbols are added).


More information about the Digitalmars-d-learn mailing list