2
#include <iostream>
#include <fstream>
#include <cstring>
#define MAX_CHARS_PER_LINE 512
#define MAX_TOKENS_PER_LINE 20
#define DELIMITER " "
using namespace std;
int main ()
{
    //char buf[MAX_CHARS_PER_LINE];
    string buf; // string to store a line in file
    fstream fin;
    ofstream fout;
    fin.open("PiCalculator.h", ios::in);
    fout.open("op1.txt", ios::out);
    fout<< "#include \"PiCalculator.h\"\n";
    static int flag=0; //this variable counts the no of curly brackets

    while (!fin.eof())
    {
        // read an entire line into memory
        getline(fin, buf);
        //fout<<buf.back()<<endl;
        if(buf.back()== "{" && buf.front() !='c'){
            flag++;
            fout<<buf<<endl;
        }
        if(flag > 0)
            fout<<buf<<endl;
        if(buf.back()== "}"){
            flag--;
            fout<<buf<<endl;
        }
    }
    cout<<buf.back()<<endl;
    return 0;
}

Here I'm getting the error in the if condition:

if(buf.back()== "{" && buf.front() !='c')

The error states that: ISO C++ forbids comparison between pointer and integer [-fpermissive]

Can anyone help me in sorting out the problem ??

amulya349
  • 1,154
  • 1
  • 14
  • 23

4 Answers4

1

The comparison you have might be invalid due to you checking for "{" for bug.back and 'c' for bug.front. I assume that the types returned by the back and front function have to be the same. One is in single quotes, and the other "{" is double quotes. Thats the issue.

Change "{" to '{'

Hope this helps.

Balaji
  • 51
  • 2
1

As noted by Samuel, std::string::back returns a char&; (e.g., see http://en.cppreference.com/w/cpp/string/basic_string/back) and you're comparing it to a string literal (double quotes signify a string, single quotes a char).

It's not clear why it isn't necessary to have a #include <string> directive in this case, but it would be best practice to have it. It's not obvious that you need <cstring>, though.

I recommend compiling with warnings on; if I compile your code that way, I get reasonably helpful error messages:

g++ -Wall -Wextra -std=c++11    q3.cc   -o q3
q3.cc: In function ‘int main()’:
q3.cc:24:25: warning: comparison with string literal results in unspecified behaviour [-Waddress]
         if(buf.back()== "{" && buf.front() !='c'){
                         ^
q3.cc:24:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
q3.cc:30:25: warning: comparison with string literal results in unspecified behaviour [-Waddress]
         if(buf.back()== "}"){
                         ^
q3.cc:30:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

This is gcc version 4.8.2.

Rory Yorke
  • 1,576
  • 8
  • 10
0

buf.back() returns either a char or a char &.

What you are comparing is char with a string literal "{" which won't work.

change this line to (and all others where you compare with a string literal:

 if(buf.back()== '{' && buf.front() !='c'){

Note the '{'

Samuel
  • 5,600
  • 32
  • 58
0
buf.back()== "{"

The string literal "{" decays into a pointer and the buf.back() returns an int, and hence u get the error

Please change that to

buf.back() == '{'
Sakthi Kumar
  • 2,967
  • 13
  • 27