Solved

How do I get a C++ program to read input from a text file?

Posted on 2010-11-17
7
652 Views
Last Modified: 2012-05-10
How do I get a C++ program to read input from a text file?  I have a program that works when entering input directly from the console.  I have modified it to accept input from a text file, but it currently will not compile.  It appears that the problem is associated with the paramaters being passed.
#include <iostream>
#include <fstream>  //
#include <strstream>  //
#include <string>
#include <vector>
using namespace std;

#include "DivideByZeroException.h" // DivideByZeroException class
#include "UninitializedException.h" // UninitializedException class
#include "SyntaxException.h" // SyntaxException class

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

SymbolTable symbolTable;

void parseAssignments(strstream& in);

int main()
{
	const int SIZE = 256;
	Expression* expression;
    char paren, comma, line[SIZE];
	string variable;  //added for IO file
    double value;     //added for IO file

	ifstream fin("input.txt");
	while (true)
	{
		symbolTable.init(variable, value);
		fin.getline(line, SIZE);
		if (!fin)
			break;
		strstream in(line, SIZE);
		
		in >> paren;
		cout << line << " ";

	//cout << "Enter expression: ";
	//cin >> paren;
 
	try
	{

		if (cin.peek() == '(')  //mistmatch left paren exception.
		{
			throw SyntaxException();
		}

		expression = SubExpression::parse(strstream&);
		
		if (cin.peek() == ')')       //mistmatch right paren exception.
		{
			throw SyntaxException();
		}

		if (cin.peek() != ',')		//missing comma exception. 
		{ 
			throw SyntaxException();
		}

		cin >> comma;
		parseAssignments(in);

		//if (cin.peek() != ';')		//missing comma exception. 
		//{ 
		//	throw SyntaxException();
		//}

		double result = expression->evaluate();
		cout << "Value = " << result << endl;
	}
	catch (DivideByZeroException &divideByZeroException)
	{
		cout << "Error: " << divideByZeroException.what() << endl;
	}
	catch (UninitializedException &uninitializedException)
	{
		cout << "Error: " << uninitializedException.what() << endl;
	}
	catch (SyntaxException &syntaxException)
	{
		cout << "Error: " << syntaxException.what() << endl;
	} 
	}
    return 0;
}

void parseAssignments(strstream& in)
{
    char assignop, delimiter;
    string variable;
    double value;

    do
    {
        variable = parseName();
		in >> ws >> assignop >> value >> delimiter;

		if (assignop != '=')
		{
			throw SyntaxException();
		}
		else
			symbolTable.insert(variable, value);
    }
    while (delimiter == ',');
}

Open in new window

0
Comment
Question by:NSing9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

by:NSing9
ID: 34161155
There appear to be problems with the sysntax here:
#include <iostream>
using namespace std;

#include "SyntaxException.h" // SyntaxException class

#include "expression.h"
#include "subexpression.h"
#include "operand.h"
#include "plus.h"
#include "minus.h"
#include "times.h"
#include "divide.h"

SubExpression::SubExpression(Expression* left, Expression* right)
{
    this->left = left;
    this->right = right;
}

Expression* SubExpression::parse(strstream&)
{
    Expression* left;
    Expression* right;
    char operation, paren;
    
    left = Operand::parse();
    cin >> operation;
    right = Operand::parse();
    cin >> paren;
	switch (operation)
    {
        case '+':
            return new Plus(left, right);
        case '-':
            return new Minus(left, right);
        case '*':
            return new Times(left, right);
        case '/':
            return new Divide(left, right);
		default:
			throw SyntaxException(); 
    }
    return 0;
}

Open in new window

0
 

Author Comment

by:NSing9
ID: 34161162
The definitions are here:
class SubExpression: public Expression
{
public:
    SubExpression(Expression* left, Expression* right);
    static Expression* parse();
protected: 
    Expression* left;
    Expression* right;
};

Open in new window

0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 34161565
what is the meaning of this line

Expression* SubExpression::parse(strstream&)

you have to pass an strstream argument else how do you expect the function to do what its supposed to do?

and strstream is deprecated. Consider using stringstream instead.
http://www.cplusplus.com/reference/iostream/stringstream/
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
trinitrotoluene earned 500 total points
ID: 34161573
this line needs to be corrected

expression = SubExpression::parse(strstream&);

as you can see no argument is being passed
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 34161582
the switch is a bit strange too

switch (operation)
    {
        case '+':
            return new Plus(left, right);
        case '-':
            return new Minus(left, right);
        case '*':
            return new Times(left, right);
        case '/':
            return new Divide(left, right);
            default:


you should be breaking out of a case statement when a specific case matches unless you have a need to do otherwise
0
 

Author Closing Comment

by:NSing9
ID: 34178291
It was understood that the wrong parameters were being assigned.  I had to figure out the correct solution myself.
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 34179691
ofcourse what did you expect? write out the complete solution for you. Thats not what EE does. We are experts not your assistants.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Simple Linear Regression
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question