hi please i need some help..i know how to write a code for postfix expression evaluation by taking input from user,, but what if all the data is in text file,,
pls help me modify my code , i have to read all the variables and values and also the expressions from text file and then evaluate them...

#include <iostream>
#include <stack> 
#include <string>
using namespace std;

void main()
{
    int i, choice = 1;
    string postfixExp;
    char token;
    float value, value1, value2;
    stack<float> s; //Declare a stack of floats



    while (choice != 0)
    {
        cout << "1. Evaluate a postfix expression" << endl;
        cout << "0. Exit " << endl;
        cout << "Enter the number for the option: ";

        cin >> choice;
        switch(choice)
        {
            case 1: cout << "Evaluate a postfix expression\n";
                    cout << "Enter the expression: ";
                    cin >> postfixExp;
                    i = 0;
                    token = postfixExp[i];
                    while((i < postfixExp.size()) && (token != '='))
                    {
                        if(isdigit(token))
                        {
                            value = token - '0';
                            s.push(value);
                        }
                        else
                        {
                            value2 = s.top();
                            s.pop();
                            value1 = s.top();
                            s.pop();
                            switch(token)
                            {
                                case '+': value = value1 + value2;
                                          break;
                                case '-': value = value1 - value2;
                                          break;
                                case '*': value = value1*value2;
                                          break;
                                case '/': value = value1/value2;
                                          break;
                            }
                            s.push(value);
                        }
               i++;
               token = postfixExp[i];
                    }
                    value = s.top();
                    s.pop();
                    cout << postfixExp << " " << value << endl;         
                    break;

            case 0: cout << "Exiting the program\n";
                    break;

            default: cout << "Invalid option\n";
                    break;
        }
     cout << endl;
    }
}

1) Welcome to DaniWeb!

2) When posting code to this board use code tags. How to use them is explained in the watermark in the message input box and in one of the "sticky" messages at the top of the board.
Some of the people here won't respond to your message if you don't use them, especially if you've been here a while.

3) Even if your compiler allows you to declare main() with a void return type, don't do it, at least not for any code you post here. Its not standard, it's not portable to many other compilers, and int is one keystroke less than void anyway.

4) Using files isn't difficult, in theory. You include the fstream header file, declare an instance of an appropriate stream object, associate a file with the stream, be sure the stream is open for use, then use the same operators/methods (like >> or << or getline(), etc) with the stream object that you would use with cin/cout (which are just stream objects that are predeclared for you) to do the reading/writing from the file. It's true that just about each of those steps has it's own foibles, so in addition to practice and reading other code samples, you should really have a good reference source available to use.

Once you get the above generics of file reading under control the problem becomes that you can only read the file if you know how the file was written. That is, does the file contain a single char per line, a single token per line, a single prefix expression per line, a single line in the whole file with each prefix expression delimited by some token, etc. All files are stored in binary and viewed by some technique, usually as something other than binary, usually as text or hexidecimal code. The program will use the binary data found in the file and use it as the variable type that your code indicates, whether it's int, char, double, etc.

commented: Well said +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.