using char in do while condition

  • Thread starter Thread starter Amanda
  • Start date Start date
A

Amanda

If I do this, i get complaints saying that || cannot be used for char
and bool. Any help?

// options will be a,b,c,or e

do
{
Console.Write("Enter a selection from the following
list:\n" +
"a. Display ...................\n" +
"b. Display ....................\n." +
"c. Display ................\n.." +
"e. Exit this program\n\n");

ch = Char.Parse(Console.ReadLine());
Console.WriteLine("");

switch (ch)
{

} // end switch

} while ( ch== 'a' || ch = 'A' || ch == 'b' || ch = 'B' || ch
== 'c' || ch = 'C' || ch == 'e' || ch = 'E');

} // end main
 
Amanda said:
If I do this, i get complaints saying that || cannot be used for char
and bool. Any help?

[...]
} while ( ch== 'a' || ch = 'A' || ch == 'b' || ch = 'B' || ch
== 'c' || ch = 'C' || ch == 'e' || ch = 'E');

You are missing an equals sign in a few of your tests. The compiler
evaluates the expression as type "char" which is not a valid type for the
boolean test.

For what it's worth, this sort of test can be handled more efficiently, and
in an easier-to-write manner, by comparing the character as a range instead
of testing for all possibilities.

For example:

while (Char.ToLower(ch) >= 'a' && Char.ToLower(ch) <= 'e');

As an added benefit, it's much harder to leave out an important character in
the comparison when you do it this way. :)

One last tip: one of the first things I do when I get a compiler error that
I can't immediately figure out is to look very carefully at the line of code
generating the error. I mean, more carefully than I was already.
Non-obvious problems often turn out to be simple typographical errors such
as the one you've got here. But once you've written the code, it takes some
very thorough inspection to find the problem...your brain knows what you
meant to write, and will keep telling you the line is correct, hiding the
error from you unless you go very slowly.

Of course, sometimes it's good just to get someone else to look at the code.
Since they didn't write it, the error will be more obvious to them. But I'd
say that a public newsgroup isn't the best place to get help with syntax
errors. :) IMHO, it's better for the newsgroup to teach how to fish, than
to hand out fish. :)

Pete
 
Peter said:
Amanda said:
If I do this, i get complaints saying that || cannot be used for char
and bool. Any help?

[...]
} while ( ch== 'a' || ch = 'A' || ch == 'b' || ch = 'B' || ch
== 'c' || ch = 'C' || ch == 'e' || ch = 'E');

You are missing an equals sign in a few of your tests.

Oh, my fever is taking over me
The compiler
evaluates the expression as type "char" which is not a valid type for the
boolean test.
Thanks.

For what it's worth, this sort of test can be handled more efficiently, and
in an easier-to-write manner, by comparing the character as a range instead
of testing for all possibilities.

Yeah, I was about to look for the method name and was wondering hether
it's the same as in Java.
For example:

while (Char.ToLower(ch) >= 'a' && Char.ToLower(ch) <= 'e');

As an added benefit, it's much harder to leave out an important character in
the comparison when you do it this way. :)

Good point.
One last tip: one of the first things I do when I get a compiler error that
I can't immediately figure out is to look very carefully at the line of code
generating the error. I mean, more carefully than I was already.
Non-obvious problems often turn out to be simple typographical errors such
as the one you've got here. But once you've written the code, it takes some
very thorough inspection to find the problem...your brain knows what you
meant to write, and will keep telling you the line is correct, hiding the
error from you unless you go very slowly.
Exactly.


Of course, sometimes it's good just to get someone else to look at the code.
Since they didn't write it, the error will be more obvious to them. But I'd
say that a public newsgroup isn't the best place to get help with syntax
errors. :)

Of cousre not. I was just copying and pasting and so I don't know how I
missed that extra =.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top