For some reason, the variables I try to pass by value (errorFlag between Main() and ReadDials() and testDigit between ToDigit() and ReadDials()) don't get passed. The variables passed by reference get passed fine. As a work-around I passed a temp variable for each and copied the value from the temp reference variable to the value passed variable in each method, but I'm confused as to why I'm not getting the 'by value' value passed.

Sorry if this is a stupid question.

Thanks.

Jim

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

namespace Lab4x
{
    class Program
    {
        // AcknowledgeCall Method - Write modified phone number to console
        static void AcknowledgeCall(char digitOne, char digitTwo, char digitThree,
               char digitFour, char digitFive, char digitSix, char digitSeven, char digitEight)
        {
            Console.WriteLine();
            Console.WriteLine("{0}{1}{2}{3}{4}{5}{6}{7}", digitOne, digitTwo, digitThree, digitFour, digitFive, digitSix, digitSeven, digitEight);
            Console.WriteLine();
            return;
        }
        // ReadDials Method  - Read phone number string and store in variables
        static int ReadDials(int errorFlag, ref int errorHolder, ref string phoneNumber, ref char digit1, ref char digit2, ref char digit3, ref char digit4, ref char digit5,
            ref char digit6, ref char digit7, ref char digit8)
        {
            errorHolder = 0;  // Declare local variables
            char holder = 'x';

            errorFlag = 0;
            if ((phoneNumber.Length != 1) & (phoneNumber.Length <= 7))              // Check for invalid phone number length  
            {                                                                       // and flag as invalid characters (null characters)
                errorFlag = -1;
            }
            else
            {
                digit1 = phoneNumber[0];                                                    // Assign first digit of number into 'digit1'

                if (((digit1 == 'Q') | (digit1 == 'q')) & ((phoneNumber.Length == 1)))      // Check for user quit code
                {
                    errorFlag = -5;
                }
                else
                {
                    ToDigit(digit1, ref errorFlag, ref holder);                              // Call ToDigit method 
                    digit1 = holder;
                }

                if (errorFlag == 0)                                                               //   If first character is not flagged for
                {                                                                             //   error, assign second phone number digit
                    digit2 = phoneNumber[1]; ToDigit(digit2, ref errorFlag, ref holder);      //   and call ToDigit method
                    digit2 = holder;
                }

                if (errorFlag == 0)
                {                                                                              //   Check for error, assign third digit,
                    digit3 = phoneNumber[2]; ToDigit(digit3, ref errorFlag, ref holder);       //   and call ToDigit method
                    digit3 = holder;
                }

                if (errorFlag == 0)
                {                                                                              //   Check for error, assign fourth digit,
                    digit4 = phoneNumber[3];                                                  //   should be a hyphen so do not call
                }                                                                              //   ToDigit method

                if (errorFlag == 0)
                {
                    digit5 = phoneNumber[4]; ToDigit(digit5, ref errorFlag, ref holder);      //   Check for error, assign fifth digit,    
                    digit5 = holder;                                                          //   and call ToDidgit method
                }

                if (errorFlag == 0)
                {
                    digit6 = phoneNumber[5]; ToDigit(digit6, ref errorFlag, ref holder);      //   Check for error, assign sixth digit,
                    digit6 = holder;                                                          //   and call ToDigit method
                }

                if (errorFlag == 0)
                {
                    digit7 = phoneNumber[6]; ToDigit(digit7, ref errorFlag, ref holder);      //   Check for error, assign seventh digit,
                    digit7 = holder;                                                          //   and call ToDigit method
                }

                if (errorFlag == 0)
                {
                    digit8 = phoneNumber[7]; ToDigit(digit8, ref errorFlag, ref holder);      //   Check for error, assign eighth digit,
                    digit8 = holder;                                                          //   and call ToDigit method
                }

            }
            if (digit4 != '-')          //  If fourth digit is not a hyphen
            {                           //  return an error code
                errorFlag = -4;
            }

            if (digit1 == '0')          //  If first digit is a zero
            {                           //  return an error code
                errorFlag = -2;
            }

            if ((digit1 == '5') & (digit2 == '5') & (digit3 == '5'))     //  If prefix is '555' 
            {                                                             //  return an error code
                errorFlag = -3;
            }
            errorHolder = errorFlag;
            return errorFlag;
        }

        // Method ToDigit
        static char ToDigit(char testDigit, ref int errorTest, ref char tempHolder)
        {

            testDigit = Char.ToUpper(testDigit);             //   Convert Character to UpperCase

            char result;                                     //   Declare char variable to hold result
            switch (testDigit)
            {
                case '0': result = '0'; break;               //   Match each digit with corresponding
                case '1': result = '1'; break;               //   numerical value using switch command
                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;
            }
            if (result == 'e')
            {
                errorTest = -1;                        //    Return error if non-alphanumeric character is present
            }
            else
            {
                errorTest = 0;                         //    else return 0 
            }



            tempHolder = result;
            return testDigit;
        }
        //  Main Method
        static void Main(string[] args)
        {
            char digit1 = 'o',      // Declare and initialize eight variables to
                 digit2 = 'o',      // hold individual phone number digits
                 digit3 = 'o',
                 digit4 = 'o',
                 digit5 = 'o',
                 digit6 = 'o',
                 digit7 = 'o',
                 digit8 = 'o';

            string phoneNo;        // Declare variables 
            int errorFlag = 0,
            tempErrorCode = 0;


            while (errorFlag != -5)                                                                           // Begin loop
            {
                Console.WriteLine("Enter a phone number.  (Q to quit)");                                  // Get phone number input from user
                phoneNo = Console.ReadLine();
                Console.WriteLine();


                ReadDials(errorFlag, ref tempErrorCode, ref phoneNo, ref digit1, ref digit2, ref digit3,   // pass variables by reference
                                       ref digit4, ref digit5, ref digit6, ref digit7, ref digit8);        // to ReadDials Method
                errorFlag = tempErrorCode;

                switch (errorFlag)
                {
                    case 0:
                        AcknowledgeCall(digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8);
                        break;
                    case -1:
                        Console.WriteLine("ERROR - An invalid character was entered.");                     // Determine output based on
                        break;                                                                              // returned error codes
                    case -2:                                                                                // using switch command
                        Console.WriteLine("ERROR - Phone number cannot begin with 0.");
                        break;
                    case -3:
                        Console.WriteLine("ERROR - Phone number cannot begin with 555.");
                        break;
                    case -4:
                        Console.WriteLine("ERROR - Hyphen is not in the correct position.");
                        break;
                    case -5:
                        Console.WriteLine("Exiting program. ");
                        break;

                }

            }
        }
    }
}

Recommended Answers

All 3 Replies

In ReadDials the first thing you do with errorFlag is set it to zero. You don't check it's value so I'm not sure what the problem is.

Your problem is between lines 87 - 89. I'm not really sure what the requirements are. "If fourth digit is not a hyphen then return an error code?".

When errorflag is passed in to readdials from todigit, it is being set to -4. Therefore causing "ERROR - Hyphen is not in the correct position."

Can you explain exactly what your trying to achieve?

I repaired it, but there is a problem with the code:

if (digit4 != '-') {}

I dont know what you have meant, but if user enters q or Q (to quit the program), the code goes into that if, and changes errorFlag to -4. Thats why user is unable to quit.
I changed the code a bit, now its working.
But still, take a look whats with that if statement.

Code:

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

namespace Jan30Exercise
{
    class Program
    {
        // AcknowledgeCall Method - Write modified phone number to console
        static void AcknowledgeCall(char digitOne, char digitTwo, char digitThree,
               char digitFour, char digitFive, char digitSix, char digitSeven, char digitEight)
        {
            Console.WriteLine();
            Console.WriteLine("{0}{1}{2}{3}{4}{5}{6}{7}", digitOne, digitTwo, digitThree, digitFour, digitFive, digitSix, digitSeven, digitEight);
            Console.WriteLine();
            return;
        }
        // ReadDials Method  - Read phone number string and store in variables
        static int ReadDials(int errorFlag, ref int errorHolder, ref string phoneNumber, ref char digit1, ref char digit2, ref char digit3, ref char digit4, ref char digit5,
            ref char digit6, ref char digit7, ref char digit8)
        {
            errorHolder = 0;  // Declare local variables
            char holder = 'x';

            errorFlag = 0;
            if ((phoneNumber.Length != 1) & (phoneNumber.Length <= 7))              // Check for invalid phone number length  
            {                                                                       // and flag as invalid characters (null characters)
                errorFlag = -1;
            }
            //
            //WHAT IT THE CODE BELLOW: 
            //IF USER ENTERS q OR Q THE CODE COMES INTO HERE! SO THIS WAS YOUR PROBLEM!
            if (digit4 != '-')          //  If fourth digit is not a hyphen
            {                           //  return an error code
                errorFlag = -4;
            }

            if (digit1 == '0')          //  If first digit is a zero
            {                           //  return an error code
                errorFlag = -2;
            }

            if ((digit1 == '5') & (digit2 == '5') & (digit3 == '5'))     //  If prefix is '555' 
            {                                                             //  return an error code
                errorFlag = -3;
            }
            else
            {
                digit1 = phoneNumber[0];                                                    // Assign first digit of number into 'digit1'

                if (((digit1 == 'Q') | (digit1 == 'q')) & ((phoneNumber.Length == 1)))      // Check for user quit code
                {
                    errorFlag = -5;
                }
                else
                {
                    ToDigit(digit1, ref errorFlag, ref holder);                              // Call ToDigit method 
                    digit1 = holder;
                }

                if (errorFlag == 0)                                                               //   If first character is not flagged for
                {                                                                             //   error, assign second phone number digit
                    digit2 = phoneNumber[1]; ToDigit(digit2, ref errorFlag, ref holder);      //   and call ToDigit method
                    digit2 = holder;
                }

                if (errorFlag == 0)
                {                                                                              //   Check for error, assign third digit,
                    digit3 = phoneNumber[2]; ToDigit(digit3, ref errorFlag, ref holder);       //   and call ToDigit method
                    digit3 = holder;
                }

                if (errorFlag == 0)
                {                                                                              //   Check for error, assign fourth digit,
                    digit4 = phoneNumber[3];                                                  //   should be a hyphen so do not call
                }                                                                              //   ToDigit method

                if (errorFlag == 0)
                {
                    digit5 = phoneNumber[4]; ToDigit(digit5, ref errorFlag, ref holder);      //   Check for error, assign fifth digit,    
                    digit5 = holder;                                                          //   and call ToDidgit method
                }

                if (errorFlag == 0)
                {
                    digit6 = phoneNumber[5]; ToDigit(digit6, ref errorFlag, ref holder);      //   Check for error, assign sixth digit,
                    digit6 = holder;                                                          //   and call ToDigit method
                }

                if (errorFlag == 0)
                {
                    digit7 = phoneNumber[6]; ToDigit(digit7, ref errorFlag, ref holder);      //   Check for error, assign seventh digit,
                    digit7 = holder;                                                          //   and call ToDigit method
                }

                if (errorFlag == 0)
                {
                    digit8 = phoneNumber[7]; ToDigit(digit8, ref errorFlag, ref holder);      //   Check for error, assign eighth digit,
                    digit8 = holder;                                                          //   and call ToDigit method
                }

            }

            errorHolder = errorFlag;
            return errorFlag;
        }

        // Method ToDigit
        static char ToDigit(char testDigit, ref int errorTest, ref char tempHolder)
        {

            testDigit = Char.ToUpper(testDigit);             //   Convert Character to UpperCase

            char result;                                     //   Declare char variable to hold result
            switch (testDigit)
            {
                case '0': result = '0'; break;               //   Match each digit with corresponding
                case '1': result = '1'; break;               //   numerical value using switch command
                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;
            }
            if (result == 'e')
            {
                errorTest = -1;                        //    Return error if non-alphanumeric character is present
            }
            else
            {
                errorTest = 0;                         //    else return 0 
            }



            tempHolder = result;
            return testDigit;
        }
        //  Main Method
        static void Main(string[] args)
        {
            char digit1 = 'o',      // Declare and initialize eight variables to
                 digit2 = 'o',      // hold individual phone number digits
                 digit3 = 'o',
                 digit4 = 'o',
                 digit5 = 'o',
                 digit6 = 'o',
                 digit7 = 'o',
                 digit8 = 'o';

            string phoneNo;        // Declare variables 
            int errorFlag = 0,
            tempErrorCode = 0;


            while (errorFlag != -5)                                                                           // Begin loop
            {
                Console.WriteLine("Enter a phone number.  (Q to quit)");                                  // Get phone number input from user
                phoneNo = Console.ReadLine();
                Console.WriteLine();


                ReadDials(errorFlag, ref tempErrorCode, ref phoneNo, ref digit1, ref digit2, ref digit3,   // pass variables by reference
                                       ref digit4, ref digit5, ref digit6, ref digit7, ref digit8);        // to ReadDials Method
                errorFlag = tempErrorCode;

                switch (errorFlag)
                {
                    case 0:
                        AcknowledgeCall(digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8);
                        break;
                    case -1:
                        Console.WriteLine("ERROR - An invalid character was entered.");                     // Determine output based on
                        break;                                                                              // returned error codes
                    case -2:                                                                                // using switch command
                        Console.WriteLine("ERROR - Phone number cannot begin with 0.");
                        break;
                    case -3:
                        Console.WriteLine("ERROR - Phone number cannot begin with 555.");
                        break;
                    case -4:
                        Console.WriteLine("ERROR - Hyphen is not in the correct position.");
                        break;
                    case -5:
                        Console.WriteLine("Exiting program. ");
                        break;

                }

            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.