• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

C# not returning value

Experts,

PROGRAM DETAILS
I have a telephone program that gets 7 characters from the user. The program will perform a conversion per a standard telephone keypad layout.
0 - nothing or error (see Errors below)
1- nothing
2 - A  B  C
3 - D  E  F
4 - G  H  I
5 - J K L
6 - M  N  O
7 - P  Q  R  S
8 - T  U  V
9 - W  X  Y  Z

ERRORS
Input Errors that needs to shown if the condition is true
- The first character is 0 (seven-digit phone numbers can't start with 0 ).
- The first three characters are "555" (no phone numbers start with 555).
- Any character is not a digit or an uppercase or lowercase letter

EXPECTED OUTPUT
Enter a 7 character phone number:  2132121
The converted phone number is:  213-2121

Enter a 7 character phone number:  2scdfER
The converted phone number is:  272-3337

Enter a 7 character phone number:  555resw
Invalid input, please try again.

Enter a 7 character phone number:  0988765
Invalid input, please try again.

PROBLEM
if I place numbers or letters it will output the converted phone number, however if I place a 0 as the first character or 555 or other 'special character' It will not run return the error values (see code below). What am I doing wrong? How do I get the 555 or special characters to jump up the return -1 and display the WriteLine message

Thank you very much


using System;


namespace telephone
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Declare character variables for Phone Dial Program
            char d1 = ' ', d2 = ' ', d3 = ' ', d4 = ' ', d5 = ' ', d6 = ' ', d7 = ' ';

            //returnValue holds the error code from value returned from ProcessInput()
            int returnValue;

            //Updates multiple values as ref parameters
            GetInput(ref d1, ref d2, ref d3, ref d4, ref d5, ref d6, ref d7);

            returnValue = ProcessInput(ref d1, ref d2, ref d3, ref d4, ref d5, ref d6, ref d7);

            if (returnValue == -1)
                Console.WriteLine("Invalid input, please try again.");

            else
                ShowResults(ref d1, ref d2, ref d3, ref d4, ref d5, ref d6, ref d7);

            Console.WriteLine();
            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }

        //reads user input digits or letters passed by ref
        static void GetInput(ref char d1, ref char d2, ref char d3, ref char d4, ref char d5, ref char d6,
                                     ref char d7)
        {
            Console.Write("Enter a 7-digit letter or number phone number: ");

            d1 = Convert.ToChar(Console.Read());
            d2 = Convert.ToChar(Console.Read());
            d3 = Convert.ToChar(Console.Read());
            d4 = Convert.ToChar(Console.Read());
            d5 = Convert.ToChar(Console.Read());
            d6 = Convert.ToChar(Console.Read());
            d7 = Convert.ToChar(Console.Read());

            Console.ReadLine();
        }

        //Use return value to indicate a valid (return 0) or invalid (return -1)
        static int ProcessInput(ref char d1, ref char d2, ref char d3, ref char d4, ref char d5, ref char d6,
                                        ref char d7)
        {
            if (ToDigit(ref d1) == -1)
                return -1;

            if (ToDigit(ref d2) == -1)
                return -1;

            if (ToDigit(ref d3) == -1)
                return -1;

            if (ToDigit(ref d4) == -1)
                return -1;

            if (ToDigit(ref d5) == -1)
                return -1;

            if (ToDigit(ref d6) == -1)
                return -1;

            if (ToDigit(ref d7) == -1)
                return -1;
            

            return 0;
        }

        //validates a single digit or letter 
        //returns -1 for invalid entry
        //returns 0 for valid entry
        static int ToDigit(ref char d)
        {
            d = Char.ToUpper(d);
            //using switch on char values with multiple cases executing same code
            switch (d)
            {
                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':
                    d = '2';
                    break;
                case 'D':
                case 'E':
                case 'F':
                    d = '3';
                    break;
                case 'G':
                case 'H':
                case 'I':
                    d = '4';
                    break;
                case 'J':
                case 'K':
                case 'L':
                    d = '5';
                    break;
                case 'M':
                case 'N':
                case 'O':
                    d = '6';
                    break;
                case 'P':
                case 'R':
                case 'S':
                    d = '7';
                    break;
                case 'T':
                case 'U':
                case 'V':
                    d = '8';
                    break;
                case 'W':
                case 'X':
                case 'Y':
                    d = '9';
                    //return 0;
                    break;

                default:
                    return -1;
            }
            return 0;
        }
    static void ShowResults(ref char d1, ref char d2, ref char d3, ref char d4, ref char d5, ref char d6, ref char d7)
        {
            Console.WriteLine("The converted phone number is: " + d1 + d2 + d3 + d4 + d5 + d6 + d7);

            Console.WriteLine();
        }

    }
}

Open in new window

0
Michael Reeve
Asked:
Michael Reeve
  • 2
  • 2
1 Solution
 
Bill NolanOwner, Lead Technology ProgrammerCommented:
You say "It will not run return the error values".  What does this mean?

Will it not run, and returns an error?  What error?
Will it not return error values?

I don't see anywhere in your code that you are checking for an invalid '0' first char, or "555" prefix.
0
 
Michael ReeveAuthor Commented:
Good point, I wasn't very clear about the return of the errors. I don't know how I can effectively check for the first character(s) being a zero OR the first three letters starting with 555 then defaulting to the invalid input message (the WriteLine already in the code). It is working for special characters just not the other two mentioned possible errors.
0
 
Bill NolanOwner, Lead Technology ProgrammerCommented:
Ok, so your question is how to check for those characters/sequences.

Lots of ways to do that.  Quickly, for your code, probably just add the conditions to your "ProcessInput()" function:

if (d1 == '0') return -1;

if (d1 == '5' && d2 == '5' && d3 == '5') return -1;
0
 
Michael ReeveAuthor Commented:
That's it! Thank you so much! I really appreciate your time and help with this problem. You have no idea how much your assistance helped me out. Please accept my sincere appreciation for making the solution understandable and complete. Your expertise in this subject is what makes this website successful!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now