10 Replies - 364 Views - Last Post: 22 August 2011 - 11:14 PM Rate Topic: -----

#1 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Having problems with this program building and am not getting errors

Posted 22 August 2011 - 08:56 PM

I'm teaching myself C#, since I want to build in XNA, I know some C++ (been going to school for it) but am stuck on why this programe isn't working, especially since I'm using code from an Adobe PDF book on learning C#. Iam not getting errors just the output is not showing what the program should do. Here is the code your help is appreciated so I can move onto my next lesson, thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SwitchCase
{
    class SwitchCaseExample
    {
        //Demonstrates the use of switch...case statement along with
        //the use of command line argument
        static void Main(string[] userInput)
        {
            int input = int.Parse(userInput[0]);
            //convert the string input to integer
            //will throw a runtime exception if there is no input at runtime or if
            //the input is not castable to integer
            switch (input)   //what is input
            {
                case 1:  //if it is 1
                    Console.WriteLine("You typed 1 (one) as the first command line argument");
                    break;  //get out of the switch block
                case 2:  //if it is 2
                    Console.WriteLine("You typed 2 (two) as the first command line argument");
                    break; //get out of the switch block
                case 3:  //if it is 3
                    Console.WriteLine("You typed 3 (three) as the first command line argument");
                    break;  //get out of the switch block
                default: // if it is not any of the above
                    Console.WriteLine("You typeda number other than 1,2,3");
                    break;  //get out of the switch block
            }

        }
    }
}



Oh and how do I turn line numbers on is visual studio 2010 Ultimate using C#?

Is This A Good Question/Topic? 0
  • +

Replies To: Having problems with this program building and am not getting errors

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Having problems with this program building and am not getting errors

Posted 22 August 2011 - 08:58 PM

Works fine here, what makes you think it isn't working? Have you tried starting it with cntl-F5 so the console window doesn't immediately close?


And It's Tools->Options, Text Editor, C#, General, Line Numbers on that page

This post has been edited by Momerath: 22 August 2011 - 08:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Having problems with this program building and am not getting errors

Posted 22 August 2011 - 09:07 PM

its not working for me, here is the output I'm receiving

Unhandled exception: System.Indexoutofrangeexception: Index was outside of the bounds of the array at switchCase Switchcase.example.Main string[] user input c: in users/gmoney93079/documents/visual studio 2010/projects/switcase/switchcase/program/.cs:line 14
press any key to continue..........
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Having problems with this program building and am not getting errors

Posted 22 August 2011 - 09:12 PM

Did you set a command line parameter before you ran the code? If you are doing it in Visual Studio, it's Project-><project name> Properties, Debug tab.
Was This Post Helpful? 0
  • +
  • -

#5 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Having problems with this program building and am not getting errors

Posted 22 August 2011 - 09:16 PM

what would I need to put there? Sorry confused on this for I never had to do this before when using C++ in visual studios 2008 express
Was This Post Helpful? 0
  • +
  • -

#6 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Having problems with this program building and am not getting errors

Posted 22 August 2011 - 09:56 PM

Well your program expects you to enter a number there.
Was This Post Helpful? 0
  • +
  • -

#7 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Having problems with this program building and am not getting errors

Posted 22 August 2011 - 10:06 PM

sorry I'm still unsure what I need to do in this section can someone please help, thanks;)

why would I need to enter a # there? Sorry confused when I go to this section am I supposed to place something in the box for command line arguments? and if so what am I supposed to place there? Basiclly very other lesson has built fine for me until now, I have never needed to mess with this section before so I am not understanding what your asking me to do here.

am I supposed to place the numbers 1,2,3 there?
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

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


Reputation: 4440
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: Having problems with this program building and am not getting errors

Posted 22 August 2011 - 10:42 PM

You're trying to use the variable you named "userInput" (which is traditionally named "args"). It's the only parameter for the Main method. This is where all command line arguments are stored in. So since you're trying to use it, you have to provide command line arguments.

Read this: http://msdn.microsof...7(v=vs.71).aspx

This post has been edited by Curtis Rutland: 22 August 2011 - 10:46 PM

Was This Post Helpful? 0
  • +
  • -

#9 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Having problems with this program building and am not getting errors

Posted 22 August 2011 - 10:52 PM

So in my program example would I go to that section project-> properties-> debug and put userInput in that box and then the program would work correctly?
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland  Icon User is offline

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


Reputation: 4440
  • View blog
  • Posts: 7,721
  • Joined: 08-June 10

Re: Having problems with this program building and am not getting errors

Posted 22 August 2011 - 11:01 PM

Try it and find out. But putting the name "userInput" won't do what you want. You put the actual input. Of course, if you're not planning on using command line parameters, and would rather prompt for input, you could do this with Console.ReadLine instead. I'm not sure if you're actually trying to use parameters or not.

Parameters means that you start the program with the parameters after the name. For instance:

ping -t google.com


Starts the program "ping" with parameters "-t" and "google.com". On the other hand, if you wanted to start the program and have it say "Please enter a number", you'd use Console.ReadLine after the prompt to capture user input.
Was This Post Helpful? 0
  • +
  • -

#11 Gmoney93079  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 256
  • Joined: 19-September 09

Re: Having problems with this program building and am not getting errors

Posted 22 August 2011 - 11:14 PM

I understand from the link you showed me but what is it that I would need to do for my program everything I try does not seem to work, sorry this is the first time I have ever used command line parameters, and your help is appreciated;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1