I want it to answer "true" if applicant meets all criteria, and "false" is they do not. This is what I have in VS:
namespace Boolean_Logic
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is your age?");
string yourAge = Console.ReadLine();
Console.WriteLine("Have you ever had a DUI? Answer with true or false");
string yourDuis = Console.ReadLine();
Console.WriteLine("How many speeding tickets do you have?");
string yourTickets = Console.ReadLine();
Console.ReadLine();
int applicantAge = 18;
bool applicantDui = false;
int applicantTickets = 3;
bool isQualified = (applicantAge >= 18 && !applicantDui && applicantTickets <= 3);
Console.WriteLine(isQualified);
Console.ReadLine();
}
}
}
CodePudding user response:
You need to parse the strings. Use int.TryParse and bool.TryParse
static void Main(string[] args)
{
Console.WriteLine("What is your age?");
string yourAgeResponse = Console.ReadLine();
if (!int.TryParse(yourAgeResponse, out var yourAge))
{
Console.WriteLine($"{yourAgeResponse} is not a valid age (must be an integer)");
return;
}
//do the same type of thing for the other parameters.
}
Something that will help you a lot (not only from a design standpoint, but also in that it will prevent this kind of problem) is to build your code in building blocks, instead dumping everything into main. Ask yourself what the code is doing and break it into steps:
- Get the age from the user
- Find out if the user has ever had a DUI
- Get the number of speeding tickets from the user
- Check if the user is qualified
- Tell the user whether or not they are qualified.
For each of these steps write a method signature. Lets start with step 1: We are going need an integer (the age of the user), and there are no parameters:
int GetAgeFromUser()
If we do this for all the steps we would get something like this:
int GetAgeFromUser()
bool HasUserHadDuis()
int GetSpeedingTicketsFromUser()
bool IsUserQualified(int age, bool hasDuis, int speedingTickets)
void TellUserIfTheyAreQualified(bool isQualified)
Your Main method becomes much more readable and easy to understand:
static void Main(string[] args)
{
int age = GetAgeFromUser();
bool hasDuis = HasUserHadDuis();
int speedingTickets = GetSpeedingTicketsFromUser();
bool isQualified = IsUserQualified(age, hasDuis, speedingTickets);
TellUserIfTheyAreQualified(isQualified);
}
Beyond readability, this has multiple additional benefits:
Your one large problem has been broken down into multiple small problems
Each method youve created is reuseable. (if you need to ask for some information again you can do that without re-writing the same code)
All your data types are now guaranteed to be correct
The last point is the relevant part to your current problem. Now that you have defined the signature of IsUserQualified, you know the parameters will always be of the expected type.
The last thing to do is to write each of the methods. For GetAgeFromUser
you would want to do something like this:
static int GetAgeFromUser()
{
int age;
while (!TryGetAgeFromUser(out age))
{
Console.WriteLine("Invalid age! age must be an integer.");
}
return age;
static bool TryGetAgeFromUser(out int age)
{
Console.WriteLine("What is your age?");
string response = Console.ReadLine();
return int.TryParse(response, out age);
}
}
At least that is how I would do it. The above code is probably a little over your head, but that's ok. you can write it your own way, or you can learn by trying to understand what I've written
CodePudding user response:
You need to convert the string you read to the approriate type.
Console.WriteLine("What is your age?");
string yourAgeStr = Console.ReadLine();
int applicantAge = Int32.Parse(yourAgeStr);
for boolean You probably want
string yourDuisStr = Console.ReadLine();
bool applicantDui = yourDuisStr == "Y" || yourDuisStr == "y";
same for tickets
Note you will be in for extra excitement if you enter non numeric info for the age prompt. See if you can work out how to fix that (there are 2 main ways or doing that, see if you can work them out)
edit - I notice you prompt the user to answer 'true' or 'false' (a pretty odd piece of UI )
so this is needed
bool applicantDui = yourDuisStr == "true";
or
bool applicantDui = bool.Parse(youDuiStr);
CodePudding user response:
Thanks everyone for the help! I was able to figure something out to get it work. The code that worked for me was:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Boolean_Logic
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What is your age?");
int yourAge = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
Console.WriteLine("Have you ever had a DUI? Please answer true/false.");
bool yourDuis = Convert.ToBoolean(Console.ReadLine());
Console.WriteLine("How many speeding tickets do you have?");
int yourTickets = Convert.ToInt32(Console.ReadLine());
Console.ReadLine();
bool isQualified = (yourAge >= 18 && !yourDuis && yourTickets <= 3);
Console.WriteLine(isQualified);
Console.ReadLine();
}
}
}