×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to evaluate a string in an if-statement
2

How to evaluate a string in an if-statement

How to evaluate a string in an if-statement

(OP)
I'm something of a C++ novice and I have this legacy code C++ program I am attampting to update. Basically, this program scrapes the contents of an emulator, formats, and then sends the data to a specialized printer. I am attempting to check the value of a single-character field on the emulator to determine how to format the printer data and am running into problems.

CODE

static char wBillingType[2];

[blah blah assign value blah]

if (wBillingType == "T") {
   [blah blah]
}
else {
   [blah blah]
}
As I understand it, wBillingType has a length of two to accomodate some sort of terminating value ('/o', IIRC), but I'm obviously doing something wrong becuase the statement never evaluates as TRUE. It's possible I am not setting wBillingTYpe correctly, but I am able to display the value as a character followed by a low-hex value. Any ideas of the obvious solution I am missing?
 

RE: How to evaluate a string in an if-statement

(OP)
Never mind, I found a solution elsewhere:

CODE

if (!stricmp(wBillingType,"T"))
 

RE: How to evaluate a string in an if-statement

Over 35 years ago this solution was found by a certain D.Ritchy...
Yet another (more effective;) solution:

CODE

if (*wBillingType == 'T')
 

RE: How to evaluate a string in an if-statement

Does *wBillingType work for wBillingType[2]?  I remember in the dim and distant past that it only worked on some compilers.  Got a feeling it doesn't work on AS400.  I'd feel safer using

CODE

if (wBillingType[0] == 'T')

RE: How to evaluate a string in an if-statement

(OP)
Ahhh, so using single quotes when referencing wBillingType[0] works! What is the difference between single and double quotes?

RE: How to evaluate a string in an if-statement

Or if you're feeling obscure

CODE

if (wBillingType[0] == "T"[0])

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

RE: How to evaluate a string in an if-statement

Single quotes for characters, double quotes for strings of characters

CODE

char sname[] = {'w', 'e', 'b', 'e', 'r', 'm', '\0'};
char dname[] = "weberm";
The contents of sname and dname are the same.

RE: How to evaluate a string in an if-statement

Builtin operator [] is defined as *(pointer+index) in C and C++. That's why it's commutative operator (see Salem's post). An array name implicitly converted in a pointer to the 1st element in all contexts except sizeof argument. That's why

CODE

*array == array[0] // array[0] == *(array+0)
and
array[0] == 0[array]
 

RE: How to evaluate a string in an if-statement

(OP)
Thanks, guys!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close