c++ - How could I exit from do-while loop? -


I have code that relates to NIM subtraction game. What I would like to implement is that the user is going to be able to play the game till he wants it. If the user enters the 999 program then he will exit, otherwise he will play until he enters 999. This is my block of code. I'm not sure whether I make a logical mistake or I need to add some specific exit code. Thanks for your time and attention.

  #include & lt; Iostream & gt; # Include & lt; Cstdlib & gt; using namespace std; int main () {int total, n; While (true) {cout & lt; & Lt; "Welcome to NIM. \ N 99 9 to quit playing! \ NClick an initial total:"; Cin & gt; & Gt; Total; Break if (total = 999); While (true) {/ Choosing the best response and print results if ((% of total 3) == 2) {total = total - 2; Cout & lt; & Lt; "I'm decreasing 2." & Lt; & Lt; Endl; } And {total--; Cout & lt; & Lt; "I'm decreasing 1." & Lt; & Lt; Endl; } cout & lt; & Lt; "New total" & lt; & Lt; Total & lt; & Lt; Endl; If (total == 0) {cout & lt; & Lt; "I won!" & Lt; & Lt; Endl; break; } // Get user feedback; Must be 1 or 2. Cout & lt; & Lt; Enter the number for "Decrease (1 or 2):"; Cin & gt; & Gt; N; While (n & lt; 1 || n & gt; 2) {cout & lt; & Lt; "Input should be 1 or 2." & Lt; & Lt; Endl; Cout & lt; & Lt; "Re-enter:"; Cin & gt; & Gt; N; } Total = total - n; Cout & lt; & Lt; "New total" & lt; & Lt; Total & lt; & Lt; Endl; If (total == 0) {cout & lt; & Lt; "You won!" & Lt; & Lt; Endl; break; }}} 0 back; }    

You are amending the total inside the loop If total == 999 and break if true , either cin & gt; Test after the total

  if (total == 99) break;   

and do-while while replacing the loop (right) {}

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -