449,353 Members | 1,277 Online
Need help? Post your question and get tips & solutions from a community of 449,353 IT Pros & Developers. It's quick & easy.

# How can i stop user input when an array is full?

 P: n/a I have an array with limit of 5 values. The code as writen allows the user to enter a 6th number before averaging the first five number. I want to stop the user from being able to enter that 6th number. thanks for any help. Code below: Expand|Select|Wrap|Line Numbers using System; public class SumNumbersArray {     public static void Main()     {         int [] arrayA=new int[5];           int number, count=0;         double sum=0.0, average;         String strNumber;             Console.Out.WriteLine("Enter a number (999 to quit): ");         strNumber=Console.ReadLine();         number=Convert.ToInt32(strNumber);               while(count<5 && number != 999)         {             arrayA[count]=number;             count++;               //loop thru asking for the next number until the while condition returns false             Console.Out.WriteLine("Enter a number (999 to quit): ");             strNumber=Console.ReadLine();             number=Convert.ToInt32(strNumber);         }           for(int i=0; i
6 Replies

 Expert 100+ P: 218 The problem is while(count<5 && number != 999). Within the while-loop you request/allow 5 inputs, but you do allow an input before the loop too, so it's 5+1. You should check after count++; if count < 5 before you allow input. Or you delete lines 13,14,15 (first input) and put lines 21 and 22 after line 27. This would mean you read values and assign to the array in one block, not like you do now, reading values and assign it the next loop-iteration. Oct 15 '10 #2

 Expert 100+ P: 1,501 Just to expand on Christian's post, I'll direct you to a do..while loop. This ensures the statements in your loop are executed at least once. http://msdn.microsoft.com/en-us/library/370s1zax.aspx Oct 15 '10 #3

 P: n/a Well - in response the Christian: that solution throws a compile error: (13,26): error CS0165: Use of unassigned local variable 'number' In response to Gary: I tried the do while as shown below and input accepted only two values before averaging: Expand|Select|Wrap|Line Numbers       do      {         arrayA[count]=number;                  count++;     }           while(count<5 && number != 999);          {                  //loop thru asking for the next number until the while condition returns false              Console.Out.WriteLine("Enter a number (999 to quit): ");              strNumber=Console.ReadLine();              number=Convert.ToInt32(strNumber);    However, in my original code if i change the while(count<5 to <4 that works. Oct 15 '10 #4

 Expert 100+ P: 1,501 Check your syntax, click the link and try to understand how it works :) What you've got there is a do-while loop that quickly spins count up to 5, then exits a loop. You then enter a new scope and do your output/conversion, then leave that scope. The syntax of a do..while loop is as follows: Expand|Select|Wrap|Line Numbers do {   // instructions } while (condition); Hopefully you can see where you went wrong. If you can't, let us know. Please note that while changing the test from 4 to 5 in your original code will indeed work, it's a good idea to learn and use proper programming practices. In your case, duplicating those lines before, and in your loop is generally accepted as a no-no :) As for what Christian said, if you're getting that error in your code check your scope. In your first post number is clearly defined (line 8) so if you're getting that error, you've either removed it or defined it farther down. Oct 15 '10 #5

 P: n/a oops! you are absolutely right - syntax! syntax! Here is the do-while code that works correctly and THANKS! Expand|Select|Wrap|Line Numbers using System;  public class SumNumbersArray  {      public static void Main()      {          int [] arrayA=new int[5];            int number, count=0;          double sum=0.0, average;          String strNumber;        do      {             Console.Out.WriteLine("Enter a number (999 to quit): ");          strNumber=Console.ReadLine();          number=Convert.ToInt32(strNumber);            arrayA[count]=number;                  count++;     }           while(count<5 && number != 999);          {            }            for(int i=0; i

 P: 21 Things are hard to see in that code. I never thought I would ever say this, but, in my opinion, you are using too much white space. The indentation is also a problem.It appears that the 'do' is directly below the '{' of the main function. At first I was wondering if the main function had ended somehow with no '}', but then I remembered what language this is. In my opinion, if you think about it, you could make finding problems visually a lot easier on yourself. Apr 7 '14 #7