I'm trying to code a C# homework assignment that takes a phone number in this format XXX-XXXX (8 digits that can be alphanumeric) and then "Define a method named ReadDials( ) that reads each digit/letter dialed into eight separate char variables (DO NOT USE ARRAYS). All digits are sent back through parameters by reference. "

There's a lot more to the assignment such as input validation, converting alpha-numeric data to individual numeric characters, testing for phone number validity,

I think I'm already in the weeds a bit since the pseudocode supplied has the loop and validation in the ReadDials method. The pseudocode requirements follow as well.

// Pseudocode

Using the pseudocode below, write the code that will meet the requirements:

Main Method
Declare the char variables for the 8 digits of the phone number
While true
Call the ReadDials( ) method passing the 8 digits
by reference. ReadDials ( ) returns an error code by
value.
If the return value is -5, exit the do while loop
If the error code is -1, display the
error message "ERROR - An invalid character was entered".
If the error code is -2, display the
error message "ERROR - Phone number cannot begin with 0".
If the error code is -3, display the
error message "ERROR - Phone number cannot begin with 555".
If the error code is -4, display the
error message "ERROR - Hyphen is not in the correct position".
Otherwise, call the Acknowledge Call function
End-While

ReadDials( ) method
Input the first digit
If a Q was entered, return -5.
Input the rest of the phone number
Call the ToDigit( ) function for each of the 7 digits
not for digit 4
If ToDigit returns -1, return -1
If digit 4 is not a hyphen, return -4.
If digit 1 is 0, return -2.
If digits 1 - 3 are 5, return -3
Otherwise, return 0

ToDigit ( ) method
Convert the digit to upper case
Use a switch statement to determine if the digit is valid
and convert the letters to digits
If the digit is invalid, return -1.
If the digit is valid, return 0.

AcknowledgeCall ( ) method
Display the Phone Number.

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

namespace Lab4_2
{
    class Program
    {
        // ReadDials method initial code

       static char ReadDials(string PhoneNo, int myCounter)
        {             
        char holder;   // variable to store individual character from string       
        holder = (char)PhoneNo[myCounter];  // parse value in counter position          
        return holder;  return single value to Main ()  
      }        
          
        static void Main(string[] args)
        {
               char digit1,  // Declare 8 variables for individual characters
                    digit2,
                    digit3,
                    digit4,
                    digit5,
                    digit6,
                    digit7,
                    digit8;
            string  phoneNo;
            
            // Get console input
            Console.WriteLine("Enter a phone number (Q to quit)");
            phoneNo = Console.ReadLine();
           

            int myCounter = 1;
            do
            {
                digit1 = ReadDials(phoneNo, myCounter);

                myCounter++;
            }

            while (myCounter < 8);

Obviously, the do loop in the main won't work, because I can't think of a way to loop the call to ReadDials() without using an array. I'm not looking for anyone to code for me. I really want to learn C#. I'm just looking for help or ideas on how to fulfill the project requirements... logic help, if you will.

I suppose that the first step would be change the Main() to a While loop to satisfy the pseudocode requirements, though I can't envision the logic involved. Anyone want to give me a hand with general program structure. Specific coding not necessary. I just need logic help / structure help... or a nudge in the right direction...

Any help is appreciated.

Jim

Hey man, this is really a sh** assignment without arrays.
See what you can do with select case statement.

I'm not sure why we would be prohibited from using arrays. It seems counter intuitive since the program requirements seem perfectly suited for an array. So, yeah, it's an odd assignment.
Thanks for your answer. I'll repost what I come up with when I get some time to code later.

Jim

It looks like you are always assigning to the same variable (digit1 = ReadDials(phoneNo, myCounter);). So it does not seem as though you will ever do anything but fill digit1 and nothing more.

Yeah, I noted that when I said "Obviously, the do loop in the main won't work, because I can't think of a way to loop the call to ReadDials() without using an array. I'm not looking for anyone to code for me. I really want to learn C#. I'm just looking for help or ideas on how to fulfill the project requirements... logic help, if you will."

The code was my first attempt to flesh it out. I'm doing a complete re-write at the moment. Thanks for the answer, though. Every comment improves my understanding even more. I think my main problem is that I'm still new at the logic part.

Thanks again!

Jim

Hi mate...

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

namespace Lab4_2
{
    class Program
    {
        // ReadDials method initial code

        static char ReadDials(string PhoneNo, int myCounter)
        {             
        char holder;         
        holder = (char)PhoneNo[myCounter];         
        return holder;
        }

        static void Main(string[] args)
        {
            char d1,
                 d2,
                 d3,
                 d4,
                 d5,
                 d6,
                 d7,
                 d8;
            
           

            Console.WriteLine("Enter a phone number (Q to quit)");
           string phoneNo = Console.ReadLine();

           string tempHolder = ""; // use this to temporary hold every char that comes back from ReadDials method...

            int myCounter = 0; // start this from 0 because phoneNo index will start from 0.
            do
            {
                tempHolder += ReadDials(phoneNo, myCounter);
                myCounter++;

            } while (myCounter < 8);

            d1 = tempHolder[0];
            d2 = tempHolder[1];    // now assign each char variable to each number 
            d3 = tempHolder[2];    // that is held in tempHolder
            d4 = tempHolder[3];
            d5 = tempHolder[4];
            d6 = tempHolder[5];
            d7 = tempHolder[6];
            d8 = tempHolder[7];

        }
    }
}

how about that? are you allowed to do something like that or not?

None of you seem to have read the assignment properly ;)

Let me highlight a section for you... All digits are sent back through parameters by reference.

This is an assignment to teach the students how to use reference variables =)

There is a keyword called "ref" you can attach to a variable in a method definition. void MyMethod(ref int myInteger){ /* stuff */ } The "ref" keyword says that this variable has been passed by reference, not by value. The difference being, that variable is not a "copy" of something else, but a variable which points back to the original.

void Main()
{
   int myInt = 3;
   Console.WriteLine(myInt.ToString());
   MyMethod(ref myInt);
   Console.WriteLine(myInt.ToString());
}

private void MyMethod(ref int myInteger)
{
   myInteger = 9;
}

Output:
3
9

=)

EDIT: I agree it is silly that you would need to pass 8 variables by reference. An array would do the job lovely. (Which can still be passed by reference. Perhaps the teacher doesn't know how to use them properly either ;))

Ok,
I spent last night doing a total re-write. Changed the logic, no arrays, and passed all of the values by reference as required. I still have to write a method for re-assembling the number for output, but that's easy.

Problem: I'm getting an error in the declaration line for the ReadDials method.

// Method ReadDials        
        static int ReadDials(int errorFlag, ref char digit, ref int count)

Error 1 'Lab4_2.Program.ReadDials(int, ref char, ref int)': not all code paths return a value 46 20 Lab4_3

Obviously Visual Studio is telling me that one or more of my variables isn't returning a value, but I'll be damned if I can find it.
I'm sure this isn't the most elegant coding you've seen, so I'm open to suggestions to improve my coding as well.

Thanks in advance.

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

namespace Lab4_2
{
    class Program
    {

        // Method ToDigit
        static char ToDigit(char testDigit, ref int errorFlag)
         {  
          Char.ToUpper(testDigit);  // Convert Character to UpperCase
    
           char result;  // char variable to hold result
           switch (testDigit)      
              {
                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;
                }
                   if (result == 'e')
                   {
                    errorFlag = -1;  // Return error if non-alphanumeric character is present
                   }
                     else
                     {
                      errorFlag = 0; // else return 0 
                     }
                result = testDigit;
                return testDigit;
          }

    
        // Method ReadDials        
        static int ReadDials(int errorFlag, ref char digit, ref int count)
        {             
                       
            if ((count == 0) & (digit == 'Q')) // or if ((count == 0) & (digit =='q'))
            {
                errorFlag = -5;
            }
            else
                if ((count == 3) & (digit != '-'))  // if forth digit is not a '-' return -4 error code
                {
                    errorFlag = -4;
                }
                else
                    if ((count == 0) & (digit == '0'))  // If first character is a zero return -2 error code
                    {
                        errorFlag = -2;
                    }
                    else
                        if (count != 3)
                        {
                            ToDigit(digit, ref errorFlag);  // call ToDigit Method
                        }  
                                //  determine how to check the first three digits by creating 3 digit string and comparing to "555"  ******************
                                //  if 555 then eCode = -3                                                                      
                            
                            else
                                return errorFlag;                    

        }

        //  Main Method

        static void Main(string[] args)
        {
            char digit1,
                 digit2,
                 digit3,
                 digit4,
                 digit5,
                 digit6,
                 digit7,
                 digit8;
            string phoneNo;
            int errorCode = 0,
                counter = 0;               

            do
            {
                counter = 0; // reset counter
                Console.WriteLine("Enter a phone number (Q to quit)");
                phoneNo = Console.ReadLine();

                phoneNo = phoneNo.Substring(0, 8);  // Take only the first eight characters 

                digit1 = phoneNo[0];
                digit2 = phoneNo[1];
                digit3 = phoneNo[2];
                digit4 = phoneNo[3];
                digit5 = phoneNo[4];
                digit6 = phoneNo[5];
                digit7 = phoneNo[6];
                digit8 = phoneNo[7];

                Console.WriteLine(digit1);       

                ReadDials(errorCode, ref digit1, ref counter);  // pass each digit and a counter by reference
                counter++;
                ReadDials(errorCode, ref digit2, ref counter);
                counter++;
                ReadDials(errorCode, ref digit3, ref counter);
                counter++; 
                ReadDials(errorCode, ref digit4, ref counter);
                counter++;
                ReadDials(errorCode, ref digit5, ref counter);
                counter++; 
                ReadDials(errorCode, ref digit6, ref counter);
                counter++; 
                ReadDials(errorCode, ref digit7, ref counter);
                counter++; 
                ReadDials(errorCode, ref digit8, ref counter);

                switch (errorCode)
                {
                    case -1:
                        Console.WriteLine("ERROR - An invalid character was entered.");
                        break;
                    case -2:
                        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;
                    default:
                        // call AcknowledgeCall() function!  Not written yet!
                        break;
                }
          
            }
            while (errorCode != -5);  // loop until errorCode is returned.


        }
    

        }

    
    }

It's exactly as it says, you're missing one (or more) return statement(s) in ReadDial. Currently your only return statement is in the very bottom "else" statement. But what if it never gets that far? You still need to return a value.

Also, by the specification you've been given, the ReadDials method is still incorrect.

You should pass by reference all 8 character variables and fill them in *inside* the ReadDials method. So in your ReadDials, you need a loop which takes input, checks the character according to the rules and perform the action according to the rule it meets.

Ugh. Stupid error. Thanks.
At least I still have a couple of days to re-write the Main and ReadDials methods.
I'm pretty confident that I can get the code to pass all of the requirements.
Thanks for the heads up.
I think I stayed up too late last night!

Thanks again.

Jim

Did another re-write. I'm getting more familiar with Visual Studio, and that's a good thing. I did wonder what was the best structure to use for a situation like this where there is a ton of 'if' statements. The logic is alright, and the program returns the expected values, but I was just trying to push my learning curve by asking questions.

Thanks.

Jim

if ((phoneNumber.Length != 1) & (phoneNumber.Length <= 7))
            {
                errorFlag = -1;
            }
            else
            {
                digit1 = phoneNumber[0];

                if (((digit1 == 'Q') | (digit1 == 'q')) & ((phoneNumber.Length == 1)))
                {
                    errorFlag = -5;
                }
                else
                {
                    ToDigit(digit1, ref errorFlag, ref holder);
                    digit1 = holder;
                }

              if (errorFlag == 0)
                  {
                      digit2 = phoneNumber[1]; ToDigit(digit2, ref errorFlag, ref holder);
                      digit2 = holder;
                  }
                    
             if (errorFlag == 0)
                  {
                      digit3 = phoneNumber[2]; ToDigit(digit3, ref errorFlag, ref holder);
                      digit3 = holder;
                  }
                        
             if (errorFlag == 0)
                  {
                       digit4 = phoneNumber[3];
                  }
                           
             if (errorFlag == 0)
                  {
                       digit5 = phoneNumber[4]; ToDigit(digit5, ref errorFlag, ref holder);
                       digit5 = holder;
                  }
                                
             if (errorFlag == 0)
                  {
                       digit6 = phoneNumber[5]; ToDigit(digit6, ref errorFlag, ref holder);
                       digit6 = holder;
                  }
                                    
             if (errorFlag == 0)
                  {
                       digit7 = phoneNumber[6]; ToDigit(digit7, ref errorFlag, ref holder);
                       digit7 = holder;
                  }
                                        
             if (errorFlag == 0)
                  {
                       digit8 = phoneNumber[7]; ToDigit(digit8, ref errorFlag, ref holder);
                       digit8 = holder;
                  }
                
            }
            return errorFlag; 
        }

I think that without being able to use arrays, this is going to be as good as it gets. However, there is no need for you "holder" variable. You can pass the digit variable straight in there.

eg. ToDigit(phoneNumber[7], ref errorFlag, ref digit8); will work just as good and saves two lines of code.

Speaking of code lines, please put your statements on separate lines. It makes it much easier to read at a quick glance ^^

You can't do this for your project, your specification doesn't allow it, but if you wanted to learn something fancy.. ;)

First, you would need to declare your 8 character variables as class fields.

class Program
{
    static char digit1, digit2, digit3, digit4, digit5, digit6, digit7, digit8;
    static void Main(string[] args)
    {
        ReadDials(ref digit1, ref digit2, ref digit3, ref digit4, ref digit5, ref digit6, ref digit7, ref digit8);
        for(int digitNum = 1; digitNum < 9; digitNum++)
        {
            FieldInfo digitField = typeof(Program).GetField(String.Format("digit{0}", digitNum), BindingFlags.Static | BindingFlags.NonPublic);
            
            char currentDigit = (char)digitField.GetValue(null);
            int errorValue = 0;
            ToDigit(ref errorValue, ref currentDigit);
            digitField.SetValue(currentDigit);

            switch(errorValue)
            {
                case -5:
                    /* Do Something As Example */
                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.