I am stuck - Lect 20

Ok Lecture 20 on Booleans and comparisons…

I am at the challenge and not seeing it. Can’t figure it out. I will of course advance to see the solution. But raises the question.

What do do at these moments?

Here is what I wrote:

bool AskToPlayAgain()
{
	cout << "Do you want to play again? ";
	string Response = "";
	getline(cin, Response);

	cout << "Is it y? " << (Response[0] == 'y' || 'Y');
	cout << "Is it n? " << (Response[1] == 'n' || 'N');
	cout << endl;

	return false;
}

This does not do anything significant…I am not sure what else we need to do. The lecture does not give enough information to follow logically I believe, or by example like in previous lectures. Should I be digging in Books and sites to understand how to use Bool at this point?

It seems to me that something is missing… shouldn’t an “if” and an “else” be somewhere here? if yes then do this else do that?

Thanks,

Well I just watched the “solution” to this lecture and I am sorry to say but it simply does not follow based on the information we are given before the Challenge.

Please do not forget this is a complete newbie to C++ here. It maybe obvious to people with more familiarity with C++ that we are looking to “RETURN” something, but not to me.

Maybe another lecture is missing here for the basics.

I tried looking for Usage of Bool on the net like in stack overflow and I got more confused with “bool”. And definitely would not have come to the solution by myself in this case and with available information.

would appreciate some feeback here…in mean time moving on and hoping it will become evident…

Cheers!

I keep thinking on this and in the Challenge we are asked to Return True or false, I guess where my confusion comes is there. So far in this introduction we have not touched on the notion of returning stuff on a per function or method basis. I had this issue in previous lectures because of the same concept.

So feedback here is, maybe to have a lecture small word on Returning stuff…how and why and when…

On a side note, I am being asked by the course to rate it periodically, I think it is too early to do so so I am not rating it yet, besides in this frustrated state you would not want me to rate it either, it would not be wise :wink:

Cheers,

I understand your frustration, however, don’t feel down if you can’t do the challenges. The effort is valuable, and you weren’t as far off as you seem to think you were.

You don’t have to succeed in every challenge, he even says so, he just wants you to attempt them.

So let’s talk about your code.

cout << "Is it y? " << (Response[0] == ‘y’ || ‘Y’);
cout << "Is it n? " << (Response[1] == ‘n’ || ‘N’);

This is very close, and you intuitively have the right idea. Unfortunately C++ is a very strict language. So what you said was LITERALLY, does the response equal y, or the letter Y. NOT or does the response equal Y. Literally, or Y.

The reason this works is probably to complicated for a new person, but the quick and dirty is that the character ‘Y’ evaluates to a number, and any number that isn’t 0 is true in c++. So your statements both say or true. They will always print 1 because Y and N as numbers are not 0. If you want to know what numbers characters evaluate to you can google the “ascii table”. However, it’s enough to know that any character you will use as a new coder won’t evaluate to 0, which means they always evaluate to true if you don’t compare them to anything.

What you need to do is say does the response equal this, or does the response equal this. You have to be very explicit.

You’re very close, keep at it.

1 Like

Just for more clarity on how this would be explained as code:

cout << "Is it y? " << (Response[0] == 'y' || 'Y');

Should be:

cout << "Is it y? " << (Response[0] == 'y' || Response[0] == 'Y');

This isn’t intuitive to English speakers as you’re literally repeating the question twice. But you are indeed asking two different questions. :wink: to programming ‘y’ and ‘Y’ are completely and utterly different.

For some interesting reading check ASCII tables.

1 Like

One other thing is that when you return something from a function, the function
returns a value and then it exits the function. Meaning that in your code, you’re
returning false at the end of the function and never returning true. So the function
will always return false no matter what.

Privacy & Terms