char comparison help

H

HED

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, and then
act appropriately. The compiler goes into fits when I do this, and if
I double click the error it opens ostream, and i figure that I really
have no business messing with that.

So, my code looks something like:

choice is a char and destination is a string. I think that the real
place that i have having trouble is in the comparing a char part of
the code. (basically I dont know how to do it and would like you to
tell me.) I have googled it, but I have a hard time finding code
snippets that explain how to do stuff.


if (choice = a)
destination = "someplace";


Thanks in advance for the help, if I need to post further
claification, please let me know.

--HED
 
S

Scott McPhillips [MVP]

HED said:
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, and then
act appropriately. The compiler goes into fits when I do this, and if
I double click the error it opens ostream, and i figure that I really
have no business messing with that.

So, my code looks something like:

choice is a char and destination is a string. I think that the real
place that i have having trouble is in the comparing a char part of
the code. (basically I dont know how to do it and would like you to
tell me.) I have googled it, but I have a hard time finding code
snippets that explain how to do stuff.


if (choice = a)
destination = "someplace";


Thanks in advance for the help, if I need to post further
claification, please let me know.

--HED

= is the assignment operator.

== is the comparison operator.

a look like a variable name. To compare choice with the char a use:
if (choice == 'a')
 
G

Gianni Mariani

HED said:
I am learning c++ ...
Thanks in advance for the help, if I need to post further
claification, please let me know.

choice = a ?

Maybe you mean

choice == 'a'

?

Unless you define the `a` you're going to get undefined identifier
errors and if you use `=` (assignment) instead of `==` (equality
compare) you're probably not going to get behaviour you want.
 
K

Kai-Uwe Bux

HED said:
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, and then
act appropriately. The compiler goes into fits when I do this, and if
I double click the error it opens ostream, and i figure that I really
have no business messing with that.

So, my code looks something like:

choice is a char and destination is a string. I think that the real
place that i have having trouble is in the comparing a char part of
the code. (basically I dont know how to do it and would like you to
tell me.) I have googled it, but I have a hard time finding code
snippets that explain how to do stuff.


if (choice = a)

Typo. Make that:

if ( choice == a )
^^
destination = "someplace";


Thanks in advance for the help, if I need to post further
claification, please let me know.

The comparison operators are

== equal
!= not equal
< less
<= less or equal (aka. not greater)
> greater
>= greater or equal (aka. not less)

You need to distinguish them from the assignment operators

= assignment
<<= shift in place

Best

Kai-Uwe Bux
 
H

HED

choice == 'a'
The comparison operators are

== equal
!= not equal
< less
<= less or equal (aka. not greater)
> greater
>= greater or equal (aka. not less)

You need to distinguish them from the assignment operators

= assignment
<<= shift in place
These are excatly (I think) what I am looking for. Thank you
 

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

Members online

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top