Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# not returning value

Posted on 2011-09-25
4
Medium Priority
?
348 Views
Last Modified: 2012-05-12
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
Comment
Question by:Michael Reeve
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:Bill Nolan
ID: 36596539
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
 

Author Comment

by:Michael Reeve
ID: 36596589
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
 
LVL 7

Accepted Solution

by:
Bill Nolan earned 2000 total points
ID: 36596601
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
 

Author Closing Comment

by:Michael Reeve
ID: 36596870
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question