Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

question on user input

user input

  • Please log in to reply
17 replies to this topic

#1 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 18 September 2008 - 03:08 PM

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


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


  • 0

#2 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 18 September 2008 - 03:09 PM

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.
  • 0

#3 gaylo565

gaylo565

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 258 posts

Posted 18 September 2008 - 11:18 PM

You need to run an exception loop. Here is a tutorial on how to go about this: http://forum.codecal...ndling-net.html
  • 0

#4 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 19 September 2008 - 08:01 AM

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?
  • 0

#5 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 19 September 2008 - 08:10 AM

My lovely tutorial explains that exception handling will catch the errors.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#6 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 19 September 2008 - 08:21 AM

Hello Xav,

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


 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?
  • 0

#7 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 19 September 2008 - 08:28 AM

Put the try block inside the Main method.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#8 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 19 September 2008 - 11:15 AM

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.
  • 0

#9 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 19 September 2008 - 11:26 AM

Ya gotta put in a catch section as well:

try
{

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

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#10 Siten0308

Siten0308

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 298 posts

Posted 19 September 2008 - 03:53 PM

yup that worked thanks : )
  • 0

#11 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 19 September 2008 - 10:36 PM

You could also use regular expressions.
  • 0

#12 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 21 September 2008 - 11:29 AM

Shh, my dear John. :)

I don't think anyone else has actually memorised the different codes for regexes.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Also tagged with one or more of these keywords: user input

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download