I have a feeling this question might be a no brainer to all you. But is it possible to convert a string object to an istream obj. Because what I need to do is read the string expression and then calculate it using the "read_and_evaluate(istream&)" method in my if else statement. Any help would be grateful.

#include <cstdlib>      // Provides EXIT_SUCCESS
#include <iostream>  // Provides cin, cout
#include <stack>       // Provides stack
#include <string>      // Provides string
#include "Calc.h"
using namespace std;
int main( )
{
    Equation calculate;
	string user_input;
	double answer;
	int x;
    cout << "Type a string with some parentheses:\n";
    getline(cin, user_input);

    if (calculate.is_balanced(user_input))
	{
		cout << "Those parentheses are balanced.\n";
		answer = calculate.read_and_evaluate(*******); 
		cout << "That evaluates to " << answer << endl;
	}
	else
	{	
		cout << "Those parentheses are not balanced.\n";
	}
    cout << "all done.\n";
    return EXIT_SUCCESS;
}

Recommended Answers

All 4 Replies

>>But is it possible to convert a string object to an istream obj
No. Why would you want to do that anyway ?

Perhaps he would like to use some of the power of STL streams to parse the string.

Use an istringstream.

#include <iostream>
#include <sstream>
#include <string>

int main() {
  using namespace std;

  string full_name;
  string first_name;

  cout << "Please enter your full name> ";
  getline( cin, full_name );

  // Get the first name out of the string
  {
    istringstream ss( full_name );
    ss >> ws;
    getline( ss, first_name, ' ' );
  }

  cout << "Hello " << first_name << endl;

  return EXIT_SUCCESS;
  }

Hope this helps.

Thanks for the reply, I guess I'll try and write my own function to parse the string then put it on a stack in RPN. the reason why I was asking was the book im learning from had that function in one of its examples and i thought i could get around writing it. thanks again.

Duoas,
you might have saved my day it just might work. Now thats what i call magic.

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.