473,509 Members | 3,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make program stop when empty string is entered?

I am creating a simple program using a couple methods that will ask the user to input data (type int) until an empty string is entered, after which the output will be displayed.

I need to figure out how to make the input stop when an empty string is entered and have the output displayed after that.

I am using a while loop, which needs to read:

Expand|Select|Wrap|Line Numbers
  1. int number;
  2.  
  3. while (number is not equal to an empty string)
  4. {
  5. statement;
  6. }
There also needs to be an if statement inside of the while loop that reads:

Expand|Select|Wrap|Line Numbers
  1. if (number is not equal to an empty string)
  2. {
  3. statement;
  4. }
  5.  
  6. else
  7. break;
Any help would be appreciated, thanks!
Oct 11 '10 #1
1 9391
Curtis Rutland
3,256 Recognized Expert Specialist
Well, the simplest thing to do is just check:
Expand|Select|Wrap|Line Numbers
  1. if(userInput == string.Empty)
Replace userInput with the name of the variable holding your user input.

Then, to break out of a loop, simply use the break statement:
Expand|Select|Wrap|Line Numbers
  1. if(condition)
  2.   break;
There's really no need to do both a break and a while, but you could always do something like this:

Expand|Select|Wrap|Line Numbers
  1. string input = Console.ReadLine();
  2. while(input != string.Empty)
  3. {
  4.   //do something with the input
  5.   input = Console.ReadLine();
  6. }
Oct 12 '10 #2

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

Similar topics

6
3450
by: Chris Connett | last post by:
I have an interesting problem with eval(). ---Background Info--- The program I'm working on launches a separate process with a popen to do some highly specialized processing of input, then this...
10
23356
by: David Graham | last post by:
Hi I have been busy going through the last weeks postings in an attempt to absorb javascript syntax (I guess it's not possible to just absorb this stuff in a passive way - I'm getting way out of...
11
4579
by: Dan Bass | last post by:
which one do you use and why? MyString == null || MyString == "" vs MyString == null || MyString.Length == 0
21
5730
by: M D | last post by:
You know how you assume you know until you find out you don't know. Well, I typed into a function definition "..., new String("")). I know what I want. Everyone reading this knows what I want....
3
1405
by: scorpion53061 | last post by:
I have little hope of resolving this as I have had to contact outside help. But I thought I would post it here to see if anyone could add an idea or solution. 1. I have a form in a Class...
6
37139
by: =?Utf-8?B?QmlsbEF0V29yaw==?= | last post by:
Hi, Thought this would be simple! If I want to validate a field that can contain "z" followed by 3 digits OR it's a completely empty string, how do I do that? The z123 bit is simple, but the empty...
14
1586
by: geebanga88 | last post by:
Hi have this program which keep crashing upon entering input, i think there is somthing wrong with the get_temp function returning the variables back to main?. #include <stdio.h> #include...
18
2100
by: Academia | last post by:
I let the use modify the text of a combobox and then I replace the selected item with the new text (in Keyup event). But if he sets the Text property to an empty string ("") that sets the...
2
4511
by: ilany | last post by:
Hi to all... Would greatly appreciate any suggestions. I'm trying to evaluate an expression during run-time that looks something like this: ...
2
2051
by: gershwyn | last post by:
I have a main report that show project details, grouped by Company and Location. Location is a text value that can be blank (empty string) if the company does not have multiple locations (and...
0
7234
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,...
0
7136
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...
0
7344
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,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
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...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3216
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
441
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...

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.