0

Well, I'm writing to make a dice game. I tried searching dice game here but none of it seems to answer my question. This isn't a problem about the dice roll thing anyway. It's about the do while loop. I am very new to this site, I just found out about this via Maximum PC Magazine so please bear with me. Also I am new to programming.

Here is my code:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main(){

    srand(time(NULL));

    int userRoll = rand() % 6 + 1 ;
    int computerRoll = rand() % 6 + 1 ;
    string yesOrNoChoice;
    string commandToThrowDie;


    do{
        cout << "Please enter \"throw\" (lowercase) to roll the die: ";
        cin >> commandToThrowDie;
    } while(commandToThrowDie != "throw");

    do{
        cout << "You rolled: " << userRoll << endl
             << "The Computer rolled: " << computerRoll << endl;

        if (userRoll < computerRoll){
            cout << "You lose. Try again? [Yes/No]: ";
         }
        if (computerRoll < userRoll){
            cout << "You win! Try again? [Yes/No]: ";
        }
        if (computerRoll == userRoll) {
            cout << "It's a draw. Try again? [Yes/No]: ";
        }
        cin >> yesOrNoChoice;
    } while(yesOrNoChoice != "Yes"); 

    system ("pause");
    return 0;
}

The problem is that after asking the user to enter a choice at the end of the do-while-loop the program exits loop no matter what I enter, instead of looping back to another throw of the die.

It ends up like this:

Screenshot of program run

  • 4
    What's your actual question? What problem do you have? What did you try? – Tomo Nov 19 '14 at 10:04
  • 1
    Please format your code properly - it's very difficult to read with random indentation. – Paul R Nov 19 '14 at 10:05
  • @Tomo the second do-while loop doesn't work. The one where I cout "You rolled: " etcetera. I don't understand why it doesn't work, it's encoded probably.. or so I think. I'm still new to programming by the way :) – Dylan Villaruel Nov 19 '14 at 10:12
  • @PaulR It's now properly formatted and fixed :) – Dylan Villaruel Nov 19 '14 at 10:12
  • @DylanVillaruel: seriously ? It's still just as unreadable and randomly indented - the only change I can see is that you deleted some stray text ? Fortunately it looks like another user is now trying to fix the formatting for you - the edit should show up soon - please pay attention to code formatting when posting questions in future. – Paul R Nov 19 '14 at 10:15
  • @PaulR It looks fine to me, but hey, it's me. I'm sorry for not properly formatting the text. I just joined the site today and I'm not so familiar in methods of posting codes – Dylan Villaruel Nov 19 '14 at 10:20
  • The guy is new and probably doesn't even know what proper identation. Dylan, take a look at [this](http://en.wikipedia.org/wiki/Indent_style), choose a style and stick to it. – vmg Nov 19 '14 at 10:20
  • @vmg Thank you for the help. Will be reading this. Yes, I am new. Very new in fact. – Dylan Villaruel Nov 19 '14 at 10:22
  • 1
    @DylanVillaruel: do you have a good book on C++ yet ? If not then check out the list here: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Paul R Nov 19 '14 at 10:27
  • @PaulR I have The C++ Programming Language, 4th Edition. But I'll try C++ Primer as seen in the link you've gave. Thanks for the suggestion! – Dylan Villaruel Nov 19 '14 at 10:31
  • That book you have is a great as a reference, but it's not really a good introductory text for beginners, so yes, get something like C++ Primer also. – Paul R Nov 19 '14 at 10:33
  • 1
    You shouldn't search "dice game", that's the wrong way to think about it : those words don't describe your *programming* problem. Also learn to run your program step by step in your debugger, you would have noticed the problem. – Leiaz Nov 19 '14 at 10:35

1 Answers1

1

I copied your code and it compiled and ran perfectly. Doesn't make sense exactly, but no issues. I say it doesn't make sense since when "Yes" is entered that is what kills it. I believe what you want it while(yesOrNoChoice == "Yes"). Perhaps having it as != was making you think you were getting the wrong behavior? Also, you should be using if, else if, else statements, not just if.

David
  • 4,744
  • 5
  • 33
  • 64
  • 1
    It may become clearer when you think about it like this: You want the loop to continue *as long as* the user enters "Yes". Thus, you need to repeat the look `while(yesOrNoChoice == "Yes")`, i.e., as long as the user enters "Yes". You may have confused `do {...} while (...)` with something like `loop {...} until (...)`, which does not exist in C or C++: http://stackoverflow.com/questions/1077216/how-do-you-make-a-repeat-until-loop-in-c – Alexander Weinert Nov 19 '14 at 10:37
  • That solved the part where the program gets killed. But whenever I try to enter "Yes", it would still output the same result. e.g.; user die: 3, computer die 5, and when I try again it still outputs the same number. It doesn't get seeded again by the srand(). But, thank you for the help. Very useful :) – Dylan Villaruel Nov 19 '14 at 10:40
  • Yes, I know it will, but you had said the question wasn't about the game, so I didn't mention that. You need to generate your `rand()`'s every time the loop goes though. – David Nov 19 '14 at 10:43
  • Oh, okay I get it now. That totally solved the problem. Thank you! I got it running stable now. – Dylan Villaruel Nov 19 '14 at 11:03