Reading in an equation from a stream

If I have a stream that has coordinates of a point that are being added together, is there any way I can read in the doubles in the stream into specific variables and also be able to get the operator?

For example, if I read in "(3.0,4.7) + (5.3,6)", how can I store the 3.0 and 4.7 into specific variables, get the operator, and store the 5.3 and 6 into other variables?

It's the first time I'm using streams in C++ so I'm not sure how to read from them and didn't find help through google, thank you for your time.
Last edited on
First, read "(3.0,4.7) + (5.3,6)" from the stream using getline and insert it into a string. Now, notice that each number is preceded and followed by a parenthesis or a comma. So you could make a program that will go through each element of the string containing "(3.0,4.7) + (5.3,6)", and if the element is a comma or parenthesis, then have the program copy every element that follows into another string until the next comma or parenthesis is reached. Then, to convert the string to a double, use a stringstream object. That's one way I can think of. And as for the operator, have the program check to see if an element is a plus sign or minus sign or whatever operators you want, and if it finds that operator then just perform the corresponding operation.

EDIT: or you can scan the string for parentheses and commas and replace any occurrences with spaces. This would probably be a lot easier.
Last edited on
or

1
2
3
4
double x1, y1, x2, y2;
char junk, char operator;

stream >> junk >> x1 >> junk >> y1 >> junk >> operator >> junk >> x2 >> junk >> y2 >> junk;


cin and cout are streams by the way.

edit: and I guess you don't want a variable named operator :)
Last edited on
Yeah, that's probably the best way. I first got rid of the junk. And I assumed the stream was a stringstream but now that I think of it it's probably cin; I overcomplicated this :-)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <sstream>
#include <limits>

int main()
{
    std::stringstream ss;
    unsigned int i;
    double x1,y1,x2,y2;
    char op;
    std::string s;
    ss<<"(3.0,4.7) + (5.3,6)";
    getline(ss,s);
    ss.clear();
    for(i=0;i<s.size();i++)
    {
        if(s[i]=='('||s[i]==','||s[i]==')')
            s.replace(s.begin()+i,s.begin()+i+1," ");
    }
    ss<<s;
    ss>>x1>>y1>>op>>x2>>y2;
    std::cout<<"x1="<<x1<<std::endl;
    std::cout<<"y1="<<y1<<std::endl;
    std::cout<<"op="<<op<<std::endl;
    std::cout<<"x2="<<x2<<std::endl;
    std::cout<<"y2="<<y2<<std::endl;
    std::cout<<std::endl<<"Press ENTER to continue...";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    return 0;
}


output:

x1=3
y1=4.7
op=+
x2=5.3
y2=6

Press ENTER to continue...
Last edited on
I only mentioned cin and cout to say "hey maybe you have worked with streams.".

@CJC0117: Your way is much more flexible.
Topic archived. No new replies allowed.