Jump to page: 1 2
Thread overview
Yes or No Options
Jul 27, 2015
Alex
Jul 27, 2015
Adam D. Ruppe
Jul 27, 2015
Alex
Jul 27, 2015
Alex
Jul 27, 2015
Anonymous
Jul 27, 2015
CraigDillabaugh
Jul 27, 2015
Tofu Ninja
Jul 28, 2015
Alex
Jul 27, 2015
Namespace
Jul 27, 2015
Ali Çehreli
Jul 27, 2015
Alex
Jul 28, 2015
wobbles
Jul 29, 2015
Chris
Jul 30, 2015
Alex
Jul 30, 2015
Chris
Jul 30, 2015
Ali Çehreli
Jul 31, 2015
Chris
Jul 31, 2015
Alex
July 27, 2015
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!
July 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.
July 27, 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" ;)
July 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..

July 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..


July 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.
July 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:
>> 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).
July 27, 2015
On 07/27/2015 08:50 AM, Alex wrote:

> a book that I bought

The 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

July 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".
July 27, 2015
On Monday, 27 July 2015 at 17:31:08 UTC, Ali Çehreli wrote:
> On 07/27/2015 08:50 AM, Alex wrote:
>
> > a book that I bought
>
> The 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



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!
« First   ‹ Prev
1 2