exit when '!' is inputted

helpbeginner

New member
Joined
Sep 10, 2016
Messages
2
Programming Experience
Beginner
Just trying to get the console to recognize lower and upper case letter(which i got to work) and getting it to exit when '!' is inputted... (which is what i can not get). I figure because its a char? i tried to do " while (char.IsLower(response) != !)"....Am i missing a variable? Is there a far more simple while loop i could use?
            char response;
           
            WriteLine("Please enter a lower case letter: ");
            response = Convert.ToChar(ReadLine());

            while (char.IsLower(response))
            {
                WriteLine("OK!");
                response = Convert.ToChar(ReadLine());
                
                while (char.IsUpper(response)) 
                { 
                    WriteLine("This is not a lowercase letter!");
                    response = Convert.ToChar(ReadLine());
                }             
            }
 
Last edited by a moderator:
Firstly, let's consider what you said you tried, i.e.
C#:
while (char.IsLower(response) != !)
What does 'char.IsLower' do? It tests whether a char is lower case and returns a bool that will be true if the char is lower case and false otherwise. Can the values true and false ever be equal to an exclamation mark? Of course not. That's a meaningless comparison. The thing to do in this situation - in ANY situation - is to first think through the steps that you need to perform and ONLY when you have those steps clearly sorted out - even written down with pen and paper - should you start to write code and then you write code to actually implement the steps. That way, you can always compare your code to your steps to see if the code does what is intended. As it stands, what do you have to compare your code to to know whether it's right or not? All you have is the end result, which can't tell you anything about WHERE in your code a problem exists.

So, what are the steps?

1. Prompt the user.
2. User enters response.
3. Convert response to char.
4. Test whether char meets exit condition.
5. If char does meet exit condition, exit.
6. Test whether char is lower-case letter.
7. If char is lower-case letter, notify user of success.
8. If char is not lower-case letter, notify user of failure.
9. Return to 1.

Note that there is only one place where those steps go back to a previous step. How many loops do you think your code should have?
 
I got it to work! this is what I came up with:
            char response;


            WriteLine("Please enter a lower case letter: ");
            response = Convert.ToChar(ReadLine());


            while (response != '!')
            {


                while (char.IsLower(response))
                {
                    WriteLine("OK!");
                    response = Convert.ToChar(ReadLine());
                }
                WriteLine("This is not a valid entry!");
                response = Convert.ToChar(ReadLine());
            }


Thank you! I am still trying to get my brain to work as a "programmer". I love the way you explained it, was very helpful without giving me the answer.
 
Last edited by a moderator:
As you can see, I had already fixed the formatting of the code snippet in your first post. I have now fixed the formatting in your second post as well. Please format your own code snippets so that we may read them comfortably in future. That includes not removing the leading whitespace from the first line while leaving it on all the rest.
 
It seems that you completely missed my point. I said that the steps I provided included only one place where they went back to a previous step and asked how many loops that meant should be in the code. The correct answer is 1. You have more than one loop in your code, thus your code does not implement the steps I provided. You don't have to implement the steps I provided but do you actually have your own steps or are you just plucking code out of the air as you go? It looks like the latter to me.
 
Back
Top Bottom