7 Replies - 3403 Views - Last Post: 01 September 2008 - 08:02 PM Rate Topic: -----

#1 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

Reading and assigning single characters to char

Posted 16 August 2008 - 12:57 PM

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?
Is This A Good Question/Topic? 0
  • +

Replies To: Reading and assigning single characters to char

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

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?
Was This Post Helpful? 0
  • +
  • -

#3 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

Re: Reading and assigning single characters to char

Posted 16 August 2008 - 03:13 PM

View Postjayman9, 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?


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

Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1511
  • View blog
  • Posts: 5,916
  • Joined: 21-March 08

Re: Reading and assigning single characters to char

Posted 16 August 2008 - 03:27 PM

View PostZeddicus, on 16 Aug, 2008 - 06:13 PM, said:

View Postjayman9, 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?


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

Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

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()
Was This Post Helpful? 0
  • +
  • -

#6 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

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.
Was This Post Helpful? 0
  • +
  • -

#7 Zeddicus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 14-July 08

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?
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,511
  • Joined: 23-August 08

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);
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1