//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();
        }
    }
}

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 humorousone

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.