How do I compare a charecter from a file with another character?

J

Jonathan

I've tried:

char ch;
ifstream fin("today.txt"); // open input file
while (fin.get(ch)) {
if (ch == "z")
cout << ch;}
fin.close();

but get the error

ISO C++ forbids comparison between pointer and integer

How do I work around this issue?

Thanks,
Jonathan
 
V

Victor Bazarov

Jonathan said:
I've tried:

char ch;
ifstream fin("today.txt"); // open input file
while (fin.get(ch)) {
if (ch == "z")

If you want to compare a character with a character, you need to
use

if (ch == 'z')

"z" is a _string_literal_ and not a _character_.
cout << ch;}
fin.close();

but get the error

ISO C++ forbids comparison between pointer and integer

How do I work around this issue?

By reading the right books. What book are you reading that doesn't
explain the difference between 'z' and "z"?

V
 
M

Mike Wahler

Jonathan said:
I've tried:

char ch;
ifstream fin("today.txt"); // open input file
while (fin.get(ch)) {
if (ch == "z")
cout << ch;}
fin.close();

but get the error

ISO C++ forbids comparison between pointer and integer

How do I work around this issue?

Compare with a character.

if(ch == 'z')

-Mike
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top