By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,353 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
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
  1. using System;
  2. public class SumNumbersArray
  3. {
  4.     public static void Main()
  5.     {
  6.         int [] arrayA=new int[5];
  7.  
  8.         int number, count=0;
  9.         double sum=0.0, average;
  10.         String strNumber;
  11.  
  12.  
  13.         Console.Out.WriteLine("Enter a number (999 to quit): ");
  14.         strNumber=Console.ReadLine();
  15.         number=Convert.ToInt32(strNumber);
  16.  
  17.  
  18.  
  19.         while(count<5 && number != 999)
  20.         {
  21.             arrayA[count]=number;
  22.             count++;
  23.  
  24.             //loop thru asking for the next number until the while condition returns false
  25.             Console.Out.WriteLine("Enter a number (999 to quit): ");
  26.             strNumber=Console.ReadLine();
  27.             number=Convert.ToInt32(strNumber);
  28.         }
  29.  
  30.         for(int i=0; i<count; i++)
  31.         sum = sum + arrayA[i];
  32.         average=sum/count;
  33.         Console.Out.WriteLine("the average of your numbers is: " + average);
  34.     }
  35.  
  36. }
  37.  
Oct 14 '10 #1
Share this Question
Share on Google+
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

GaryTexmo
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
  1.  
  2.     do 
  3.     {
  4.         arrayA[count]=number; 
  5.                 count++;
  6.     }
  7.  
  8.         while(count<5 && number != 999); 
  9.         { 
  10.  
  11.  
  12.             //loop thru asking for the next number until the while condition returns false 
  13.             Console.Out.WriteLine("Enter a number (999 to quit): "); 
  14.             strNumber=Console.ReadLine(); 
  15.             number=Convert.ToInt32(strNumber); 
  16.  
However, in my original code if i change the while(count<5 to <4 that works.
Oct 15 '10 #4

GaryTexmo
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
  1. do
  2. {
  3.   // instructions
  4. 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
  1. using System; 
  2. public class SumNumbersArray 
  3.     public static void Main() 
  4.     { 
  5.         int [] arrayA=new int[5]; 
  6.  
  7.         int number, count=0; 
  8.         double sum=0.0, average; 
  9.         String strNumber; 
  10.  
  11.     do 
  12.     {
  13.  
  14.  
  15.         Console.Out.WriteLine("Enter a number (999 to quit): "); 
  16.         strNumber=Console.ReadLine(); 
  17.         number=Convert.ToInt32(strNumber); 
  18.  
  19.         arrayA[count]=number; 
  20.                 count++;
  21.     }
  22.  
  23.         while(count<5 && number != 999); 
  24.         { 
  25.  
  26.         } 
  27.  
  28.         for(int i=0; i<count; i++) 
  29.         sum = sum + arrayA[i]; 
  30.         average=sum/count; 
  31.         Console.Out.WriteLine("the average of your numbers is: " + average); 
  32.     } 
  33.  
  34.  
Oct 15 '10 #6

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

Post your reply

Sign in to post your reply or Sign up for a free account.