Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: question on user input

  1. #1
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Question question on user input

    Hello,

    Right now i am trying to sharp (ha made a funny) my skills by bugging my program and fixing it, well my question is, lets say a user is prompt for only a number but accidently puts a letter in it, how do I go about that, or vise versa, I have the code below which works fine but if you can let me know what i am missing to fix that, it would be great.
    Thanks in advance

    Code:
            enum compuss:int
            {
                north = 1,
                south = 2,
                west = 3,
                east = 4,
            }
    
            static int input;
    
                static void Main(string[] args)
                {
                    Console.WriteLine("Please enter \n1=north\n2=south\n3=west\n4=east\n");
                    input = int.Parse(Console.ReadLine());
                    while (input !=1 && input !=2 && input !=3 && input!=4)
                    {
                        Console.WriteLine("\nPlease enter 1, 2, 3, or 4");
                        input = int.Parse(Console.ReadLine());
                    }
                    
                    Console.WriteLine("\nyou have entered to go " + input + " direction " + (compuss)input);
                    Console.ReadKey();

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Re: question on user input

    Oops, correct: how do i go about if a user inputs a string, or letter and not a number, the while loops kicks in, which works for any number however I am trying to figure out if someone puts in a string or different character, then loops takes affect? So far as shown i got the while loop down and using in many practices so i can keep it fresh in my mind, but of course string and int together is difficult.

  4. #3
    gaylo565's Avatar
    gaylo565 is offline Programming Professional
    Join Date
    May 2007
    Location
    flagstaff, az
    Posts
    268
    Rep Power
    21

    Re: question on user input

    You need to run an exception loop. Here is a tutorial on how to go about this: Exception Handling in .NET

  5. #4
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Question Re: question on user input

    Hello gaylo565,

    Thank you for that link, i have read it and is very interesting, however, does it solve also command application issues too, or does it work only in OOP?

  6. #5
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: question on user input

    My lovely tutorial explains that exception handling will catch the errors.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  7. #6
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Talking Re: question on user input

    Hello Xav,

    Is this a good example with the code I included above?

    Code:
     try
        {
    
            enum compuss : int
            {
                north = 1,
                south = 2,
                west = 3,
                east = 4,
            }
    
            static int input;
    
            static void Main(string[] args)
            {
                Console.WriteLine("Please enter \n1=north\n2=south\n3=west\n4=east\n");
                input = int.Parse(Console.ReadLine());
                while (input != 1 && input != 2 && input != 3 && input != 4)
                {
                    Console.WriteLine("\nPlease enter 1, 2, 3, or 4");
                    input = int.Parse(Console.ReadLine());
                }
    
                Console.WriteLine("\nyou have entered to go " + input + " direction " + (compuss)input);
                Console.ReadKey();
    
            }
    
        }
    Because if i do try, catch, or final, then it would through this error message "invalid token 'try' in class, struct, or interface member declaration" and also "Type or namespace definition, or end-of-file expected". can you explain what I am doing wrong and/or correct the code? thanks for all your help, I am getting better... right?

  8. #7
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: question on user input

    Put the try block inside the Main method.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  9. #8
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Question Re: question on user input

    ok cool, thanks, but when i put the try { after the main like you said, the error message appears "Expected catch or finally" and i made sure to close the brackets.

  10. #9
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: question on user input

    Ya gotta put in a catch section as well:

    Code:
    try
    {
    
    }
    catch (Exception exc)
    {
    Console.WriteLine("Error: " + exc.Message);
    }

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  11. #10
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Cool Re: question on user input

    yup that worked thanks : )

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Getting input from user
    By restin84 in forum Linux Programming and Scripting
    Replies: 3
    Last Post: 10-04-2011, 05:35 AM
  2. NASM User input help 2
    By untitled_1 in forum Assembly
    Replies: 3
    Last Post: 11-07-2010, 12:46 AM
  3. NASM User input help
    By untitled_1 in forum Assembly
    Replies: 2
    Last Post: 11-02-2010, 02:30 PM
  4. user input
    By Ananta2010 in forum Java Help
    Replies: 4
    Last Post: 06-16-2010, 08:03 AM
  5. Question about C# user int input
    By TheMagicalRock in forum C# Programming
    Replies: 3
    Last Post: 04-01-2008, 06:36 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts