Home > Software engineering >  Why isn’t a string resetting when I recall a method as part of an error check?
Why isn’t a string resetting when I recall a method as part of an error check?

Time:11-28

As a beginner in c#, I’ve been making some simple console apps. In one, I have a simple game to solve a computer-generated number code. For each attempt at solving the code, the user enters 5 digits in the form of an input string. The issue is with the error checking, and I’ve put together a simple section of code that shows the problem. If I enter 5 digits, say 12345, the code passes through the errorCheck() and is displayed correctly. If instead, I press a random key, say “a”, the error is recognised, and I can enter again. But this time if I enter a valid number, the output will be “a”.

What I am trying to do is reject an invalid input and replace with a valid one that can be passed to the rest of the program.

Why is this code not working the way I expected?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ErrorCheck
{
    internal class Program
    {
        static void Main(string[] args)
        {           
            string code=enterCode();
            errorCheck(code);
            Console.WriteLine(code);
            Console.ReadLine();
        }
    
    private static string enterCode()
        {
            Console.Write("Please enter code ");
            string code = Console.ReadLine();
            return code;
        }

        private static void errorCheck(string code)
        {
            bool valid = true;
            int number;
            valid = Int32.TryParse(code, out number);
            if (number > 99999) valid = false;
            if (number < 0) valid = false;
            if (valid == false)
            {
                Console.WriteLine("You did not enter a valid code");
                Console.WriteLine("Press any key to try again");
                Console.ReadLine();
                enterCode();
            }
            return;
        }
    }
}

CodePudding user response:

The "code" you specify in errorCheck does not get passed to your main function. Inside of erroCheck you call enterCode again, but this doesn't assign it to anything, so the variable "code" in main remains the same as it was on the failed attempt.

I'm not sure if you know about do-while loops already, but it may be a good idea to rely on a while loop and basically do something like this:

string code;
do
{
    code = enterCode();
} while (errorCheck(code) == false) // or !errorCheck(code)

// Should return true or false depending on success
private static bool errorCheck() 
{
    ...
}

That way, no matter how many times the code is wrong, it will keep checking. With the additional benefit of fixing your bug in the process.

Some additional unwanted advice: method names in C# should start with an upper case letter LikeThis(). Good luck on your coding journey!

CodePudding user response:

Using enter image description here

  • Related