digitalmars.D.learn - Yes or No Options
- Alex (35/35) Jul 27 2015 Hey guys!
- Adam D. Ruppe (25/26) Jul 27 2015 What happens here is a bit tricky and trips up a lot of
- Alex (27/27) Jul 27 2015 Thank you! That helped me a lot.
- Alex (19/19) Jul 27 2015 Okay. By pure trying I found out what I did wrong:
- Anonymous (4/23) Jul 27 2015 Check out what is the length of yesno after you do your readln.
- CraigDillabaugh (7/37) Jul 27 2015 Also, notice in Namespace's answer above the use of:
- Tofu Ninja (3/22) Jul 27 2015 readln includes the '\n' at the end, so when you typed "Y" and
- Alex (38/38) Jul 28 2015 So I now combined a few of the options here and got this, which
- Namespace (28/28) Jul 27 2015 Look at my example:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/6) Jul 27 2015 The program looks a lot like one of the exercises in this chapter:
- Alex (4/11) Jul 27 2015 Yes! I am really sorry. I did not buy it. Thanks for this book,
- wobbles (5/21) Jul 28 2015 Good on you to pick it up and learn. It's a very interesting
- Chris (3/6) Jul 29 2015 Out of interest: what made you start with D? It's not the most
- Alex (19/19) Jul 30 2015 My father owns a small software company, specialized in market
- Chris (6/25) Jul 30 2015 Very interesting indeed! Care to write an article about it one
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/7) Jul 30 2015 I don't think they use D yet but the page is here:
- Chris (5/13) Jul 31 2015 That's the link. Thanks! I find the reasons Alex gave for using D
- Alex (9/41) Jul 31 2015 Sure I'd do something like that! Maybe refering to the Ali
Hey guys! I am super new to programming and still trying to learn the very basics via a book that I bought. My problem is the following: import std.stdio; import std.string; void main() { char[] yesno; write("Roll the dice: Enter a number!"); int dieNumber; readf(" %s", &dieNumber); if (dieNumber < 4) { writeln("You won!"); } else if ((dieNumber >= 4) && (dieNumber <= 6)) { writeln("I won!"); } else if (dieNumber > 6){ writeln("ERROR: Invalid Value"); } writeln("Do you want to play again? Y/N?"); readln(yesno); if (yesno == "Y") { writeln("Let's go again!"); } } The program quits after "writeln("Do you want to play again? Y/N?");" It ignores readln. Furthermore: What I am actually trying to do is: If I type "Y", the programm should just rerun from the beginning. I am really new to programming and there is probably a much easier way but this is all I know to this point. Thanks in advance!
Jul 27 2015
On Monday, 27 July 2015 at 15:50:11 UTC, Alex wrote:readf(" %s", &dieNumber);What happens here is a bit tricky and trips up a lot of programmers: readf leaves the end-of-line character in the buffer, which readln then sees as meaning its job is done. When you enter, say, 5, then press enter, the input looks like: "5\n". (\n represents the newline aka end-of-line character). readf " %s" first skips any spaces. There's none, so it doesn't matter. Then it reads in a number. So it takes the 5, leaving the rest of the input behind.... so that \n from pressing enter is still there. readln reads everything it can until it hits a \n, picking up where readf left off. So the result is it immediately sees the \n that readf stopped on and it also stops. An easy fix is to stick an extra readln() after the readf: readf(" %s", &dieNumber); readln(); // tell it to skip the rest of the line // the rest of your code is unchanged The next thing you'll have to face is actually going again when they enter Y. (BTW be aware that this is case sensitive and readln leaves the newline at the end of the input too! So entering "y" won't work. And when you enter "Y"... the program will see "Y\n", so it won't match "let's go again" either.) But to actually go again, you'll probably want to read about loops. I'll let you play with it a little from here though.
Jul 27 2015
Thank you! That helped me a lot. I'm sure that - in order to get to the point to repeat the whole first part of the program - I'll have to read further in the instructions I have BUT let's just say that I don't want it to repeat the first part of the program but just writeln something like "Ok let's do it again!" if I enter Y and press enter. I now got to the point where the program doesn't quit but if I enter "Y" and hit Enter, nothing happens. I do understand how to define an int and then compare it to a number I entered and do something if the number was smaller/bigger/the same etc. But how do I get the programm to simply write a line if I enter a letter like Y? With the code I got here it does not work. What is wrong with this part here: writeln("Do you want to play again? Y/N?"); readf(" %s", &yesno); readln(); if (yesno == "Y") { writeln("Yeah!"); } Doesn't the code mean: Write "Do you want to play again? Y/N?". Read keyboard input and define "yesno" by what was entered. And then: If what was entered was a "Y", write the following line "Yeah!". Where is my error in thinking? Sorry guys, I know it's probably something very easy..
Jul 27 2015
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..
Jul 27 2015
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.
Jul 27 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: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).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.
Jul 27 2015
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..readln includes the '\n' at the end, so when you typed "Y" and pressed enter, readln returned "Y\n" which != "Y".
Jul 27 2015
So I now combined a few of the options here and got this, which finally works: import std.stdio; import std.string; import std.random; void main() { while (true) { string yesno; int weiter; char[] uschi; write("Press ENTER to roll the dice!"); readln(uschi); if (uschi == "\n") { auto rng = uniform(1, 6); writeln(rng); if (rng < 4) { writeln("You won!"); } else if ((rng >= 4) && (rng <= 6)) { writeln("I won!"); } else if (rng > 6){ writeln("ERROR: Invalid Value"); } writeln("Do you want to play again? Y/N?"); yesno = readln(); if (yesno.toLower() != "y\n") { writeln("Damn it"); break; } } } } Thank you ! :)
Jul 28 2015
Look at my example: ---- import std.stdio; import std.string; import std.conv : to; void main() { while (true) { write("Roll the dice: Enter a number: "); int dieNumber = readln.strip.to!int; if (dieNumber < 4) { writeln("You won!"); } else if ((dieNumber >= 4) && (dieNumber <= 6)) { writeln("I won!"); } else if (dieNumber > 6){ writeln("ERROR: Invalid Value"); } writeln("Do you want to play again? Y/N?"); immutable string yesno = readln.strip; if (yesno.toLower() != "y") break; writeln("Let's go again!"); } } ---- With the while loop you really can "go again" ;)
Jul 27 2015
On 07/27/2015 08:50 AM, Alex wrote:a book that I boughtThe program looks a lot like one of the exercises in this chapter: http://ddili.org/ders/d.en/if.html You didn't actually pay for it, right? Because it is free. :) Ali
Jul 27 2015
On Monday, 27 July 2015 at 17:31:08 UTC, Ali Çehreli wrote:On 07/27/2015 08:50 AM, Alex wrote:Yes! I am really sorry. I did not buy it. Thanks for this book, it is really cool to learn and understandable even for people like me that have never had any contact with programming!a book that I boughtThe program looks a lot like one of the exercises in this chapter: http://ddili.org/ders/d.en/if.html You didn't actually pay for it, right? Because it is free. :) Ali
Jul 27 2015
On Monday, 27 July 2015 at 18:23:57 UTC, Alex wrote:On Monday, 27 July 2015 at 17:31:08 UTC, Ali Çehreli wrote:Good on you to pick it up and learn. It's a very interesting field once you get past the initial learning curve! Be sure to continue to ask questions here, I find most people here are very willing to help newbies :)On 07/27/2015 08:50 AM, Alex wrote:Yes! I am really sorry. I did not buy it. Thanks for this book, it is really cool to learn and understandable even for people like me that have never had any contact with programming!a book that I boughtThe program looks a lot like one of the exercises in this chapter: http://ddili.org/ders/d.en/if.html You didn't actually pay for it, right? Because it is free. :) Ali
Jul 28 2015
On Monday, 27 July 2015 at 15:50:11 UTC, Alex wrote:Hey guys! I am super new to programming and still trying to learn the very basics via a book that I bought.Out of interest: what made you start with D? It's not the most obvious choice for a programming novice.
Jul 29 2015
My father owns a small software company, specialized in market data products. www.bccgi.com (in case anyone is interested) So programming was basically around all my life. I do a small job in his company and my next task was to learn D. There are two trainees and the three of us have to learn D. Ofc the two trainees have to learn other languages as well. My dad said the reason why we learn this language is that he personally finds it to be a very intuitive language that produces machine code. If he just wanted us to teach programming he said In addition to that he wants to keep up and always have new languages and features in the company (only 8 people). And since we have experts for almost any language here but not a single one for D, it was time for someone to start! Once I started I found it to be really interesting and challenging plus I like solving problems. Thank you for being so nice! I have seen very few communities where beginners are welcomed so well!
Jul 30 2015
On Thursday, 30 July 2015 at 14:20:41 UTC, Alex wrote:My father owns a small software company, specialized in market data products. www.bccgi.com (in case anyone is interested) So programming was basically around all my life. I do a small job in his company and my next task was to learn D. There are two trainees and the three of us have to learn D. Ofc the two trainees have to learn other languages as well. My dad said the reason why we learn this language is that he personally finds it to be a very intuitive language that produces machine code. If he just wanted us to teach In addition to that he wants to keep up and always have new languages and features in the company (only 8 people). And since we have experts for almost any language here but not a single one for D, it was time for someone to start! Once I started I found it to be really interesting and challenging plus I like solving problems. Thank you for being so nice! I have seen very few communities where beginners are welcomed so well!Very interesting indeed! Care to write an article about it one day? "Learning D as an absolute beginner" or something like that. I wonder, is your father's company listed among those using D? I think there's a list somewhere on Wiki, if not we should have one :-)
Jul 30 2015
On 07/30/2015 08:14 AM, Chris wrote:I wonder, is your father's company listed among those using D? I think there's a list somewhere on Wiki, if not we should have one :-)I don't think they use D yet but the page is here: http://wiki.dlang.org/Current_D_Use Ali
Jul 30 2015
On Thursday, 30 July 2015 at 17:48:51 UTC, Ali Çehreli wrote:On 07/30/2015 08:14 AM, Chris wrote:That's the link. Thanks! I find the reasons Alex gave for using D very interesting. It's beyond all hype and reddit and stuff like that. It shows that D attracts users for what it is, without any hype or sh*t like that.I wonder, is your father's company listed among those using D? I think there's a list somewhere on Wiki, if not we should have one :-)I don't think they use D yet but the page is here: http://wiki.dlang.org/Current_D_Use Ali
Jul 31 2015
On Thursday, 30 July 2015 at 15:14:28 UTC, Chris wrote:On Thursday, 30 July 2015 at 14:20:41 UTC, Alex wrote:Sure I'd do something like that! Maybe refering to the Ali Çehreli's book! Question is if my English skills are sufficient.. They aren't in the list because right now nothing is written in D in our company. But the future will look different I according to what my dad told me. Is there a reddit community? How is D generally seen in the internet?My father owns a small software company, specialized in market data products. www.bccgi.com (in case anyone is interested) So programming was basically around all my life. I do a small job in his company and my next task was to learn D. There are two trainees and the three of us have to learn D. Ofc the two trainees have to learn other languages as well. My dad said the reason why we learn this language is that he personally finds it to be a very intuitive language that produces machine code. If he just wanted us to teach In addition to that he wants to keep up and always have new languages and features in the company (only 8 people). And since we have experts for almost any language here but not a single one for D, it was time for someone to start! Once I started I found it to be really interesting and challenging plus I like solving problems. Thank you for being so nice! I have seen very few communities where beginners are welcomed so well!Very interesting indeed! Care to write an article about it one day? "Learning D as an absolute beginner" or something like that. I wonder, is your father's company listed among those using D? I think there's a list somewhere on Wiki, if not we should have one :-)
Jul 31 2015