-1

I tried to write a program that takes input as a string and then passes that string to a macro which is supposed to insert the string as a plain-text expression but the macro is not behaving as I would suspect.

#include <iostream>
#include <cmath>
#include <string>

#define  PARSE(a) a;


using namespace std;

int main() {
    string c ;
    int b;
    cin >> c;

    b = PARSE(c);

    cout << b;

    return 0;
}

This code will not compile, it gives an error saying that I cannot convert string to int, however PARSE(c) should not be treated like a string it should just be replaced by plain text.

1 Answers1

1

The error means you are really trying to convert std::string (c) to int and it can't be done (b). If you want to access the plain text, once you are using std::string, you should call the c_str() method, example:

cout << c.c_str();

@edit: If you are trying to do something like "eval()" from PHP, you can check this: https://stackoverflow.com/a/11078610/12385171

Matt
  • 53
  • 4