473,606 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:

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
6 6953
Christian Binder
218 Recognized Expert New Member
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
1,501 Recognized Expert Top Contributor
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
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
1,501 Recognized Expert Top Contributor
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
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
Honduras2811
21 New Member
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

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

Similar topics

14
42383
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal = Array(\, /, :, *, ?, ", <, >, |) If Me.TextBox Contains illegal Then MsgBox "You entered illegal characters. Please try again." Me.TextBox = Null
23
3908
by: ern | last post by:
I have a program that runs scripts. If the user types "script myScript.dat" the program will grab commands from the text file, verify correctness, and begin executing the script UNTIL... I need a way to stop the execution with user input. I was thinking something like this: while(user hasn't pressed 'any key'){ keepExecutingScript(); }
3
2691
by: Steven Spits | last post by:
Hi group, We've developed a rather large ASP.NET WebApp with *A LOT* of input-fiels. Many of these fields have AutoPostBack set to true. I want to block all user input when the form is submitting on an autopostback. We happens frequently is that the user is already typing the next field when the forms comes back and shows an empty field again. This is really frustating.
7
4988
by: Daniel Walzenbach | last post by:
Hello, I want to create a Word XML file based on the input users make in a VB.NET application. I imagine creating a template in Word and saving it as a XML file. I then want to fill the template (in my application) based on the user input. When the document gets opened there should be fields users can change but others should be prevented from beeing changed (I know that an "open" file format like XML can always be modified to allow...
0
2033
by: Paulers | last post by:
hello VB masters I have an issue that I am hoping you can help me with. I am creating an application that accepts user input via a richtextbox. the user can enter multiple entries and I need to parse the information and account for whitespaces, newlines etc. I am hoping that the user enters one per line but that is not always the case. the input would look something like this abc def hij
3
3682
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the first part) needs to get input from the user, and I want to do this with cin.getline also. The problem I am getting, is when I run the program, the text if read in from the file correctly, but it seems to just skip over the cin.getline when I want...
1
2495
by: kingshuk | last post by:
OS : WinXp ORACLE VERSION : Oracle 10g I m unable to use the loop in PL/SQL with user input. When ever i am going to use the block of code is shows error...... /* TABLE WITH DATA TYPE */ Name Type SLNO NUMBER(5) ITEM VARCHAR2(10)
6
2519
by: AZRebelCowgirl73 | last post by:
Here is my problem: I have two java files: One named Car.java and the other named CarDealerApp.java: In the CarDealerApp program, I read in through user input the make, model, year and price of a car and put them into an array called carShop. This part of the program is running fine. Every thing in the CarDealerApp.java file runs correctly except for the last loop. I need to read in through user input the make of a car and then search the...
14
2394
by: n3o | last post by:
Hello Comp.Lang.C Members, I have an issue with user input that I have been trying to figure out for the longest. For instance, let's say you have something like this: void foo() { int num; printf("Please enter a number: "); scanf("%d", &num); // yes I know this function may throw a
0
8015
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7951
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5966
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5465
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1553
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1296
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.