Repeating the question if user input is empty in C# [closed]
I'm having a basic problem with my school code here. The code needs to ask user input as a console program, but I need it to repeat the question when entered an empty field before proceeding. So far I've tried a lot of while-loops, !isStringNullorEmpty, string.lengths, tried to create a function that checks it and some if-statements. I can't get it to work on either one of these. The program proceeds to the end all of the time.
System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();
System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();
System.Console.Write("Give date of birth");
DateTime = dt = DateTime.Parse(System.Console.ReadLine();
c# loops
closed as off-topic by Default, Gerhard Barnard, EdChum, Umair, marsze Nov 13 '18 at 13:21
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Default, Gerhard Barnard, EdChum, Umair
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
I'm having a basic problem with my school code here. The code needs to ask user input as a console program, but I need it to repeat the question when entered an empty field before proceeding. So far I've tried a lot of while-loops, !isStringNullorEmpty, string.lengths, tried to create a function that checks it and some if-statements. I can't get it to work on either one of these. The program proceeds to the end all of the time.
System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();
System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();
System.Console.Write("Give date of birth");
DateTime = dt = DateTime.Parse(System.Console.ReadLine();
c# loops
closed as off-topic by Default, Gerhard Barnard, EdChum, Umair, marsze Nov 13 '18 at 13:21
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Default, Gerhard Barnard, EdChum, Umair
If this question can be reworded to fit the rules in the help center, please edit the question.
3
You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
– Erik Šťastný
Nov 13 '18 at 8:04
add a comment |
I'm having a basic problem with my school code here. The code needs to ask user input as a console program, but I need it to repeat the question when entered an empty field before proceeding. So far I've tried a lot of while-loops, !isStringNullorEmpty, string.lengths, tried to create a function that checks it and some if-statements. I can't get it to work on either one of these. The program proceeds to the end all of the time.
System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();
System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();
System.Console.Write("Give date of birth");
DateTime = dt = DateTime.Parse(System.Console.ReadLine();
c# loops
I'm having a basic problem with my school code here. The code needs to ask user input as a console program, but I need it to repeat the question when entered an empty field before proceeding. So far I've tried a lot of while-loops, !isStringNullorEmpty, string.lengths, tried to create a function that checks it and some if-statements. I can't get it to work on either one of these. The program proceeds to the end all of the time.
System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();
System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();
System.Console.Write("Give date of birth");
DateTime = dt = DateTime.Parse(System.Console.ReadLine();
c# loops
c# loops
asked Nov 13 '18 at 8:01
dunielduniel
111
111
closed as off-topic by Default, Gerhard Barnard, EdChum, Umair, marsze Nov 13 '18 at 13:21
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Default, Gerhard Barnard, EdChum, Umair
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Default, Gerhard Barnard, EdChum, Umair, marsze Nov 13 '18 at 13:21
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – Default, Gerhard Barnard, EdChum, Umair
If this question can be reworded to fit the rules in the help center, please edit the question.
3
You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
– Erik Šťastný
Nov 13 '18 at 8:04
add a comment |
3
You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
– Erik Šťastný
Nov 13 '18 at 8:04
3
3
You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
– Erik Šťastný
Nov 13 '18 at 8:04
You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
– Erik Šťastný
Nov 13 '18 at 8:04
add a comment |
5 Answers
5
active
oldest
votes
You could try
string firstName = null;
Console.WriteLine("Give first name");
while(string.IsNullOrWhiteSpace(firstName = Console.ReadLine()))
Console.WriteLine("OMG you had one job as a user of this application, to put in the right value!");
You can do the same for DateTime
Console.Write("Give date of birth");
while (!DateTime.TryParse(Console.ReadLine(),out var dob))
Console.Write("OMG you had one job as a user of this application, to put in the right value!");
Additional Resources
DateTime.TryParse Method
Converts the specified string representation of a date and time to its
DateTime equivalent and returns a value that indicates whether the
conversion succeeded.
add a comment |
you can write
System.Console.Write("Give first name");
String firstname = null;
while(firstname==null || firstname==""){
firstname = System.Console.ReadLine();
}
add a comment |
Try this:
String lastname = null;
Console.Write("Give last name: ");
while (!String.IsNullOrEmpty(lastname)) {
lastname = System.Console.ReadLine();
}
add a comment |
Try this
var firstname = string.Empty;
var lastname = string.Empty;
var dt = DateTime.MinValue;
do
{
System.Console.Write("Give first name");
firstname = System.Console.ReadLine();
} while (string.IsNullOrEmpty(firstname));
do
{
System.Console.Write("Give last name");
lastname = System.Console.ReadLine();
} while (string.IsNullOrEmpty(lastname));
do
{
System.Console.Write("Give date of birth");
dt = DateTime.Parse(System.Console.ReadLine());
} while (dt != DateTime.MinValue);
add a comment |
try this
bool value = true;
while (value==true)
{
System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();
if (firstname == "")
{
value = false;
break;
}
System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();
if (lastname == "")
{
value = false;
break;
}
System.Console.Write("Give date of birth");
DateTime dt = DateTime.Parse(System.Console.ReadLine());
if (dt.ToString()=="")
{
value = false;
break;
}
}
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could try
string firstName = null;
Console.WriteLine("Give first name");
while(string.IsNullOrWhiteSpace(firstName = Console.ReadLine()))
Console.WriteLine("OMG you had one job as a user of this application, to put in the right value!");
You can do the same for DateTime
Console.Write("Give date of birth");
while (!DateTime.TryParse(Console.ReadLine(),out var dob))
Console.Write("OMG you had one job as a user of this application, to put in the right value!");
Additional Resources
DateTime.TryParse Method
Converts the specified string representation of a date and time to its
DateTime equivalent and returns a value that indicates whether the
conversion succeeded.
add a comment |
You could try
string firstName = null;
Console.WriteLine("Give first name");
while(string.IsNullOrWhiteSpace(firstName = Console.ReadLine()))
Console.WriteLine("OMG you had one job as a user of this application, to put in the right value!");
You can do the same for DateTime
Console.Write("Give date of birth");
while (!DateTime.TryParse(Console.ReadLine(),out var dob))
Console.Write("OMG you had one job as a user of this application, to put in the right value!");
Additional Resources
DateTime.TryParse Method
Converts the specified string representation of a date and time to its
DateTime equivalent and returns a value that indicates whether the
conversion succeeded.
add a comment |
You could try
string firstName = null;
Console.WriteLine("Give first name");
while(string.IsNullOrWhiteSpace(firstName = Console.ReadLine()))
Console.WriteLine("OMG you had one job as a user of this application, to put in the right value!");
You can do the same for DateTime
Console.Write("Give date of birth");
while (!DateTime.TryParse(Console.ReadLine(),out var dob))
Console.Write("OMG you had one job as a user of this application, to put in the right value!");
Additional Resources
DateTime.TryParse Method
Converts the specified string representation of a date and time to its
DateTime equivalent and returns a value that indicates whether the
conversion succeeded.
You could try
string firstName = null;
Console.WriteLine("Give first name");
while(string.IsNullOrWhiteSpace(firstName = Console.ReadLine()))
Console.WriteLine("OMG you had one job as a user of this application, to put in the right value!");
You can do the same for DateTime
Console.Write("Give date of birth");
while (!DateTime.TryParse(Console.ReadLine(),out var dob))
Console.Write("OMG you had one job as a user of this application, to put in the right value!");
Additional Resources
DateTime.TryParse Method
Converts the specified string representation of a date and time to its
DateTime equivalent and returns a value that indicates whether the
conversion succeeded.
edited Nov 13 '18 at 10:39
answered Nov 13 '18 at 8:06
TheGeneralTheGeneral
28k63365
28k63365
add a comment |
add a comment |
you can write
System.Console.Write("Give first name");
String firstname = null;
while(firstname==null || firstname==""){
firstname = System.Console.ReadLine();
}
add a comment |
you can write
System.Console.Write("Give first name");
String firstname = null;
while(firstname==null || firstname==""){
firstname = System.Console.ReadLine();
}
add a comment |
you can write
System.Console.Write("Give first name");
String firstname = null;
while(firstname==null || firstname==""){
firstname = System.Console.ReadLine();
}
you can write
System.Console.Write("Give first name");
String firstname = null;
while(firstname==null || firstname==""){
firstname = System.Console.ReadLine();
}
answered Nov 13 '18 at 8:06
HarisHaris
679
679
add a comment |
add a comment |
Try this:
String lastname = null;
Console.Write("Give last name: ");
while (!String.IsNullOrEmpty(lastname)) {
lastname = System.Console.ReadLine();
}
add a comment |
Try this:
String lastname = null;
Console.Write("Give last name: ");
while (!String.IsNullOrEmpty(lastname)) {
lastname = System.Console.ReadLine();
}
add a comment |
Try this:
String lastname = null;
Console.Write("Give last name: ");
while (!String.IsNullOrEmpty(lastname)) {
lastname = System.Console.ReadLine();
}
Try this:
String lastname = null;
Console.Write("Give last name: ");
while (!String.IsNullOrEmpty(lastname)) {
lastname = System.Console.ReadLine();
}
answered Nov 13 '18 at 8:07
SysDragonSysDragon
7,610143870
7,610143870
add a comment |
add a comment |
Try this
var firstname = string.Empty;
var lastname = string.Empty;
var dt = DateTime.MinValue;
do
{
System.Console.Write("Give first name");
firstname = System.Console.ReadLine();
} while (string.IsNullOrEmpty(firstname));
do
{
System.Console.Write("Give last name");
lastname = System.Console.ReadLine();
} while (string.IsNullOrEmpty(lastname));
do
{
System.Console.Write("Give date of birth");
dt = DateTime.Parse(System.Console.ReadLine());
} while (dt != DateTime.MinValue);
add a comment |
Try this
var firstname = string.Empty;
var lastname = string.Empty;
var dt = DateTime.MinValue;
do
{
System.Console.Write("Give first name");
firstname = System.Console.ReadLine();
} while (string.IsNullOrEmpty(firstname));
do
{
System.Console.Write("Give last name");
lastname = System.Console.ReadLine();
} while (string.IsNullOrEmpty(lastname));
do
{
System.Console.Write("Give date of birth");
dt = DateTime.Parse(System.Console.ReadLine());
} while (dt != DateTime.MinValue);
add a comment |
Try this
var firstname = string.Empty;
var lastname = string.Empty;
var dt = DateTime.MinValue;
do
{
System.Console.Write("Give first name");
firstname = System.Console.ReadLine();
} while (string.IsNullOrEmpty(firstname));
do
{
System.Console.Write("Give last name");
lastname = System.Console.ReadLine();
} while (string.IsNullOrEmpty(lastname));
do
{
System.Console.Write("Give date of birth");
dt = DateTime.Parse(System.Console.ReadLine());
} while (dt != DateTime.MinValue);
Try this
var firstname = string.Empty;
var lastname = string.Empty;
var dt = DateTime.MinValue;
do
{
System.Console.Write("Give first name");
firstname = System.Console.ReadLine();
} while (string.IsNullOrEmpty(firstname));
do
{
System.Console.Write("Give last name");
lastname = System.Console.ReadLine();
} while (string.IsNullOrEmpty(lastname));
do
{
System.Console.Write("Give date of birth");
dt = DateTime.Parse(System.Console.ReadLine());
} while (dt != DateTime.MinValue);
answered Nov 13 '18 at 8:13
Gaurav MoolaniGaurav Moolani
9519
9519
add a comment |
add a comment |
try this
bool value = true;
while (value==true)
{
System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();
if (firstname == "")
{
value = false;
break;
}
System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();
if (lastname == "")
{
value = false;
break;
}
System.Console.Write("Give date of birth");
DateTime dt = DateTime.Parse(System.Console.ReadLine());
if (dt.ToString()=="")
{
value = false;
break;
}
}
add a comment |
try this
bool value = true;
while (value==true)
{
System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();
if (firstname == "")
{
value = false;
break;
}
System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();
if (lastname == "")
{
value = false;
break;
}
System.Console.Write("Give date of birth");
DateTime dt = DateTime.Parse(System.Console.ReadLine());
if (dt.ToString()=="")
{
value = false;
break;
}
}
add a comment |
try this
bool value = true;
while (value==true)
{
System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();
if (firstname == "")
{
value = false;
break;
}
System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();
if (lastname == "")
{
value = false;
break;
}
System.Console.Write("Give date of birth");
DateTime dt = DateTime.Parse(System.Console.ReadLine());
if (dt.ToString()=="")
{
value = false;
break;
}
}
try this
bool value = true;
while (value==true)
{
System.Console.Write("Give first name");
String firstname = System.Console.ReadLine();
if (firstname == "")
{
value = false;
break;
}
System.Console.Write("Give last name");
String lastname = System.Console.ReadLine();
if (lastname == "")
{
value = false;
break;
}
System.Console.Write("Give date of birth");
DateTime dt = DateTime.Parse(System.Console.ReadLine());
if (dt.ToString()=="")
{
value = false;
break;
}
}
edited Nov 13 '18 at 8:44
answered Nov 13 '18 at 8:35
Dhanushka DayawanshaDhanushka Dayawansha
32619
32619
add a comment |
add a comment |
3
You should post code with your try which contains loop. Then we can say you where is problem instead of writting whole code.
– Erik Šťastný
Nov 13 '18 at 8:04