Hi, I have a homework assignment that I am trying to accomplish and I can't seem to be able to complete.
Here are the instructions for the assignment http://www.carloscruz.info/week4_ilab.pdf
This has been discussed in this forum before, but since I can't plagerize I cant just copy it. I've however modified the program with "inspiration" from the former. Here is the link to that discussion http://www.daniweb.com/software-development/csharp/threads/342803/c-coding-question-for-newbie

Below is the code that I've managed to write so far. Any sugguestions and fixes will be greatly appreciated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //DEFINE VARIABLES
            char cD0, cD1, cD2, cD3, cD4, cD5, cD6, cD7;
            string sPhone;
            int iError, iCount;

            do
            {
                //RESET COUNTER
                iCount = 0;
                iError = 0;
                //ASK FOR INPUT
                Console.WriteLine("Please enter a phone number: ");
                //STORE INPUT
                sPhone = Console.ReadLine();
                //ASSIGN EACH CHAR INTO A VARIABLE
                sPhone = sPhone.Substring(0, 8);
                cD0 = sPhone[0];
                cD1 = sPhone[1];
                cD2 = sPhone[2];
                cD3 = sPhone[3];
                cD4 = sPhone[4];
                cD5 = sPhone[5];
                cD6 = sPhone[6];
                cD7 = sPhone[7];

                //SEND EACH DIGIT WITH COUNTER BY REF
                ErrorCheck (iError, ref cD0, ref iCount);
                iCount++;
                ErrorCheck (iError, ref cD1, ref iCount);
                iCount++;
                ErrorCheck (iError, ref cD2, ref iCount);
                iCount++;
                ErrorCheck (iError, ref cD3, ref iCount);
                iCount++;
                ErrorCheck (iError, ref cD4, ref iCount);
                iCount++;
                ErrorCheck (iError, ref cD5, ref iCount);
                iCount++;
                ErrorCheck (iError, ref cD6, ref iCount);
                iCount++;
                ErrorCheck (iError, ref cD7, ref iCount);
                iCount++;

                //ERROR CODES
                switch (iError)
                {
                    case -1:
                        Console.WriteLine("ERROR! | Syntax Error, an invalid character was entered (1)");
                        break;
                    case -2:
                        Console.WriteLine("ERROR! | Phone numbers cannot begin with 0 (2)");
                        break;
                    case -3:
                        Console.WriteLine("ERROR! | Phone numbers cannot begin with the sequence 555 (3)");
                        break;
                    case -4:
                        Console.WriteLine("ERROR! | A hypen was not in the expected location. (4)");
                        break;
                    default:
                        break;
                }
            }
        }
        static int ErrorCheck(int iError, ref char digit, ref int count)
        {
            //RETURN ERROR CODE 2 IF FIRST CHAR IS ZERO
            if ((count == 0) & (digit == '0'))
            {
                iError = -2;
            }
            //RETURN ERROR CODE 3 IF FIRST THREE DIGITS ARE FIVE
            else if (((count == 0) & (digit == '5')) & ((count == 1) & (digit == '5')) & ((count == 2) & (digit == '5')))
            {
                iError = -3;
            }
            //IF FOURTH DIGIT ISNT A HYPEN RETURN ERROR
            else if ((count == 3) & (digit != '-'))
            {
                iError = -4;
            }
            else if ((count == 0) & (digit == 'Q'))
            {
                iError = -5;
            }
            else if (count != 3)
            {
                DialerMatrix(digit, ref iError);
            }
            else return iError;       
        }
        static char DialerMatrix(char digit, ref int iError)
        {
            //CONVERT ALL TO UPPER CASE
            Char.ToUpper(digit);
            //DATA MATRIX
            char result;
            switch (digit)
            {
                case '0': result = '0'; break;
                case '1': result = '1'; break;
                case '2': case 'A': case 'B': case 'C': result = '2'; break;
                case '3': case 'D': case 'E': case 'F': result = '3'; break;
                case '4': case 'G': case 'H': case 'I': result = '4'; break;
                case '5': case 'J': case 'K': case 'L': result = '5'; break;
                case '6': case 'M': case 'N': case 'O': result = '6'; break;
                case '7': case 'P': case 'Q': case 'R': case 'S': result = '7'; break;
                case '8': case 'T': case 'U': case 'V': result = '8'; break;
                case '9': case 'W': case 'X': case 'Y': case 'Z': result = '9'; break;
                default: result = 'e'; break;
            }
            //NON-ALPHANUMERIC CHAR PRESENT
            if (result == 'e')
            {
                iError = -1;
            }
            //NO ERROR PRESENT
            else
            {
                iError = 0;
            }
            result = digit;
            return digit;
        }
    }
}

Thanks!

Edited 4 Years Ago by rstoplabe14

Your ErrorCheck method returns a value, but you never accept the value so it might as well not return one.
You check iError in line 57 to see if it is one of your error codes, but since it never gets changed from the initial value, it will always be zero.
No idea why you are sending those values by reference in the method in line 76. You never change those values. And using ref variables should be something you do because you have to, not because you can.

This article has been dead for over six months. Start a new discussion instead.