1

I'm trying to have the program check the user entry in chars against the Password "prog". The user has three tries to get the right Password. However it often gives the answer "right Password" although it doesnt match.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            int tries = 0;
            bool valid = false;
            bool p, r, o, g;
            char userInput;
            int characters;
 
            while (tries < 3 && valid == false)
            {
                Console.WriteLine("Please enter password:");
                p = r = o = g = false;
                characters = 0;
                while (characters < 4)
                {
                    userInput = Console.ReadKey().KeyChar;
                    if (userInput == 'p' || userInput == 'P') p = true;
                    else if (userInput == 'r' || userInput == 'R') r = true;
                    else if (userInput == 'o' || userInput == 'O') o = true;
                    else if (userInput == 'g' || userInput == 'G') g = true;
                    characters++;
                }
 
                if (p == r == o == g == true)
                {
                    valid = true;
                }
                tries++;
            }
 
            if (valid == true)
            {
                Console.WriteLine("\nright password");
            }
            else Console.WriteLine("\nwrong password");
            Console.ReadLine();
        }
    }
}
zom4
  • 21
  • 3

1 Answers1

1

Change your if statement to

if (p == true && r == true &&  o == true && g == true)
{
    valid = true;
}

Or because your boolean values are set to true you can write something like this there is no need to write the true:

if(p && r && o && g) valid = true;

Id change the program to look something similar to this:

        var tries = 0;

        while (tries < 3)
        {

            Console.Write("Please enter password: ");                
            var userInput = Console.ReadLine();
            if (userInput == "prog")
            {
                Console.WriteLine("\nright password");
                Console.ReadKey();
                break;
            }
                Console.WriteLine("\nwrong password, please press enter to try again");
                Console.ReadKey();
                Console.Clear();

                tries++;      
       }  
Nicole Phillips
  • 753
  • 1
  • 18
  • 41
  • I agree, but I would probably go with: if(p&&r&&o&&g){...} – phishfordead Nov 04 '16 at 15:40
  • Thanks a lot, it worked now! The Task was to do it with chars so that it would still work to enter "PRoG" or "ropr" just that the letters are the same, so i couldnt just compare it with "prog". But this way it works great thanks!! – zom4 Nov 04 '16 at 16:04
  • @zom4 no problem if it works please accept the answer :) – Nicole Phillips Nov 04 '16 at 16:05