0

This is the continuation of my previous question, In C++, how to read the contents of a text file, and put it in another text file?

After reading the contents of the file, and storing it in a vector, i want to compare the contents of the file with some integer like 0,

assume that the contents of my input.txt file are 0100.. So, after opening the second file, this is what i have given,

   string a = inputLines[0] ;
   cout << a[0] << '\n';
   if (a[0] == "0")
{
   cout << "Match" << '\n';
}

but, i am getting the error as pointer to integer comparison, how to avoid this error ?

Community
  • 1
  • 1
user2917559
  • 163
  • 1
  • 2
  • 12

2 Answers2

4

a[0] is a char, but "0" is a const char[2]. You want to use the character literal '0' instead (different quotes).

if (a[0] == '0')
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
0

a[0] has type char while "0" is a string literal that is it is represented in memory as an array of two characters

['0']['\0']

and is converted to pointer to its first element in the expression

a[0] == "0"

So the compiler issues the error because types of operands do not coinside. You need to compare character with character literal that is

if (a[0] == '0')

Though you could even write :) as

if (a[0] == "0"[0] )

because "0"[0] is a character. Or even the following way

if (a[0] == *"0" )

But of course it is much better to use simply character literal '0'.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335