Reputation: 1932
For beginner practice I'm trying to create a simple loop that accepts a single character from the user, prints that character to the console and keeps doing that until the user enters 'R'.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SimpleLoop
{
class Program
{
static void Main(string[] args)
{
char cplayerSelection = 'R';
while(cplayerSelection == 'R')
{
Console.WriteLine("Enter R, P, or S:");
cplayerSelection = (char)Console.Read();
Console.WriteLine(cplayerSelection);
}
}
}
}
How ever no matter what the user enters it only loops once end then exits. What do I need to change to continue the loop?
Upvotes: 3
Views: 349
Reputation: 12956
I think you are confused.
...loop that accepts a single character from the user, prints that character to the console and keeps doing that until the user enters a character other than 'R'.
The loop exit condition is any character that is not 'R'
. In other words, the only input that continues the loop is 'R'
.
no matter what the user enters it only loops once end then exits. What do I need to change to continue the loop?
This is what your first statement means.
What do I need to change to continue the loop?
Change the loop exit condition.
Upvotes: 2
Reputation: 101681
Try this one, inside of your while
loop:
cplayerSelection = Console.ReadKey().KeyChar
That will only work if you type uppercase R
If you want to allow three letters then you can try:
var letters = new[] {'R', 'P', 'S'};
while (letters.Contains(cplayerSelection))
{
Console.WriteLine("Enter R, P, or S:");
cplayerSelection = Console.ReadKey().KeyChar;
Console.WriteLine(cplayerSelection);
}
If you want to make a case-insensitive check simply add lower-case
versions of these characters to the array.Or you can use this (suggested by @Habib):
while (letters.Any(r => r == char.ToUpper(cplayerSelection)))
Upvotes: 4
Reputation: 3248
As others have mentioned, you need to be wary of case sensitivity. You can do so by checking the input with both the upper and lower case of the char. Or you could convert the char to its upper/lower-case form and only compare to that.
Also, for programs that always execute at least once like yours, using do while is more appropriate. For example:
class Program
{
static void Main()
{
char input;
do
{
input = char.ToUpperInvariant(Console.ReadKey().KeyChar);
} while (input != 'R');
}
}
Upvotes: 2
Reputation: 2244
Console.Read
only reads a single character. The next time you get in the loop, it reads the return character, as well. You have a couple of options, based on whether you want the user to have to hit enter
or not.
If you want them to hit enter:
char cplayerSelection = 'R';
while (cplayerSelection == 'R')
{
Console.WriteLine("Enter R, P, or S:");
cplayerSelection = (Console.ReadLine())[0];
Console.WriteLine(cplayerSelection);
}
If you don't want them to hit enter:
char cplayerSelection = 'R';
while (cplayerSelection == 'R')
{
Console.WriteLine("Enter R, P, or S:");
cplayerSelection = Console.ReadKey().KeyChar;
Console.WriteLine(cplayerSelection);
}
Upvotes: 2
Reputation: 1179
I believe that it should be
while(cplayerSelection != 'R' || cplayerSelection != 'r')
You have to check for both the uppercase and lowercase letter since they do not have the same value.
Edit: Also change the cplayerSelection declaration to some other letter so the loop can actually be executed for the first time.
Also replace this line
cplayerSelection = (char)Console.Read();
with
cplayerSelection = Console.ReadKey().KeyChar;
Read the comment by Habib on this answer as to understand why.
Upvotes: 5
Reputation: 1835
According to this http://social.msdn.microsoft.com/Forums/vstudio/en-US/5c5b43e2-4536-4411-a34f-290eb8525b4d/why-does-consolereadline-not-wait-for-user-input?forum=csharpgeneral, Console.Read() will return a single character. The second time through the loop it will pull your 'return' character and exit your loop.
Upvotes: 1