8 Replies - 806 Views - Last Post: 11 March 2015 - 07:09 PM Rate Topic: -----

#1 protexxi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 08-November 14

HELP! Console.ReadLine() Crash

Posted 11 March 2015 - 11:35 AM

I have a menu on a console screen.
 Menu:
            Console.Clear();
            Console.WriteLine("[1] Register New Person (Staff, Prisoner or visitor)");
            Console.WriteLine("[2] Remove Person (Staff, Prisoner or Visitor)");
            Console.WriteLine("[3] Edit Person (Staff, Prisoner or Visitor)");
            Console.WriteLine("[4] View Person (Staff, Prisoner or Visitor)");
            Console.WriteLine("[5] Order Supplies");
            Console.WriteLine("[6] Help ");
            Console.WriteLine("[7] Exit");
            Console.WriteLine("Please Enter The Number Of your Option");
            int Option = 8;
			Option = int.Parse(Console.ReadLine());
            if (Option == 1)
            {
                goto AddMenu;
            }
            if (Option == 2)
            {
                goto RemoveMenu;
            }
            if (Option == 3)
            {
                goto EditMenu;
            }
            if (Option == 4)
            {
                goto ViewMenu;
            }
            if (Option == 5)
            {
                goto OrderMenu;
            }
            if (Option == 6)
            {
                goto Help;
            }
            if (Option == 7)
            {

                System.Environment.Exit(0);
            }
            if (Option == null)
            {
                Console.WriteLine("Please Enter a Number");
                System.Threading.Thread.Sleep(3000);
                goto Menu;
            }
            else
                Console.WriteLine("Invalied Selection");
            System.Threading.Thread.Sleep(3000);
            goto Menu;


But the if null function dosnt work so entering no number and hitting enter or entering a char/s courses a crash.

please dont tell my you wont write my code for me as its collage, i simply with to be pointed in the right direction would be a grete halp iv solved to entaring no number problem with this

 Menu:
            Console.Clear();
            Console.WriteLine("[1] Register New Person (Staff, Prisoner or visitor)");
            Console.WriteLine("[2] Remove Person (Staff, Prisoner or Visitor)");
            Console.WriteLine("[3] Edit Person (Staff, Prisoner or Visitor)");
            Console.WriteLine("[4] View Person (Staff, Prisoner or Visitor)");
            Console.WriteLine("[5] Order Supplies");
            Console.WriteLine("[6] Help ");
            Console.WriteLine("[7] Exit");
            Console.WriteLine("Please Enter The Number Of your Option");
            int Option = 8;
			Option = int.Parse(Console.ReadLine()+1);
            if (Option == 2)
            {
                goto AddMenu;
            }
            if (Option == 3)
            {
                goto RemoveMenu;
            }
            if (Option == 4)
            {
                goto EditMenu;
            }
            if (Option == 5)
            {
                goto ViewMenu;
            }
            if (Option == 6)
            {
                goto OrderMenu;
            }
            if (Option == 7)
            {
                goto Help;
            }
            if (Option == 8)
            {

                System.Environment.Exit(0);
            }
            if (Option == 9)
            {
                Console.WriteLine("Please Enter a Number");
                System.Threading.Thread.Sleep(3000);
                goto Menu;
            }
            else
                Console.WriteLine("Invalied Selection");
            System.Threading.Thread.Sleep(3000);
            goto Menu;

adding the +1 to the console.read.. but it always returens saying invalid selection so i just created another problem.

been hacking away at this with trial and error for 2 weeks now no where near a decent solution and simple cannot waste anymore time!

i understand i need to check the data entered is valid i believe this is called validating data?

thanks jake

Is This A Good Question/Topic? 0
  • +

Replies To: HELP! Console.ReadLine() Crash

#2 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 466
  • View blog
  • Posts: 2,212
  • Joined: 07-April 08

Re: HELP! Console.ReadLine() Crash

Posted 11 March 2015 - 11:43 AM

Have you put a breakpoint at the int.parse and checked what it's doing? just saying "It crashes" or "It doesn't work" is not good enough. You need to tell us what the exact error is. I suspect that you haven't tried or don't know what debugging is, so look at this tutorial and come back with an actual error.


Also one thing. NEVER, and I mean NEVER use GOTO. It is horrible and extremely bad design. I really wish they would just let it die in the fires of hell where it belongs. You should be using a loop instead.

This post has been edited by rgfirefly24: 11 March 2015 - 11:45 AM

Was This Post Helpful? 0
  • +
  • -

#3 protexxi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 08-November 14

Re: HELP! Console.ReadLine() Crash

Posted 11 March 2015 - 11:51 AM

Posted Image

heres My error
Input string Was Not In a correct format
Was This Post Helpful? 0
  • +
  • -

#4 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 466
  • View blog
  • Posts: 2,212
  • Joined: 07-April 08

Re: HELP! Console.ReadLine() Crash

Posted 11 March 2015 - 12:00 PM

So what does that tell you? That means what ever your putting in your console isn't what int.Parse needs in order to work.

Here are the MSDN articles for Console.ReadLine() (NOTE what it says under Return Value)

Quote

The next line of characters from the input stream, or null if no more lines are available.


and Int32.Parse() (Note what it says under Exceptions)


Also it really doesn't seem like you understand the basics of the language. I would highly suggest you stop just "Banging away" at it and go get yourself some kind of Teach Yourself book. Your use of GOTO, and your lack of methods, loops, and your if blocks leads me to believe that you need to go back to the very basics.

Here are a few items for you to consider:

1. Your If(option == null) will never be hit because you explicitly say that option is equal to 8. Even if you didn't, int defaults to 0 and is not a nullable type.

2. Because of number 1, your else block will always be hit, and you will always be shown the invalid selection (which you spelled wrong.)

3. You don't use {}'s with your else which means that only the Console.Writeline will be included in the else statement, and the sleep and goto will always be run no matter what happens. Basically you've created yourself a very bad infinite loop.

4. the Environment.Exit is not the best way to go about exiting the program.

This post has been edited by rgfirefly24: 11 March 2015 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

#5 protexxi   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 08-November 14

Re: HELP! Console.ReadLine() Crash

Posted 11 March 2015 - 12:06 PM

i dont think you understood my question im testing the input with abnormal data. the program should take the data and say no thats not the right data do this or say yes this it the right data do that iv looked though that link about 5 time since starting this problem.
Was This Post Helpful? 0
  • +
  • -

#6 rgfirefly24   User is offline

  • D.I.C Lover
  • member icon


Reputation: 466
  • View blog
  • Posts: 2,212
  • Joined: 07-April 08

Re: HELP! Console.ReadLine() Crash

Posted 11 March 2015 - 12:17 PM

I don't think you get what I'm telling you. Int.Parse expects a very specific input. It expects a string that is a valid int. "1", "2", "3", etc. It will give you the exact error you're getting if you pass it anything else. So what do you think you need to do about it? HINT: You answered this question in your initial post. You need to validate that the input is correct before trying to parse it. One way you can do that is with int.TryParse().

Beyond that the rest of my post is very valid for you. Your sending yourself down a rabbit hole that leads you nowhere good. No developer in their right mind uses GOTO, and no employer would allow code like that to go out to a production system.
Was This Post Helpful? 0
  • +
  • -

#7 ybadragon   User is offline

  • Home Owner
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,647
  • Joined: 11-May 12

Re: HELP! Console.ReadLine() Crash

Posted 11 March 2015 - 01:14 PM

I'm with rgfirefly on this, if this is a college course like you say it is, I would recommend you get your money back. They shouldn't be teaching you to use GoTo statements.
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: HELP! Console.ReadLine() Crash

Posted 11 March 2015 - 02:48 PM

While I agree with everyone about goto, let's just leave it at that. We've known that "goto [is] considered harmful" since the 70s (when Dijkstra wrote the article). But like anything else, it has its place and time. For instance, a switch statement is actually a well-structured goto.

In this case, yes, goto is a bad idea. But we can just leave it at that.

What you want to do is write functions (also called methods) instead of labeled blocks of code.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7052
  • View blog
  • Posts: 23,972
  • Joined: 05-May 12

Re: HELP! Console.ReadLine() Crash

Posted 11 March 2015 - 07:09 PM

Nor should they be teaching you to use System.Environment.Exit().

It almost feels like somebody took their old FORTRAN or BASIC lesson plan and converted it over to use C#. Something is definitely wrong here.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1