Do-while does not stop at the first 'q'

So I am working on a simple little text game in D to get some experience with the language. Here is the do-while loop I'm currently struggling with:

do{
  writeln("a. Advance 1 year\tc. Advance 10 years\tq. Quit");
  writeln("b. Advance 5 years\td. Modify faction");

  input = chomp(stdin.readln());

  switch(input){
    ...
    default:
      break;
  }
  writeln(input[0]);
}while(input[0] != 'q');

      

Now the problem I am facing is that when I press q and enter the loop, it does not exit. It just keeps walking. But then, after the first q input, another q will end the loop. The writer is there as a sanity check, and it prints the characters that I type exactly as I type. I feel like I'm going crazy, but it's probably just a simple-o-o or something that you guys will notice instantly. Nothing in the switch statement changes "enter".

EDIT: Ok some people are asking to see all the code. Here it is: http://pastebin.com/A7qM5nGW

When I said nothing in the changed switch input it was supposed to hide the fact that I hadn't written anything in the switch yet. I tried to get some of the output to work before adding more complex stuff. Also, here's a sample file for what I'm running it for: http://pastebin.com/4c2f4Z5N

+3


source to share


1 answer


Ok my friend found this. It has nothing to do with the while loop itself. I briefly forgot that args [0] is the name of the program. So it actually runs through the parent loop once without anything and then actually leaves and then goes through the appropriate loop. This was fixed by creating a parent loop like this ...

foreach(filename; args[1..$]){
    ...
    do{
        ...
    while(input[0] != 'q');
}

      

Unlike:



foreach(filename; args){

      

etc...

+1


source







All Articles