//The purpose of the program is to simulate dialing a phone number and having it convert to a numeric format with a hyphen.
//We're not allowed to set the characters to a string or to use arrays, I'm having trouble entering an if statement that would return -1 if I enter less than 7 characters or greater than 7 characters
//This statement would then let me validate my program. My other problem is where should I make a loop for my main(). Could this be accomplished with the context of my code?


static void GetInput(ref char N1, ref char N2, ref char N3, ref char N4, ref char N5, ref char N6, ref char N7)
        {   
            //Gathering input
            Console.Write("Enter a 7 character phone number: ");
            N1 = Convert.ToChar(Console.Read());
            N2 = Convert.ToChar(Console.Read());
            N3 = Convert.ToChar(Console.Read());
            N4 = Convert.ToChar(Console.Read());
            N5 = Convert.ToChar(Console.Read());
            N6 = Convert.ToChar(Console.Read());
            N7 = Convert.ToChar(Console.Read());
            Console.ReadLine();
        }
        static int ProcessInput(ref char N1, ref char N2, ref char N3, ref char N4, ref char N5, ref char N6, ref char N7)
        {
            //validates my code
            if (ToDigit(ref N1) == -1)
                return -1;
            if (ToDigit(ref N2) == -1)
                return -1;
            if (ToDigit(ref N3) == -1)
                return -1;
            if (ToDigit(ref N4) == -1)
                return -1;
            if (ToDigit(ref N5) == -1)
                return -1;
            if (ToDigit(ref N6) == -1)
                return -1;
            if (ToDigit(ref N7) == -1)
                return -1;
            // first character can't start with 0
            if (N1 == '0')
                return -1;
            // no phone number can start with 555
            if (N1 == '5' && N2 == '5' && N3 == '5')
                return -1;

            return 0;
        }
        static int ToDigit(ref char N)
        {
            N = char.ToUpper(N); 
            switch (N)
            {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9': break;
                case 'A':
                case 'B':
                case 'C': N = '2'; break;
                case 'D':
                case 'E':
                case 'F': N = '3'; break;
                case 'G':
                case 'H':
                case 'I': N = '4'; break;
                case 'J':
                case 'K':
                case 'L': N = '5'; break;
                case 'M':
                case 'N':
                case 'O': N = '6'; break;
                case 'P':
                case 'Q':
                case 'R':
                case 'S': N = '7'; break;
                case 'T':
                case 'U':
                case 'V': N = '8'; break;
                case 'W':
                case 'X':
                case 'Y':
                case 'Z': N = '9'; break;
                default: return -1; break;                
            }
            return 0;
        }
        static void ShowResults(ref char N1, ref char N2, ref char N3, ref char N4, ref char N5, ref char N6, ref char N7)
        {
            Console.WriteLine("The converted phone number is: " + N1 + N2 + N3 + "-" + N4 + N5 + N6 + N7);
            //displa
        }
        static void Main(string[] args)
        {
            char N1 = 'a', N2 = 'a', N3 = 'a', N4 = 'a', N5 = 'a', N6 = 'a', N7 = 'a';
            GetInput(ref N1, ref N2, ref N3, ref N4, ref N5, ref N6, ref N7);
            int returnval = ProcessInput(ref N1, ref N2, ref N3, ref N4, ref N5, ref N6, ref N7);
            if (returnval == -1)
                Console.WriteLine("Invalid input, please try again.");
            else
                ShowResults(ref N1, ref N2, ref N3, ref N4, ref N5, ref N6, ref N7);
            Console.ReadLine();
        }
    }
}

Dani AI

Generated

The core problem is input handling. Because 's GetInput calls Console.Read seven times, the routine will block until seven characters arrive, so ProcessInput never sees "too few" characters — it only sees exactly what was read. and are right that reading the whole line makes validation much easier. Two practical options follow: a simple string-based fix (preferred) and a no-strings alternative that keeps the current ref-char signature.

If strings/arrays are allowed, read the whole line, remove optional hyphens/spaces, check length, then validate/convert each character (calling the existing ToDigit mapping). Example pattern:

string line = Console.ReadLine().Trim();
string cleaned = new string(line.Where(ch => ch != '-' && !char.IsWhiteSpace(ch)).ToArray());
if (cleaned.Length != 7) { Console.WriteLine("Enter exactly 7 characters."); /* loop */ }
for (int i = 0; i < 7; i++) {
    char c = cleaned[i];
    if (ToDigit(ref c) == -1) { /* invalid */ }
    // store converted digit
}

If strings/arrays are explicitly forbidden, change GetInput so it reads characters until the user presses Enter, counts them, fills the seven ref parameters in order, and returns the count. That lets ProcessInput check count != 7 and return -1. Example pattern:

int GetInput(ref char N1,...,ref char N7) {
  int count = 0;
  while (true) {
    int r = Console.Read();
    if (r == -1 || r == '\n') break;
    if (r == '\r') continue;
    char ch = (char)r;
    switch (count) { case 0: N1 = ch; break; case 1: N2 = ch; break; /* ... */ }
    count++;
  }
  return count;
}

Place a loop in Main that calls GetInput, checks the returned count (or cleaned string length), then calls ProcessInput; repeat on error until valid input is supplied. Extra tips: filter out carriage returns, explicitly ignore whitespace/hyphens if desired, and keep validation (first digit not '0', avoid 555 prefix) close to input so the program can re-prompt cleanly.

Recommended Answers

All 2 Replies

Why don't you allow the user to enter the entire number in one go, and then convert it to a Char[]

You can then look at the Char[].Length to see how many they have entered?

Member Avatar for Member #1091071

Mike Askew's answer is exactly what I was going to post.
Don't forget that a string is just an array of characters, and can be manipulated in exactly the same way as a standard array.

As to your question, as you've hard coded the input, it's not exactly possible with your code to enter any more or less than 7 characters...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.