-
October 7th, 2013, 04:33 PM
#1
Using a char as the control variable in a loop.
Hello, I am new to programming and c#, so please bear with me. I have a problem using a char as the control variable in a loop. I am able to get the code to work fine when using a string as the control variable, but can someone tell me what is wrong in the original code to cause the error I'm experiencing. It seems when using a char, the loop goes through two extra iterations for each input.
Original Code:
Code:
//This program asks for a character input
//then determines if said character is a
//lower case letter. Typing an ! will close
//the program.
using System;
namespace EnterLowerCaseLetters
{
class Program
{
static void Main(string[] args)
{
const char CLOSE = '!';
char c;
do {
Console.Write("Enter a lower case letter: ");
c = Convert.ToChar(Console.Read());
if (Char.IsLower(c) && Char.IsLetter(c))
Console.WriteLine("Ok");
else if (c != CLOSE)
Console.WriteLine("Error");
} while (c != CLOSE);
Console.ReadLine();
Console.ReadLine();
}
}
}
Output:
Code:
Enter a lower case letter: g
Ok
Enter a lower case letter: Error
Enter a lower case letter: Error
Enter a lower case letter: G
Error
Enter a lower case letter: Error
Enter a lower case letter: Error
Enter a lower case letter: 1
Error
Enter a lower case letter: Error
Enter a lower case letter: Error
Enter a lower case letter: #
Error
Enter a lower case letter: Error
Enter a lower case letter: Error
Enter a lower case letter: !
Working Code:
Code:
//This program asks for a character input
//then determines if said character is a
//lower case letter. Typing an ! will close
//the program.
using System;
namespace EnterLowerCaseLetters
{
class Program
{
static void Main(string[] args)
{
const char CLOSE = '!';
string input;
char c;
do {
Console.Write("Enter a lower case letter: ");
input = Console.ReadLine();
c = Convert.ToChar(input);
if (Char.IsLower(c) && Char.IsLetter(c))
Console.WriteLine("Ok");
else if (c != CLOSE)
Console.WriteLine("Error");
} while (input != "!");
Console.ReadLine();
Console.ReadLine();
}
}
}
Output:
Code:
Enter a lower case letter: g
Ok
Enter a lower case letter: G
Error
Enter a lower case letter: 1
Error
Enter a lower case letter: %
Error
Enter a lower case letter: !
-
October 7th, 2013, 06:45 PM
#2
Re: Using a char as the control variable in a loop.
Step through the code in a debugger and see what the values of c are. Or display what c is to the console window.
-
October 8th, 2013, 02:42 PM
#3
Re: Using a char as the control variable in a loop.
Thanks for the reply! I added some code to show the value of c, but I'm still not sure what's wrong.
Code:
//This program asks for a character input
//then determines if said character is a
//lower case letter. Typing an ! will close
//the program.
using System;
namespace EnterLowerCaseLetters
{
class Program
{
static void Main()
{
const char CLOSE = '!';
char c;
do {
Console.Write("Enter a lower case letter: ");
c = Convert.ToChar(Console.Read());
if (Char.IsLower(c) && Char.IsLetter(c))
Console.WriteLine("Ok");
else if (c != CLOSE)
Console.WriteLine("Error");
Console.WriteLine("c = " + c);
} while (c != CLOSE);
Console.ReadLine();
}
}
}
Code:
Enter a lower case letter: g
Ok
c = g
Enter a lower case letter: Error
c =
Enter a lower case letter: Error
c =
Enter a lower case letter: G
Error
c = G
Enter a lower case letter: Error
c =
Enter a lower case letter: Error
c =
Enter a lower case letter:
-
October 8th, 2013, 03:02 PM
#4
Re: Using a char as the control variable in a loop.
Try this
Code:
do
{
Console.Write("Enter a lower case letter: ");
c = Console.ReadLine().ToCharArray()[0];
if (Char.IsLower(c) && Char.IsLetter(c))
Console.WriteLine("Ok");
else if (c != CLOSE)
Console.WriteLine("Error");
} while (c != CLOSE);
-
October 8th, 2013, 03:10 PM
#5
Re: Using a char as the control variable in a loop.
Reading up on this in msdn. Console.Read() doesn't return until the enter key is pressed (then the contents of the while loop are executed for each character in the read buffer).
What you want is Console.ReadKey();
Code:
ConsoleKeyInfo cki;
do
{
cki = Console.ReadKey(true);
Console.WriteLine("{0}", cki.KeyChar);
}
while (cki.KeyChar != '!');
If I type in "this is a test!", the following output is produced.
Code:
t
h
i
s
i
s
a
t
e
s
t
!
-
October 8th, 2013, 04:03 PM
#6
Re: Using a char as the control variable in a loop.
Thanks again for the relies. I was able to get the program working with both the ways suggested.
-
October 8th, 2013, 06:27 PM
#7
Re: Using a char as the control variable in a loop.
Originally Posted by Back Rain
Thanks again for the relies. I was able to get the program working with both the ways suggested.
Notice the difference it both approaches. One approach requires that you press the enter key after each key is pressed, while the other approach doesn't. Experiment and understand both approaches (so you know which one to use later).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|