#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Beginner here that needs a hand with this code.


    I recently started learning C# and have crossed a problem.

    The code below is what I have got to try and sort the problem out:

    {

    int Hours;

    Console.WriteLine("Enter number of hours: ");
    Hours = Convert.ToInt32(Console.Read());

    Console.WriteLine("{0}", Hours);

    Console.Read();

    }

    Now when I run this and say I type in the number 1, the outcome will be 49; along with - 0 to 48, 2 to 50 and so forth.
  2. #2
  3. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1803
    You have reinterpreted the ASCII code of the input character as an integer. You need to convert the input string not the input character.

    System.Console.Read() reads a single character, so if for example you wanted to enter 12 hours, this input method would not work as you might expect in any case. In this case you are probably better off using System.Console.ReadLine(). That returns a System.String object; converting a string using ToInt32() will work as you expect.

    Code:
    Hours = Convert.ToInt32(Console.ReadLine());
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by clifford
    You have reinterpreted the ASCII code of the input character as an integer. You need to convert the input string not the input character.

    System.Console.Read() reads a single character, so if for example you wanted to enter 12 hours, this input method would not work as you might expect in any case. In this case you are probably better off using System.Console.ReadLine(). That returns a System.String object; converting a string using ToInt32() will work as you expect.

    Code:
    Hours = Convert.ToInt32(Console.ReadLine());
    I shall try this tonight when I get home from work.

    Thanks,
    MrLevinson
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by clifford
    You have reinterpreted the ASCII code of the input character as an integer. You need to convert the input string not the input character.

    System.Console.Read() reads a single character, so if for example you wanted to enter 12 hours, this input method would not work as you might expect in any case. In this case you are probably better off using System.Console.ReadLine(). That returns a System.String object; converting a string using ToInt32() will work as you expect.

    Code:
    Hours = Convert.ToInt32(Console.ReadLine());
    Thank you very much, this worked. I had only had that problem for a few hours and was bugging me, such a simple solution. I feel slightly idiotic now.

    Thank you again,
    MrLevinson
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1803
    Originally Posted by MrLevinson
    T I feel slightly idiotic now.
    Well not really, but realise this: I am not a C# developer (although I have developed a little C# code from time-to-time, and 23 years od development experience in genral); I determined a solution to your problem simply by reading the documentation - I never even bothered to test the answer, so I am glad it worked! Navigating Microsoft's documentation is itself a skill however!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    I have currently got a new prolem that I can't get around.

    I am trying to make, essentially a Error Handler. I want to create a loop where if a incorrect value is selected it will run the question again.

    So far I have:

    Code:
                //First Q
                Console.WriteLine("Can you please enter your choice of Moped (1 for Mopette, 2 for Mohawk.)");
                MopedType = Convert.ToInt32(Console.ReadLine());
    
                //Error Handler Start.
                if (MopedType > 2)
                {
                    MopedType = 0;
    
                    Console.WriteLine("Please enter a valid Moped Number. \nPress enter to continue.");
                    Console.Read();
    
                    Console.WriteLine("Can you please enter your choice of Moped (1 for Mopette, 2 for Mohawk.)");
                    MopedTypeNew = Convert.ToInt32(Console.ReadLine());
                }
    
                //Error Handler End.
    If I was to type h or 8 or something completely irrelevant I want it to loop back up to top. I wasn't sure if this could be done so I currently have got it to ask the question again. It comes up with error :

    Input string was not in a correct format.
    Any ideas?

    Thanks,
    MrLevinson
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    One way to resolve your issue is to create a separate function and use a while true loop. In this loop you'll use Int32.TryParse to parse Console.ReadLine. If it is determined that a valid numeric has been entered, you then have to test for either 1 or 2. You would then break out of the loop using a return statement when it is verified that either a 1 or 2 has been entered.

    I'd post POC code but I'm not sure whether or not this is a homework assignment.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0
    It's neither homework or assignment, I am doing this project for my friend.

    I don't yet understand Parse, I am still learning C# and have only been learning for about a week or two.

    I shall try work it out though

    Thanks,
    MrLevison
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    Well, I posted the POC code primarily because I need to add to my post count to get out of newbie status.

    Code:
    using System;
    
    class Program
    {
        static void Main()
        {
            switch( MopedQuery( "Can you please enter your choice of Moped (1 for Mopette, 2 for Mohawk.)"))
            {
            case 1:
                Console.WriteLine("Mopette selected");
                break;
            case 2:
                Console.WriteLine("Mohawk selected");
                break;
            default:
                break;
            }
        }
        static Int32 MopedQuery(string prompt)
        {
            while (true)
            {
                Console.Write(prompt);
                Int32 result;
                if (Int32.TryParse(Console.ReadLine(), out result))
                {
                    if(result == 1 || result == 2)
                        return result;
                }
                Console.WriteLine("Invalid Moped number entered. Try again.");
            }
        }
    }

IMN logo majestic logo threadwatch logo seochat tools logo