Strings to characters

I have used STL in C++ to read an input line as a string. Now, I need to make a comparison of this input and a character..say a. These are of incompatible types so this is not possible. How can I convert the string to a charcter to make a comparison or vice versa.

Also, I cannot read the fitst line as character due to other strings that follow. So reading the input as a character is not an option.
gvijay1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
string str;

//...

char ch = 'a';
char pch[2] = { ch, '\0' };

if (str.compare(pch) == 0)
    //...
else
    //...


or


string str;

//...

char ch = 'a';
string strch = ch;

if (str.compare(strch) == 0)
    //...
else
    //...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
Right,b ut the other way around is more efficient.  Just use the [] operator to obtain a single character from the string.  i.e somestring[x] returns the character at position x (with 0 being the first position).  So

if (str[0] == 'a')
0
chensuCommented:
>if (str[0] == 'a')

Yeah, I though of that. But what if str is "abc"? I think gvijay1 wants to determine if it is a single character.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

nietodCommented:
Oh I see.  you were thinking he/she wants to check if its ONLY 1 character, then you could do

if (str == "a"')  

or

if (str.length() == 1 && str[0] == 'a')

there are lots of options.
0
chensuCommented:
>if (str == "a")  

I think the character is not a constant. That's why I use pch or strch to form a string given a character.

>if (str.length() == 1 && str[0] == 'a')

This will do.
0
gvijay1Author Commented:
Dear Experts,
             I have tried out your suggestion of

if (str == "a") and also
(str[0] == 'a')

but it is not able to male the comparison between the character a and my string input of a. Do you have any suggestions. The program compiles but cannot make the correct comparison.  
0
chensuCommented:
Have you debugged it? What is the value of str after you input 'a'?
0
gvijay1Author Commented:
Oh I am sorry. There was another part of the program that was giving the error. This actually works.

Thanks!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Fonts Typography

From novice to tech pro — start learning today.