I want my user to be able to enter a fraction. I could be lazy and just take num and denom separate...

But I was wondering if there is way I can allow user to enter a fraction like "3/4". And somehow ignore the '/'. I guess I could take it as a string and parse it somehow, but is there a way just to simply ignore the '/'..treating it like whitespace or something.

How will treating the / as whitespace help you? I assume that you want to convert the user's input (e.g. 3/4) to a float (0.75) in your code?

So you'll have to split the input string on the '/' character (and do something sensible if the '/' is not found), convert both halves to some numerical type, and then 'calculate' the fraction.

Well, I was thinking white space because I could do something like this.

cin << num << denom;

And no, the num and denom are being fed into a fraction object, not being turned into a float, sorry should have mentioned.

But I guess that answers my question..in that I will have to use a string. Was just looking for an easy way, since I've never really messed with strings in c++. Guess I've got some reading to do. Thanks for post

Ok, then there actually is an easier way. You can overload operator>> in your fraction class.

e.g.

struct fraction
{
	int num, denom;
};
istream &operator >> (istream &in, fraction &f)
{
	in >> f.num;
	in.ignore(1,'/');
	in >> f.denom;
	return in;
}

Hope that helps

Oh, wow thanks a bunch. Looks real interesting. I'm gonna try that out tonight.

int numerator, denominator ;
    char slash ;

    // std::cin >> std::skipws // this is the default
    if( ( std::cin >> numerator >> slash >> denominator ) && ( slash == '/' ) )
    {
        // ok
    }
    else
    {
        // error in input
    }
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.