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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michelle eccles

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

    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:

    Code:
    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<count; i++)
    		sum = sum + arrayA[i];
    		average=sum/count;
    		Console.Out.WriteLine("the average of your numbers is: " + average);
    	}
    		
    }
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    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.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      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.

      C# iteration statements (for, foreach, do, and while) repeatedly execute a block of code. You use those statements to create loops or iterate through a collection.

      Comment

      • michelle eccles

        #4
        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:

        Code:
        	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.

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          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:

          Code:
          do
          {
            // instructions
          } [b]while (condition);[/b]
          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.

          Comment

          • michelle Eccles

            #6
            oops! you are absolutely right - syntax! syntax! Here is the do-while code that works correctly and THANKS!

            Code:
            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<count; i++) 
                    sum = sum + arrayA[i]; 
                    average=sum/count; 
                    Console.Out.WriteLine("the average of your numbers is: " + average); 
                } 
              
            }

            Comment

            • Honduras2811
              New Member
              • Apr 2014
              • 21

              #7
              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.
              Last edited by Honduras2811; Apr 7 '14, 01:57 PM. Reason: Left out some white space. :)

              Comment

              Working...