#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;
}
这里我在 if 条件中遇到错误:
if(buf.back()== "{" && buf.front() !='c')
错误指出:ISO C++ 禁止指针和整数之间的比较 [-fpermissive]
谁能帮我解决这个问题吗?
最佳答案
由于您检查了 bug.back 的“{”和 bug.front 的“c”,因此您的比较可能无效。我假设 back 和 front 函数返回的类型必须相同。一个是单引号,另一个“{”是双引号。这就是问题所在。
将“{”更改为“{”
希望这有帮助。
关于c++ - 在 C++ 中将字符与字符串的组成部分进行比较时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23403421/