Solved

C# not returning value

Posted on 2011-09-25
4
334 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
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:Slimfinger
Comment Utility
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
Comment Utility
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:
Slimfinger earned 500 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now