VitaminD
VitaminD

Reputation: 33

How do I check if user input is a number and if it's the same as another variable in the same while loop?

I'm trying to implement a little of everything I have learned so far in two weeks of C# into a mock ATM type of app. So keep in mind while there are better ways of doing my code or simplifying it, I'm trying to stick to what I know for now so I can understand it better.

  1. So here's what I'm trying to do at the moment: User enters a username (userName) and I need to check if it matches correctUserName, which I know to do with an if statement, however, I need it to also be able to recognize if the user has entered a string and if anything else it will give an error. With an int I know I can try int.TryParse, I don't know what the string equivalent of that is.
  2. User inputs pin (pinCode) and similar to above, but I know how to check if that's a number and not a string and compare it to correctPinCode; I don't understand how to display two different error messages based on that condition. Where one error message if the user didn't enter a numerical value(pinNumericalError) and the other if the pin just didn't match(pinError). I'm assuming two "if statements" in the else statement (is that even possible?), but I don't know what the syntax looks like with the TryParse condition above it.
  3. Then once both userName and pinCode are correct (based on the conditions above) I can proceed to a menu or whatever it is I plan to do next. Do both go nested in a parent while loop or something?
namespace ATM
{
    class AtmLogin
    {
        static void Main()
        {
            //User input
            string userName;
            int pinCode;

            //Correct values to proceed
            string correctUserName = "Chad Warden";
            int correctPinCode = 6969;
            
            //Error messages
            string userNameError = "Enter a valid username";
            string pinError = "Enter a valid pin";
            string pinNumericalError = "Enter a valid numerical pin";
            string namePinError = "Either your username or pin was invalid, try again.";
            
            //Welcome message
            Console.WriteLine("Welcome to PSTriple Banking!");   
         
            //Username prompt - checks if userName is a string and not a number or anything else and compares it to correctUserName
            Console.WriteLine("User name:");
            userName = Console.ReadLine();
            
            //Pin prompt - checks if pinCode is a number and not a string or anything else and compares it to correctPinCode    
            while (true)
            {
                Console.WriteLine("Pin:");
                if (int.TryParse(Console.ReadLine(), out pinCode) && pinCode == correctPinCode) {
                    
                    break;
                }
                else
                {
                    
                    Console.WriteLine(pinError);
                }
            }

          

            Console.ReadKey();
        }
    }
}

Upvotes: 0

Views: 1041

Answers (1)

ShanieMoonlight
ShanieMoonlight

Reputation: 1861

A username must have at least one letter but a pin must have zero letters so you could use this answer from stackoverflow

  bool isIntString = "your string".All(char.IsDigit)

Will return true if the string is a number

bool isIntString = !"your string".All(char.IsDigit)

Will return true if the string contains at least one non digit

https://stackoverflow.com/a/18251942/2826614

Upvotes: 1

Related Questions