-1

So for this C# class I have to have the user input a 7 digit phone number into the console. Each number is stored as a different character. Then each character is changed to a letter. There is no logic behind the letters the numbers are changed to. (I really don't see the point of this assignment, which might be contributing to my coder's block.) Here's what the assignment says.

Your mission: A prepaid phone service needs a program that converts alphanumeric keyboard input into a phone number. The user will input eight characters and the program will output either an error message or the translated seven-digit phone number. The input may contain digits, letters, or both. Letters can be uppercase or lowercase.

Method of Coding:

  • Main(): Declares seven character variables and passes these to the following methods by reference:
    • ProcessInput(): gets user input and performs the conversion
      • ShowResults(): displays the results
        • GetInput(): Gets seven characters from the user and stores them into the seven variables Main() has passed by reference.
        • ProcessInput(): Calls ToDigit() for each, passing each character variable by reference, and returns one of these codes to Main() by value: o 0 if there were no input errors o -1 if there were input errors"

The program will perform the conversion per a standard telephone keypad layout.

Basically, 2 s A,B,C 3 is D,E,F Etc and then D, E, F is 3 and so on.

Right now I have nothing done except for an input, but it's not stored as characters, just a string. I really hate this assignment because we just did two assignments with Cases and If statements, it just seems redundant to me.

Console.WriteLine("Write a Phone Number that consists of Seven Numbers.");
string Number = Console.ReadLine();

if (Number.Length != 7)
    Console.WriteLine("You have entered a phone number that is too long.");

Console.WriteLine("You have entered: {0}", Number);
Console.ReadLine();

So my real question is: How do I store the input number as a character and then define those characters with case statements?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
user1580598
  • 53
  • 1
  • 2
  • 9

5 Answers5

1

Do not use a Dictionary class if it has not been covered in class, you will likely get a bad grade on the assignment as this homework is to be solved using the tools you learned in class.

There are two ways you could solve this.

  1. Make a switch statement for every letter or number a person could enter. This will likely get you a bad grade if you make a case for every letter of the alphabet and enter it's corresponding number, but if you use some of the features of case statements like "fall through" (no link, I leave it to you to research what that is) you will likely get a good grade.

  2. This can also be done with if statements, and just like point 1, if you make a if for every entry you will get a bad grade, but if you can figure out how to solve A, B, C, and 1 with one if you may get a good grade.


I will give you the answer for how to get each letter individually because I don't think that is the purpous of the lesson, the easist way is to do it with a foreach statement.

string finalNumber = "";
foreach(char digit in Number)
{
  finalNumber += ProcessDigit(digit);
}
Console.WriteLine(finalNumber);

//Elsewhere
char ProcessDigit(char digit)
{
     char newDigit;

     //change digit to whatever you needed to turn it in to and store it in newDigit;

     return newDigit;
}

Also another freebee, do a ToUpperInvariant() on the string before you feed it in to the foreach and you only need to check capital letters.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • So how exactly do I make it so the entered numbers can be ran across the switch/if statements? – user1580598 Aug 07 '12 at 01:12
  • Updated with the answer as I don't think it is the intent of the homework as you described it is to test you on `foreach` statements. – Scott Chamberlain Aug 07 '12 at 01:14
  • I don't think we're allowed to use a foreach statement either :'[ – user1580598 Aug 07 '12 at 01:15
  • I updated the question with a sort of "guide" we're suppose to be using. – user1580598 Aug 07 '12 at 01:17
  • If you can't use foreach then you must use a `for` with the indexing I showed in the comment in the original question, however this is the end of the handholding I will do and you will need to figure the rest of the problem out yourself. "Give a man a fish..." – Scott Chamberlain Aug 07 '12 at 01:20
0

Since this is homework, some steps you should do:

  1. Create a map of letters to numbers as corresponds to the keypad on the telephone
  2. Loop through each character of the entered string:
    1. If its a number (an integer), then look it up in the letter > number mapping, print the character.
    2. If its a letter, then look up the same table, and print the corresponding number.
    3. If it doesn't match the two conditions, print an error message.
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

You could just create a dictionary that maps all possible input characters to your desired output. Upon input you replace the character based on the dictionary.

Jan
  • 2,168
  • 2
  • 19
  • 28
0

this should work

        Dictionary<string, int> dict = new Dictionary<string, int>();
        dict.Add("ABC", 2);
        dict.Add("CDE", 3);
        dict.Add("FGH", 4);
        dict.Add("JKL", 5);

        string Numbers = "BDJ";
        string myints = "";
        foreach (char c in Numbers)
            myints += dict.FirstOrDefault(X => X.Key.Contains(c)).Value.ToString();
//the output : "235"
S3ddi9
  • 2,121
  • 2
  • 20
  • 34
0

Really sorry for not responding to this or picking a right answer, I eventually figured it out on my own. The reason I was having so much trouble was because I thought I was suppose to create several outputs for 1(800)384-2347 and convert the last 4 numbers into various letters that might make sense. But it was just a simple case statement.

user1580598
  • 53
  • 1
  • 2
  • 9