473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

using char in do while condition

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

Oct 9 '06 #1
2 4678
"Amanda" <am**********@yahoo.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
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
Oct 9 '06 #2

Peter Duniho wrote:
"Amanda" <am**********@yahoo.comwrote in message
news:11**********************@m7g2000cwm.googlegro ups.com...
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 =.
IMHO, it's better for the newsgroup to teach how to fish, than
to hand out fish. :)
>
Pete
Oct 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Simon | last post by:
Hi, If I have a string, (variable len), and I am looking for the first position of one char in array starting from position 'x' For example, // the 'haystack' $string = "PHP is great,...
7
by: Alex Vinokur | last post by:
I came across the following piece of code: #define ERROR -1 #define STATUS0 0 #define STATUS1 1 #define STATUS2 2 unsigned char foo() { if (/* condition-A */) return ERROR; /* is that...
6
by: Rudolf Bargholz | last post by:
Hi , I have the following tables ------------- PAX: Id Order_Id Name Position
6
by: Sona | last post by:
Hi, What's the advantage/disadvantage of using a "const char*" over a "char*" ? I read some place that char* are string literals that on some machines are stored in a read only memory and cannot...
2
by: fran | last post by:
Server: IBM XSERIES 225 (Intel Xeon CPU 2.40GHz 1GB RAM) Operating System: Linux RedHat ES 2.1 kernel 2.4.9 Languaje: C++ Compiler: gcc 2.96 Libs: pthread We are in need of your help in...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
8
by: Jack | last post by:
So from what I have read there is no end of file char like \0 (for text) that marks the end of a binary file. Is this true? If this is true the only way one can read a binary file is to get the...
10
by: thh108688 | last post by:
Hi all, I have got a question here perhaps someone can provide me some good information or suggestion. In the Access Form I have created 2 combo box one is Activity combo box, the other is...
8
by: lovecreatesbea... | last post by:
Thank you for your time. #include <stdio.h> int main(void) { int c; while ((c = getchar()) != EOF){
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.