How to handle external if statement?

Let's say I have this in a file:

1
2
3
if 1 < 5 {
    print hello.
}


I'm assuming I'd compare the intergers in the string. However I can't seem to store the "1" from the string into an interger variable in the program. Is it possible to handle something like this when in a file being parsed?:

1
2
3
if (1 < 5) {
    print hello.
}
The typical way to do this is use stringstreams.

http://www.cplusplus.com/reference/sstream/stringstream/stringstream/
closed account (3qX21hU5)
Just to add onto what RB said, here is a quick example of using stringstreams to convert a string number number into a int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
#include <sstream>  // Need this for stringstream

using namespace std;


int main()
{
    string myNumber("5");
    int convertedNumber;

    // This won't work
    //if (myNumber == 5)
    //    cout << "Hello!";

    istringstream convert(myNumber);

    // ConvertedNumber now hold the 5 from the string.
    convert >> convertedNumber;

    if (convertedNumber == 5)
        cout << "It worked!!!";

    return 0;
}
Topic archived. No new replies allowed.