Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Slight problem with my array program

Slight problem with my array program

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12941702
    wrote on last edited by
    #1

    Instead of my program showing this: Enter element value: 0 Enter element value: 1 Enter element value: 2 ...and so forth it is currently showing this: Enter element value: 0 Enter element value: 0 Enter element value: 0 What am I doing wrong?

    using System;

    class InputArrayValues
    {
    static void Main()
    {
    int[] nums = new int[5];

        for (int i = 0; i < 5; i++)
        {
            Console.Write("Enter element value " + nums\[i\] + ": ");
    
                nums\[i\] = Convert.ToInt32(Console.ReadLine());
        }
    
        Console.WriteLine();
        Console.Write("The array values you entered are: ");
    
    
        for (int i = 0; i < 5; i++)
            Console.Write(nums\[i\] + " ");
    
        Console.WriteLine();
        Console.WriteLine();
    }
    

    }

    Richard Andrew x64R OriginalGriffO 2 Replies Last reply
    0
    • U User 12941702

      Instead of my program showing this: Enter element value: 0 Enter element value: 1 Enter element value: 2 ...and so forth it is currently showing this: Enter element value: 0 Enter element value: 0 Enter element value: 0 What am I doing wrong?

      using System;

      class InputArrayValues
      {
      static void Main()
      {
      int[] nums = new int[5];

          for (int i = 0; i < 5; i++)
          {
              Console.Write("Enter element value " + nums\[i\] + ": ");
      
                  nums\[i\] = Convert.ToInt32(Console.ReadLine());
          }
      
          Console.WriteLine();
          Console.Write("The array values you entered are: ");
      
      
          for (int i = 0; i < 5; i++)
              Console.Write(nums\[i\] + " ");
      
          Console.WriteLine();
          Console.WriteLine();
      }
      

      }

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2
              Console.Write("Enter element value " + nums\[i\] + ": ");
      

      Should be:

              Console.Write("Enter element value " + i + ": ");
      

      Do you see why?

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • U User 12941702

        Instead of my program showing this: Enter element value: 0 Enter element value: 1 Enter element value: 2 ...and so forth it is currently showing this: Enter element value: 0 Enter element value: 0 Enter element value: 0 What am I doing wrong?

        using System;

        class InputArrayValues
        {
        static void Main()
        {
        int[] nums = new int[5];

            for (int i = 0; i < 5; i++)
            {
                Console.Write("Enter element value " + nums\[i\] + ": ");
        
                    nums\[i\] = Convert.ToInt32(Console.ReadLine());
            }
        
            Console.WriteLine();
            Console.Write("The array values you entered are: ");
        
        
            for (int i = 0; i < 5; i++)
                Console.Write(nums\[i\] + " ");
        
            Console.WriteLine();
            Console.WriteLine();
        }
        

        }

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Richard is spot on with the problem you have noticed, but do yourself a favour and don't use Convert for user input. If the user mistypes - and everybody does - then Convert will fail and your app will crash. That's OKish with five values, but for a user it's very, very annoying if they type 19 of 20 and the app crashes and throws everything away! Instead, use the TryParse methods:

                int\[\] nums = new int\[5\];
        
                for (int i = 0; i < 5; i++)
                    {
                    do
                        {
                        Console.Write("Enter element value " + i + ": ");
                        string input = Console.ReadLine();
                        if (int.TryParse(input, out nums\[i\]))
                            {
                            break;
                            }
                        Console.WriteLine(input + " is not a number!");
                        } while (true);
                    }
        

        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups