Is it possible to read a single character from the keyboard and then assign the inserted character to a char variable in a simple way? Which method should you call to do this?
Reading and assigning single characters to char
Page 1 of 17 Replies - 3403 Views - Last Post: 01 September 2008 - 08:02 PM
Replies To: Reading and assigning single characters to char
#2
Re: Reading and assigning single characters to char
Posted 16 August 2008 - 01:49 PM
Define simple, there are many ways to accomplish the task you are asking about.
What have you tried, so far?
What have you tried, so far?
#3
Re: Reading and assigning single characters to char
Posted 16 August 2008 - 03:13 PM
jayman9, on 16 Aug, 2008 - 01:49 PM, said:
Define simple, there are many ways to accomplish the task you are asking about.
What have you tried, so far?
What have you tried, so far?
To define simple I must refer to C++ (I really hope it's OK because I am trying to learn these languages at the same time). In C++ you only use cin to store a character in a char variable, then implicitly typecast the char to an int.
The version I am using right now in C# is too complex in my opinion:
Console.WriteLine("Please enter a character:");
string input = Console.ReadLine();
uint i;
foreach (char c in input)
{
i = Convert.ToUInt32(c);
System.Console.WriteLine("{0}'s ASCII integer value is {1}.", c, i);
}
First of all, it utilizes a string that is "broken down" into its char components which are then converted to integers. I am looking for a procedure similar to the one described above in C++.
This post has been edited by Zeddicus: 16 August 2008 - 03:18 PM
#4
Re: Reading and assigning single characters to char
Posted 16 August 2008 - 03:27 PM
Zeddicus, on 16 Aug, 2008 - 06:13 PM, said:
jayman9, on 16 Aug, 2008 - 01:49 PM, said:
Define simple, there are many ways to accomplish the task you are asking about.
What have you tried, so far?
What have you tried, so far?
To define simple I must refer to C++ (I really hope it's OK because I am trying to learn these languages at the same time). In C++ you only use cin to store a character in a char variable, then implicitly typecast the char to an int.
The version I am using right now in C# is too complex in my opinion:
Console.WriteLine("Please enter a character:");
string input = Console.ReadLine();
uint i;
foreach (char c in input)
{
i = Convert.ToUInt32(c);
System.Console.WriteLine("{0}'s ASCII integer value is {1}.", c, i);
}
First of all, it utilizes a string which is then "broken down" into its char components and converted to integers. I am looking for a procedure similar to the one described above in C++.
how about this?
Console.WriteLine("Please enter a character:");
uint charInt = 0;
while (charInt != 13)
{
charInt = (uint)Console.Read();
System.Console.WriteLine("{0}'s ASCII integer value is {1}.", (char)charInt, charInt);
}
This post has been edited by eclipsed4utoo: 16 August 2008 - 03:34 PM
#5
Re: Reading and assigning single characters to char
Posted 16 August 2008 - 04:07 PM
In addition to the Read() function, mentioned by eclipsed4utoo, there is also a ReadKey() function which serves just this purpose.
Console.ReadKey()
Console.ReadKey()
#6
Re: Reading and assigning single characters to char
Posted 01 September 2008 - 08:36 AM
jayman9: I'm getting weird results when using the Console.ReadKey method for this task. Should you use it in a special way in order to assign the value to a char? And if I do a explicit typecast like (char) Console.ReadKey, the application throws an exception.
#7
Re: Reading and assigning single characters to char
Posted 01 September 2008 - 09:35 AM
eclipsed4utoo:
When running your code I get the following output (given the character 'A'):
A's ASCII integer value is 65.
's ASCII integer value is 13.
Not what I expected. Does it have something to do with the Read metod?
When running your code I get the following output (given the character 'A'):
A's ASCII integer value is 65.
's ASCII integer value is 13.
Not what I expected. Does it have something to do with the Read metod?
#8
Re: Reading and assigning single characters to char
Posted 01 September 2008 - 08:02 PM
Try:
Console.WriteLine("Please enter a character:");
uint charInt = 0;
while ((charInt = (uint)Console.Read()) != 13)
{
System.Console.WriteLine("{0}'s ASCII integer value is {1}.", (char)charInt, charInt);
}
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|