0

Using Microsoft visual c++ I have this program. I want to read in expressions line by line and produce output just as it does, but with expressions from a file. How would I do this? Also where would I place the input file for it to be read in? I am more familiar with java not C++.

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

#include "expression.h"
#include "subexpression.h"
#include "symboltable.h"
#include "parse.h"

SymbolTable symbolTable;

void parseAssignments();

int main()
{

    Expression* expression;
    char paren, comma;
    cout << "Enter expression: ";
    cin >> paren;
    expression = SubExpression::parse();
    cin >> comma;
    parseAssignments();
    cout << "Value = " <<(int) expression->evaluate() << endl;
    return 0;
}

void parseAssignments()
{
    char assignop, delimiter;
    string variable;
    double value;
    do
    {
        variable = parseName();
        cin >> ws >> assignop >> value >> delimiter;
        symbolTable.insert(variable, value);
    } while (delimiter == ',');
}
user3602515
  • 19
  • 3
  • 9

1 Answers1

1

You would have to use ifstream and ofstream from the fstream library to read and write to files

Read from file

char data[10];
ifstream file("text.txt");
file >> data;

Write to file

char data[10];
ofstream file("text.txt");
file << data;

Then read line by line

vector<string> string[Amount of lines];
int i = 0;
while (!file.eof())
{
    getline(file, string[i]);
    i++;
}

don't forget to close file

answer for line by line -> Link

Source -> Link2

More info on fstream -> Link3

Prompt: I want to read in expressions line by line and produce output just as it does, but with expressions from a file

By the way, what kinds of "expressions from a file" do you want?

Community
  • 1
  • 1
  • possible duplicate of http://stackoverflow.com/questions/24987600/reading-from-txt-file-line-by-line-in-c – Irrational Person Dec 14 '14 at 22:25
  • Expressions such as: (x + (y * 3)), x = 2, y = 6;. The program understands input such as this. – user3602515 Dec 14 '14 at 22:30
  • so is this program going to take in expressions and store them in arrays/vectors and then evaluate them? – Irrational Person Dec 14 '14 at 22:33
  • no I just want it to read in the first expression solve it and output, then repeat for each line. the code up above will ask the user to enter in an expression which will look like the example that i posted above "(x + (y * 3)), x = 2, y = 6;" and then it solves it. I just want to do the exact same thing but with an input file. – user3602515 Dec 14 '14 at 22:36
  • Fyi, [`while (!file.eof())` is this answer is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – WhozCraig Dec 14 '14 at 23:04
  • This is the wrong context. This problem has nothing to do with your question. (file.eof()) is correct because of this example - http://www.cplusplus.com/reference/ios/ios/eof/ – Irrational Person Dec 14 '14 at 23:24
  • Give me one example of how file.eof() is wrong and I will change my solution. – Irrational Person Dec 14 '14 at 23:26