Thread: How to read in an integer and display it again

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    205

    How to read in an integer and display it again

    Hi,
    I have the simple code below
    Code:
    Console::WriteLine("Hello World");
    int i = Console::Read();
    char c =(char)i;
    Console::WriteLine(c);
    After I type in '1', Writeline writes 49 back to the screen when I want it to write '1'. Anybody can help with this.
    Amish

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Unusual looking C++ code, but I suspect there is much more to the program than you have posted. Of interest is that 49 is the ASCII decimal value for the char '1'.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Managed C++! Yeurgh!

    Apparently there's no overload for WriteLine that accepts a char as first parameter. This is not surprising, as .Net only uses UNICODE. Use wchar_t instead of char.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    I would like to read in the number that I type as an integer instead of a character. Any clues?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I have no clue what you want.

    You type in '1'. It writes out the UNICODE code value of the character '1', which is 49. What do you want it to do? Read '1' as an integer? No can do. Read it as String (ReadLine) and then parse it (Int32.Parse).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Maybe?
    Code:
    Console::WriteLine("Hello World");
    char c = Console::Read();
    Console::WriteLine(c);

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That reads a single character and writes it out again, though.

    Best way of course is using proper C++.
    Code:
    #include <iostream>
    
    int main()
    {
      int i;
      std::cin >> i;
      std::cout << i;
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    205
    Quote Originally Posted by CornedBee
    I have no clue what you want.

    You type in '1'. It writes out the UNICODE code value of the character '1', which is 49. What do you want it to do? Read '1' as an integer? No can do. Read it as String (ReadLine) and then parse it (Int32.Parse).
    Thanks. That works
    Amish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 09-21-2008, 06:11 PM
  2. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM
  3. How to display integer to binary format?
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2005, 11:32 PM
  4. Help me: Display 100's 10's 1's from an integer!
    By Ramza in forum C Programming
    Replies: 5
    Last Post: 03-12-2002, 10:31 PM
  5. How to display a 64-bit integer?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2002, 11:13 PM