Simple C# Question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Simple C# Question

  1. #1
    Join Date
    Apr 2010
    Posts
    2

    Simple C# Question

    I have a simple c# question about how to grab a users input and store it into an Array

    public void RunExercise()
    {
    int[] userIn = new int[9];
    Console.WriteLine("User, please enter number 0 - 10");


    }

    This is what I have so far...

    I just want it to ask the user to enter numbers 0-10
    and then store them in the array
    and then just sum up the numbers and print it out...
    If anyone could help me that would be much appreciated

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Simple C# Question

    Check out the Console.ReadLine method
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Apr 2010
    Posts
    2

    Re: Simple C# Question

    I looked at those before and I know how it works with normal int and strings. I just don't know how to use it when it comes to arrays

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Simple C# Question

    Then you need to figure out how to modify an array, which really has nothing to do with gathering user input.

    I also just noticed that your array has a Length of 9, but you're asking the user to enter 10 numbers. See a problem?

    To learn more about arrays check this out.
    It's not a bug, it's a feature!

  5. #5
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Simple C# Question

    just something simple...

    Code:
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = RunExercise();
    
            Console.WriteLine("You entered numbers: ");
    
            foreach (int i in array)
            {
                Console.WriteLine(i);
            }
    
            Console.Read();
        }
    
        public static int[] RunExercise()
        {
            int[] userIn = new int[9];
            int counter = 0;
    
            while (counter < 9)
            {
                Console.WriteLine("User, please enter number 0 - 10 [{0}]", counter + 1);
                userIn[counter] = int.Parse(Console.ReadLine());
    
                counter++;
            }
    
            return userIn;
        }
    }
    ===============================
    My Blog

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center