473,396 Members | 1,938 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,396 software developers and data experts.

Char Comparison Logic error

196 100+
Hello i seem to have trouble with comparing two chars

e.g.
This works if
Expand|Select|Wrap|Line Numbers
  1. (char == 'z')
but when i try if
Expand|Select|Wrap|Line Numbers
  1. (char != 'z')
it always returns false.

Heres the actual line of code-
Expand|Select|Wrap|Line Numbers
  1. while (var_name !=('a' || 'b' || 'c' || 'd' || 'e' || 'f'')  ) {
  2. code goes here
  3. }
  4.  
The problem is that even if var_name is equal to one of those values it loops infinitley.

Any help in this matter is greatly appreciated
Apr 10 '08 #1
3 1699
chazzy69
196 100+
Actually i did some more checking it seems rather to be a error with the or statement -

Expand|Select|Wrap|Line Numbers
  1. var_name !=('a' || 'A');
but
Expand|Select|Wrap|Line Numbers
  1. var_name !=('a');
works
Apr 10 '08 #2
chazzy69
196 100+
No need to reply i have got around the problem,

by using an if flag within the a while statement,

Thanks anyways
Apr 10 '08 #3
Banfa
9,065 Expert Mod 8TB
('a' || 'A') will have the value 1, || is a logical OR and outputs 0 or 1. It only outputs 0 if both sides of the operand are 0 so ('a' || 'A') = 1.

Therefore

Expand|Select|Wrap|Line Numbers
  1. var_name !=('a' || 'A')
is equivalent to
Expand|Select|Wrap|Line Numbers
  1. var_name != 1
which is, I suspect, not what you wanted. To test to see if var_name is not 'a' and not 'A' you need this expression
Expand|Select|Wrap|Line Numbers
  1. var_name != 'a' && var_name != 'A'
Apr 10 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

74
by: Peter | last post by:
Hi, So many times, I have seen compile warning: "you used a char* without initilize it", probably on the code like this: ------------ char* ptr; func(..., ptr); ----------
12
by: Francis Bell | last post by:
Hello, I've got a program that is reading in a data file of 25 lines. Here is an example of the first two lines: sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1 f/floating minnow/AAA...
2
by: Neil Zanella | last post by:
Hello, Consider the following program. There are two C style string stack variables and one C style string heap variable. The compiler may or may not optimize the space taken up by the two stack...
5
by: Marcus | last post by:
I'm struggling with a bit of code that maybe some of you could help me understand. This code processes character values from streaming data. I'm trying to get my head around being able to get at...
3
by: Vishal Ladha | last post by:
Hi ! I have been experimenting with char * for a while now. I have two pieces of code : Code1 : ===== char *ptr = "hello";
14
by: Santi | last post by:
I see in some code, I donīt remember now if it is c# or c++, that the when they perform a comparison they use the value first and then the variable, like: if( null == variable ){} Is there an...
7
by: tlyczko | last post by:
I have a char(11) for SSN, and I would like to default it to 123-45-6789 so I can avoid having nulls in this column, and so I can easily find the rows in which I need to have a 'correct' SSN...
9
by: subramanian100in | last post by:
Suppose we have char *a = "test message" ; Consider the comparison if (a == "string") ..... Here "string" is an array of characters. So shouldn't the compiler
4
by: HED | last post by:
I am learning c++, and there is a lot that i dont know, so if this is a stupid question, I am sorry. regardless, I am trying to get my program to check and see if a char value is a certain value,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.