From ea406c23722c4d20ace06bc8fe7b07faf74b0775 Mon Sep 17 00:00:00 2001 From: Mark J Price Date: Thu, 6 Apr 2023 08:16:14 +0100 Subject: [PATCH] Fixed logic error in password loop --- vs4win/Chapter03/IterationStatements/Program.cs | 9 ++++++--- vscode/Chapter03/IterationStatements/Program.cs | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/vs4win/Chapter03/IterationStatements/Program.cs b/vs4win/Chapter03/IterationStatements/Program.cs index 567b7ed..8ca2fa4 100644 --- a/vs4win/Chapter03/IterationStatements/Program.cs +++ b/vs4win/Chapter03/IterationStatements/Program.cs @@ -10,7 +10,9 @@ while (x < 10) // Looping with the do statement +string? actualPassword = "Pa$$w0rd"; string? password; +int maximumAttempts = 10; int attempts = 0; do @@ -19,15 +21,16 @@ do Write("Enter your password: "); password = ReadLine(); } -while ((password != "Pa$$w0rd") & (attempts < 10)); +while ((password != actualPassword) & (attempts < maximumAttempts)); -if (attempts < 10) +if (password == actualPassword) { WriteLine("Correct!"); } else { - WriteLine("You have used 10 attempts!"); + WriteLine("You have used {0} attempts! The password was {1}.", + arg0: maximumAttempts, arg1: actualPassword); } // Looping with the for statement diff --git a/vscode/Chapter03/IterationStatements/Program.cs b/vscode/Chapter03/IterationStatements/Program.cs index 567b7ed..8ca2fa4 100644 --- a/vscode/Chapter03/IterationStatements/Program.cs +++ b/vscode/Chapter03/IterationStatements/Program.cs @@ -10,7 +10,9 @@ while (x < 10) // Looping with the do statement +string? actualPassword = "Pa$$w0rd"; string? password; +int maximumAttempts = 10; int attempts = 0; do @@ -19,15 +21,16 @@ do Write("Enter your password: "); password = ReadLine(); } -while ((password != "Pa$$w0rd") & (attempts < 10)); +while ((password != actualPassword) & (attempts < maximumAttempts)); -if (attempts < 10) +if (password == actualPassword) { WriteLine("Correct!"); } else { - WriteLine("You have used 10 attempts!"); + WriteLine("You have used {0} attempts! The password was {1}.", + arg0: maximumAttempts, arg1: actualPassword); } // Looping with the for statement