Reputation: 1
I am trying to make a small cinema program, setting if statements depending on the users age, and one age is 12 A, meaning I have to ask if they are accompanied by an adult
Console.WriteLine(" Enter the number of the film you wish to see :");
int selection = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your age:");
int age = int.Parse(Console.ReadLine());
bool yes = true;
bool no = false;
with the first two options i have, everything goes smoothly.
if (selection == 3)
{
if (age >= 12)
{
Console.WriteLine("You may enter");
}
else
{
{
Console.WriteLine("Are you accompanied by an adult? Answer yes or no" );
Console.ReadLine();
if (true)
{
Console.WriteLine("You may pass.");
}
else if (false)
{
Console.WriteLine("You are not allowed.");
...
Here, no matter what I input it will go through the first conditional and end there or it is unreachable code if I write Console.ReadLine();
on the else
if statement.
Thanks in advance for any help.
Upvotes: 0
Views: 107
Reputation: 1
It looks like you are not storing or testing the answer to the question 'Are you accompanied by an adult?'. You need to store the answer from the readline method and test if the answer = yes or no.
IE: amend the top of your file to include a variable to check the age against:-
Console.WriteLine(" Enter the number of the film you wish to see :");
int selection = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your age:");
int age = int.Parse(Console.ReadLine());
bool isUserOldEnough = false;
And then modify your code to something like:-
if (selection == 3)
{
if (age >= 12)
{
Console.WriteLine("You may enter");
}
else
{
Console.WriteLine("Are you accompanied by an adult? Answer yes or no" );
if (Console.ReadLine().ToLower() == "yes") isUserOldEnough = true;
if (isUserOldEnough == true)
{
Console.WriteLine("You may pass.");
}
else
{
Console.WriteLine("You are not allowed.");
}
}
}
Upvotes: 0
Reputation: 376
You forgot to receive the value of the adult follow-up question.
Try this:
private static void Main(string[] args)
{
Console.WriteLine(" Enter the number of the film you wish to see :");
int selection = int.Parse(Console.ReadLine());
Console.WriteLine("Enter your age:");
int age = int.Parse(Console.ReadLine());
if (selection == 3)
{
if (age < 12)
{
Console.WriteLine("Are you accompanied by an adult? Answer yes or no");
string isAccompanied = Console.ReadLine();
if (isAccompanied.ToUpper().Equals("NO"))
{
Console.WriteLine("You are not allowed.");
return;
}
Console.WriteLine("You may pass.");
return;
}
Console.WriteLine("You may enter");
return;
}
}
Upvotes: 1
Reputation: 186843
You should check not true
or false
constants, but actual user input, say bool accompanied
:
if (selection == 3) {
if (age >= 12)
Console.WriteLine("You may enter")
else {
Console.WriteLine("Are you accompanied by an adult? Answer yes or no" );
// Trim() - let's be nice and allow user to leave leading/trailing spaces
string input = Console.ReadLine().Trim();
// accompanied if user's input "y" or "yes" (case insensitive)
bool accompanied = "yes".Equals(input, StringComparison.OrdinalIgnoreCase) ||
"y".Equals(input, StringComparison.OrdinalIgnoreCase);
if (accompanied)
Console.WriteLine("You may pass.");
else
Console.WriteLine("You are not allowed.");
}
}
Upvotes: 2
Reputation: 385
Rather than evaluating true, or false which will always give the same answer you need to check what the user wrote back to you by storing it in a variable.
if (age >= 12)
{
Console.WriteLine("You may enter");
}
else
{
string response = null;
while(response != "yes" || response != "no"){
response = Console.ReadLine("Are you accompanied by an adult? Answer yes or no" );
}
if (response == "yes")
{
Console.WriteLine("You may pass.");
}
//Only other way to get here is if they answered, "no" so don't need to check response
else{
Console.WriteLine("You are not allowed.");
}
}
Upvotes: 2
Reputation: 219107
You're reading the input, but not doing anything with it:
Console.ReadLine();
And you are trying to evaluate a hard-coded boolean literal which will never change:
if (true)
Instead, examine what's actually being input. For example:
var userInput = Console.ReadLine();
if (userInput.Equals("yes", StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("You may pass.");
}
else
{
Console.WriteLine("You are not allowed.");
}
Upvotes: 0
Reputation: 737
if you want "yes/no", you should store it in a string variable, evaluate if
condition according to this variable.
if (selection == 3)
{
if (age >= 12)
{
Console.WriteLine("You may enter");
}
else
{
{
Console.WriteLine("Are you accompanied by an adult? Answer yes or no" );
string res = Console.ReadLine();
if (res == "yes")
{
Console.WriteLine("You may pass.");
}
else if (res == "no")
{
Console.WriteLine("You are not allowed.");
Upvotes: 0