I am working on a homework assignment, beginning C#.
Have to accept input from user:
- phone number with numbers or characters
- then return only the numeric version of number.
My program takes input and returns value, but does not end when 10 characters have been entered.
You can enter as many characters as you want, then when enter is pressed it only displays first 10.
It also does not test the cases after each character entered. Seems to do at the end.
I would like to have each character tested after input and then once ten characters have been entered
the program display results.
I hope I am giving enough info. I am pretty stressed about this.
It is due tomorrow and I have a few other programs to do yet.
Any help would be greatly appreciated.
My code:
Console.Write("Please enter your phone number: ");
do
{
int temp = Console.Read();
input = Convert.ToChar(temp);
//int tempInput = Convert.ToString(tempInput);
switch (input)
{
case '0':
alphaNumericPhoneNumber += input.ToString();
numericPhoneNumber += input.ToString();
counter--;
break;
// { other cases }
default:
// if input does not match cases then loop returns to
// request new input
Display.Error(input.ToString());
Console.Write(alphaNumericPhoneNumber);
//Display.Continue();
//Console.Clear();
input = ' ';
break;
}
}
while (numericPhoneNumber.Length < 0);
return numericPhoneNumber;
}
The documentation for the
Console.Read()method explains the described behavior.If you want to process the characters as they arrive, you may use the
KeyAvailableandReadKeymethods as demonstrated in the documentation. The example is available in 5 .NET languages, including C#.