So I'm working on a simple little text-based game in D to gain some experience working with the language. Here is a do-while loop that 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'm running into is that when I hit q and enter the loop doesn't exit. It just keeps going. But then after the first time q is input, another q will terminate the loop. The writeln is in there as a sanity check, and it prints out the characters I type in exactly as typed. I feel like I'm going crazy, but it's probably just a simple type-o or something you guys will spot instantly. Nothing in the switch statement modifies 'input'.
EDIT: Okay some people have been asking to see all of the code. Here it is: http://pastebin.com/A7qM5nGW
When I said nothing in the switch modified input, it was to hide the fact I hadn't written anything in the switch yet. I've been trying to get the quit part to work right before adding the more complicated stuff. Also, here's a sample file for what I run it on: http://pastebin.com/4c2f4Z5N
Okay my friend found it. It has nothing to do with the while loop itself. I briefly forgot that args[0] is the name of the program. So it's actually running through the parent loop once with nothing, then actually quitting, and then running through the appropriate loop. It was fixed by making the parent loop like so...
foreach(filename; args[1..$]){
...
do{
...
while(input[0] != 'q');
}
as opposed to:
foreach(filename; args){
etc...
Related
(New to C++)
I have this simple code (simplified for the question):
int main()
{
string currInput;
while (getline(cin, currInput))
{
}
cout << "wont be printed" << std::flush;
return 0;
}
I have been debugging for a while and I probably miss something:
When running it and pressing ctrl+d (after some strings or right away), it does not print the string that is after the while loop. It just stop running. I thought it might be something with flushing so I added that too.
What am I missing?
PS: When running in debug, it mentions something about sighup signal.
So, with a lot of help from #manni and #rici, we found the problem.
It turns out to be a known problem in cLion.
see sending EOF to stdin in Clion IDE
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206849765-How-to-enter-EOF-Ctrl-z-in-Run-console-
https://youtrack.jetbrains.com/issue/IDEA-12514
Your error is the while loop. You should not be using a loop here. What happens is it hits this line and attempts to get input. Regardless of whether you type in anything or not, if you hit CTRL+D it will end the getline. But you have it in a while loop....so it'll hop back to the top of the loop and get another line...then another line....then another line. Welcome to your first infinite loop.
while (getline(cin, currInput))
{
}
The simplest thing would be to do just
getline(cin, currInput);
If you're starting out programming this is probably what you want to do anyway.
If you feel gutsy, read this page: http://www.cplusplus.com/reference/string/string/getline/
You'll notice that getline returns the stream you pass in. Which will evaluate to true as far the loop is concerned.
Is there a way to make a loop that checks user input but doesn't stop if the user doesn't input anything?
For example:
while(condition){
...
selection = _getch();// the or an equivalent of getch()
...
}
But without the loop stopping and waiting for the user to input something.
I'm on VS if it matters.
I found an answer right after posting this, so I'll make it an answer:
Using kbhit() like so will solve it (at least on VS):
while(condition){
if (_kbhit()) {
char ch = _getch();
...
I have made a function called pause(). Here is the code for it:
void pause(){
std::cin.sync();
std::cin.ignore();
}
I also use a declaration in front of my code:
void pause();
int main(){
}
It worked at first, but now for some reason it just skips the function when I call it...
std::cout << "Random text. Press any key to carry on...";
pause();
EDIT: Unfortunately, when I try it differently, it goes makes me enter something and then I have to press enter. Is there really not a way to make it so that if you press any key (like the ugly system("pause") it immediately carries on? I'll have to make a new thread for this...
This is why it's important to post the smallest complete code you can come up with that compiles and runs and shows the problem. Chances are, though, that there's some input done before calling this function, and it's leaving something in the input buffer. So pause isn't being skipped, but it returns immediately because of the noise in the input.
Yeah. So, I'm trying to make a code for a guessing game. In this game, there's a hard mode. In hard mode, you have 15 guesses, and have to guess between 1 and 500. But my problem is this:
I'm trying to have hard mode save & display your wins/losses, but when it outputs the contents of wins.txt it outputs something like this:
Wins: 0x7fffee26df78
Losses: 0x7fffee26e178
It's really confusing me. Here's the part of the code I have for that:
ifstream losses_var("losses.txt");
ifstream wins_var("wins.txt");
losses_var>> loss;
wins_var>> win;
wins_var.close();
losses_var.close();
Then it gets called with:
cout<<"Wins: "<< wins <<"\nLosses: "<< losses <<"\n"
If you would like to see the full source code, it's here: http://pastebin.com/gPT37uBJ
My second problem:
Hard mode won't display when you win. That's pretty much the whole problem. In my code, the loop for asking users for input uses
while (guess != randNum)
So at the end bracket I have what I want the code to display when a user wins, but it just doesn't run. It just stops. I would like it if someone could help me with this. The line that has the bug is line 97 through 105. Again, source code is here: http://pastebin.com/gPT37uBJ
You've got your variable names confused
cout<<"Wins: "<< wins <<"\nLosses: "<< losses <<"\n";
should be
cout<<"Wins: "<< win <<"\nLosses: "<< loss <<"\n";
It's important to pick good variable names. One reason is so that you don't confuse yourself about what your variables mean (if you confuse yourself think how it's going to be for someone else looking at your code).
Others have already answered the output problem (win vs. wins). The other problem is probably in your logic of while loop nesting. The outer loop (while (guess != randNum)) starts, but its body contains the entire inner loop (while (guesses_left != 0)). This means that the outer condition is not checked again until the inner loop terminates, which means you've run out of guesses. Also note that if you guess correctly, inner loop will never terminate. You probably want something like this:
while (guesses_left > 0) {
// input user's guess
if (guess < randNum) {
// process it
} else if (guess > randNum) {
// process it
} else {
// it's equal, user won
// do what's necessary for a win
return 0;
}
}
// ran out of guesses
// do what's necessary for a loss
return 0;
You are not writing your variables win and loss to cout. From your pasted code, I can see that wins and losses are ofstream objects, which means you are probably seeing addresses there. I would advise you to choose more informative variable names to avoid hard to spot mistakes like this.
I'm a rookie programmer, so please be polite.
Well i'm trying to write a simple Terminal Backgammon game, just for fun, but i have a problem.
The entire game runs in a while loop which keeps re running as long as nobody moved all their bricks to the end of the board.
A simple integer controls whatever it is black or white who plays.
I wrote a function to check for any possible moves, cause i want to program to skip the turn in case absolutely no moves can be made.
Well, i want this function to run and in case it returns false(No possible moves) then i want the rest of the code to skip and change the turn to the next player. For example if the dice combination gives no possible moves for black, then i want the program to skip black and go to white.
So i sort of want to break the rest of the while loop, but keep it running.
It's a little complicated for me to explain the issue, but i hope you guys understand.
Thanks alot
- Martin
It sounds like you want to use continue:
while (someCondition)
{
doSomething();
if (someOtherCondition)
continue;
doSomethingElse();
}
In this example, if someOtherCondition is true, the continue statement will cause the program to jump back to the top of the loop rather than continuing to execute the following statements. If someOtherCondition is false, doSomethingElse() will get run as normal.
I think this is roughly what you want to know.
Hope it helps.
while( keepRunning )
{
bool noPossibleMoves = checkForPossibleMoves();
setup for each loop iteration
Do things here that are always necessary.
if( noPossibleMoves )
{
continue; // This will go to the top of the while loop
}
wait for user input etc...
...
...
}