C# looping help!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: C# looping help!

  1. #1
    Join Date
    Mar 2013
    Posts
    1

    C# looping help!

    Hi, I am not very good at programming, which you will be able to tell with how simple this problem is. (This is not an actual homework assignment, just a similar one so I can try to figure it out) But here is the problem.

    People sometimes give their telephone numbers using one or more alphabetic characters. Write a program that accepts a 10 digit telephone number that may contain one or more alphabetic characters. Display the corresponding number using numbers. The numbers and letters are associated as follows on your telephone:
    ABC: 2
    DEF: 3
    GHI: 4
    JKL: 5
    MNO: 6
    PQRS: 7
    TUV: 8
    WXYZ: 9

    If the user enters a character that is not on the telephone as a part of the number, display a message indicating that the value does not match a number. Allow both upper and lowercase characters to be entered.

    Here is my embarassing work that I have so far. Like I said I am very bad at programming and would love some help! Feel free to start from scratch if you can't build off my work.
    Code:
    using System;
    
    class DoLoop
    {
        public static void Main()
        {
            string myChoice;
    
            do
            {
                
                Console.WriteLine("Telephone Key\n");
    
                Console.WriteLine("ABC = 2");
                Console.WriteLine("DEF = 3");
                Console.WriteLine("GHI = 4");
                Console.WriteLine("JKL = 5");
                Console.WriteLine("MNO = 6");
                Console.WriteLine("PQRS = 7");
                Console.WriteLine("TUV = 8");
                Console.WriteLine("WXYZ = 9");
    
                Console.WriteLine("Please enter a 10 - digit telephone number using the letters above.");
    
                
                myChoice = Console.ReadLine();
    
                // Make a decision based on the user's choice
                switch (myChoice)
                {
                    case "ABC":
                    case "abc":
                        Console.WriteLine("2");
                        break;
                    case "DEF":
                    case "def":
                        Console.WriteLine("3");
                        break;
                    case "GHI":
                    case "ghi":
                        Console.WriteLine("4");
                        break;
                    case "JKL":
                    case "jkl":
                        Console.WriteLine("5");
                        break;
                    case "MNO":
                    case "mno":
                        Console.WriteLine("6");
                        break;
                    case "PQRS":
                    case "pqrs":
                        Console.WriteLine("7");
                        break;
                    case "TUV":
                    case "tuv":
                        Console.WriteLine("8");
                        break;
                    case "WXYZ":
                    case "wxyz":
                        Console.WriteLine("9");
                        break;
                    default:
                        Console.WriteLine("The value you have entered does not correspond with a number.", myChoice);
                        break;
                }
    
                // Pause to allow the user to see the results
                Console.Write("press Enter key to continue...");
                Console.ReadLine();
                Console.WriteLine();
            } while (myChoice != "Enter" && myChoice != "enter"); // Keep going until the user wants to quit
        }
    }

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,101

    Re: C# looping help!

    The problem is not with your looping. The problem exists with your Switch statement.

    If I type in ABC it gives me 2

    Whereas if I type A it gives me your error message : The value you have entered does not correspond with a number.

    You have to have separate entities for A and B and C and so on...

    I'd suggest using a dictionary, hashtable object where you can supplu a key value pair for your values.

    So your homework is to look at MSDN's examples, or look here : http://www.codeguru.com/columns/vb/w...es-in-.net.htm

  3. #3
    Join Date
    Jun 2001
    Location
    USA
    Posts
    286

    Re: C# looping help!

    A couple of suggestions...

    You've prompted the user to enter 10 digits. The input string myChoice will be (should be) 10 characters long. You only need to input that once, unless there are not 10 chars. Then you need to parse the input string for each character and do your case statement char by char. A foreach loop is perfect for that.

    Also, the default case will write a line for every bad entry. So with 4 illegal characters you'll print the message 4 times. A more elegant method would be count the bad entries as they are found and display a message at the end if there were any.

    Code:
    static void Main(string[] args)
            {
                string myChoice;
                int str_length = 0;
                int bad_entries = 0; // count illegal entries
                do
                {
                    
                    Console.WriteLine("Telephone Key\n");
    
                    Console.WriteLine("A,B,C = 2");
                    Console.WriteLine("D,E,F = 3");
                    Console.WriteLine("G,H,I = 4");
                    Console.WriteLine("J,K,L = 5");
                    Console.WriteLine("M,N,O = 6");
                    Console.WriteLine("P,Q,R,S = 7");
                    Console.WriteLine("T,U,V = 8");
                    Console.WriteLine("W,X,Y,Z = 9");
    
                    Console.WriteLine("Please enter a 10 - digit telephone number using the letters above.");
    
                    
                    myChoice = Console.ReadLine(); // only need to do this once, for a 10 digit string
                    str_length = myChoice.Length;
                    // if not 10 digits, error
                    if (str_length != 10) Console.WriteLine("Entry must be 10 digits");
                }while(str_length != 10);
    
                    
                // Make a decision based on the user's choice
                // For each character in the input string
                foreach (Char c in myChoice)
                {
                    switch (c)
                    {
                        case 'A':
                        case 'B':
                        case 'C':
                        case 'a':
                        case 'b':
                        case 'c':
                            Console.Write("2 ");
                            break;
                        case 'D':
                        case 'E':
                        case 'F':
                        case 'd':
                        case 'e':
                        case 'f':
                            Console.Write("3 ");
                            break;
                        // etc for the rest of the alphabet
                        default:
                            Console.Write("X "); // to mark a bad entry
                            bad_entries++; // count them
                            break;
                    }
                }
                
                // if any bad entries, say so
                if (bad_entries > 0) Console.WriteLine("You entered " + bad_entries.ToString() + " bad chars"); 
                // Pause to allow the user to see the results
                Console.Write("\r\npress Enter key to continue...");
                Console.ReadLine();
                Console.WriteLine();
            }
    Using Write rather than WriteLine will put the numbers all on one line.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center