How to check if input is an operand?

I am trying to find a way to check if an input is an operand.
Is it okay if I go:

1
2
3
  fstream file;
  file.open("sample.txt", ios::in);
  if (file==int) // not seems right. Intend to check if the input is operand or not 


Anyone helps me out?
Last edited on
Perhaps something like this, though really it depends on what is supposed to be in the file and what you intend to do with it.

1
2
3
4
5
6
7
8
9
10
11
12
    ifstream file("sample.txt");
    int number;
    
    if (file >> number)
    {
        cout << "number is an integer " << number << endl;
    }
    else
    {
        cout << "not an integer" << endl;
        file.clear(); // reset error flags
    } 
Last edited on
Yeah. I am trying to create a program which converts infix notation to postfix notation. In the text file it contains infix notation, and thus I have to convert file input to postfix notation.
Contents of textfile:
A + B * (C - D * E) / F
AB * CDE + (RST - UV / XX) * 3 - X5
A + B * (C - D * E) / F
AB * CDE + (RST - UV / XX) * 3 - X5

A looks like an operand, but it isn't an integer (rather it is just a symbol)
RST could be a single operand, or three variables R, S AND T multiplied together?
3 is an integer
X5 - I assume is a single variable

It might be necessary to go through the text a single character at a time, following some rules which you have to devise, in order to determine what each character or group of characters represents.




I actually have two text files, which in the second one, has mostly integers.


23 * (8 - 3 * 2) - 5 * (30 - 18)
12.53 + 21.2 * (33.2-15.4/2) - 8.5 * (2.6 + 12/2)


I will try it man, and I would ask for help again if I encountered any problems later.
Last edited on
Small comment, there are floating-point numbers such as 12.53 here.
Rather than trying to distinguish between integer and floating, point, I'd just use type double for all the numbers.
Topic archived. No new replies allowed.