Resolved Convert true/false bool to letters/text output

Wineitot

New member
Joined
Jan 26, 2023
Messages
2
Programming Experience
Beginner
Hi,

I have in
C#:
static void Main(string[] args) petObj.start();

And in the Pet class I have:
C#:
class Pet

public void Start()
{
ReadAndSavePetData();
DisplayPetInfo();
}
Then I have:
C#:
public void ReadAndSavePetData ()
{
char response = strGender[0];

if ((response == 'y') || (response == 'Y')
isFemale = true;
else
isFemale = false;
}
And last
C#:
public void DisplayPetInfo()
{
string textOutTwo = isFemale;
}
What I need is another step. I want the isFemale in string textOutTwo to put out girl if response is y/Y and boy if something else. So I need to convert true to girl and false to boy in some way. Now I only get true or false as the output. But I want girl or boy. Anyone that have an idea how to do this?
 
Last edited by a moderator:
C#:
textOutTwo = isFemale ? "Female" : "Male";

Note your setter method can be reduced; you don't have to run an if to generate a boolean and then assign another boolean in the if:

C#:
//long
if(age>70)
  old = true;
else
  old = false;

You can just assign the boolean the test generates, to the variable:

C#:
old = age>70;

Hence:
C#:
public void ReadAndSavePetData ()
{
  isFemale = strGender.ToUpper() == 'Y';
}
 
Thanks for the try cjard. I am a total beginner. I understand what you are doing. But I don't manage to implement it. I probably gave to little information. Because no matter what I do I get "CS0029 Cannot implicitly convert type 'string' to 'bool'. And for the code below I also get "CS0019 Operator'==' cannot be applied to operands of type 'string' and 'char'

C#:
 Console.Write("Is your pet a female (y/n)? ");

            string strGender = Console.ReadLine();
            isFemale = strGender.ToUpper() == 'Y';

C#:
string textOutTwo = "The " + animalSpieces + " " + animalName + " is a good " + isFemale ? "Girl" : "Boy" + "!";
            Console.WriteLine(textOutTwo);
 
Thanks for the try cjard. I am a total beginner. I understand what you are doing. But I don't manage to implement it. I probably gave to little information. Because no matter what I do I get "CS0029 Cannot implicitly convert type 'string' to 'bool'. And for the code below I also get "CS0019 Operator'==' cannot be applied to operands of type 'string' and 'char'

C#:
 Console.Write("Is your pet a female (y/n)? ");

            string strGender = Console.ReadLine();
            isFemale = strGender.ToUpper() == 'Y';

C#:
string textOutTwo = "The " + animalSpieces + " " + animalName + " is a good " + isFemale ? "Girl" : "Boy" + "!";
            Console.WriteLine(textOutTwo);
In the first snippet, you are indeed comparing a string with a character. The 'Y' should be "Y".

In the second snippet, you should use parentheses around the ternary expression:

string textOutTwo = "The " + animalSpieces + " " + animalName + " is a good " + (isFemale ? "Girl" : "Boy") + "!";
But note that this is much better written using string interpolation :

string textOutTwo = $"The {animalSpieces} {animalName} is a good {isFemale ? "Girl" : "Boy"} !";
 
I learned 2 new things today:
  1. You can put quotes inside the interpolated part without having to do any special escaping.
  2. Ternary operators within string interpolation need to be in parenthesis.
C#:
bool good = true;
var s = $"Today is a {(good ? "good" : "bad")} day.";

Without the parenthesis, the error is:
Code:
CS8361    A conditional expression cannot be used directly in a string interpolation because the ':' ends the interpolation. Parenthesize the conditional expression.
CS8076    Missing close delimiter '}' for interpolated expression started with '{'.
CS1003    Syntax error, ',' expected
CS8088    A format specifier may not contain trailing whitespace.
 
CS0019 Operator'==' cannot be applied to operands of type 'string' and 'char'
Apologies, I made a typo when I gave the code. I should have put:

C#:
isFemale = strGender.ToUpper()[0] == 'Y';

This will result in isFemale being true for inputs like:

Y
Yes
y
yes
yeah
yellow

...
 
CS0029 Cannot implicitly convert type 'string' to 'bool
You go this because C# understands this:

C#:
var str = "Hey " + isFemale ? "girl" : "boy";

To actually be this:

C#:
var str = ("Hey " + isFemale) ? "girl" : "boy";

//ie perhaps this:

var str = "Hey true" ? "girl" : "boy";

The ? operator needs a bool to the left of it, and "Hey true" is a string not a bool, hence the "cannot convert string to bool"

Using your own parenthesis tells C# what to calculate first:

C#:
 var str = "Hey " + (isFemale ? "girl" : "boy");


If you look at the Precedence table on C# operators and expressions - List all C# operators and expression you'll see that + is higher up than ?, so c# will try to do the "Hello" + isFemale first
 
Back
Top Bottom