1

Possible Duplicate:
overloading >> for a fraction class C++

So I need to read in a line from the command line.

Prints out instructions, and then prompts for input. Looks as below.

Type your first fraction: 8/27

Now the point is we're supposed to overload istream >> to be able throw the 8 into a fraction class as the numerator and 27 as the denominator, but I can't for the life of me figure out how. I've tried .getline and trying to convert from char to int, I've got everything working inputting the 8 THEN the 27, but can't get '8/27' as an input to work. Any advice as to where I should be heading?

Community
  • 1
  • 1
user1900308
  • 11
  • 1
  • 2
  • 3
    Show us the code that you have tried till far . – The Dark Knight Dec 13 '12 at 08:44
  • 1
    overload >>, and in the overloaded >> do in >> num >> c >> denum where num & denum are ints, and c is a char – coyotte508 Dec 13 '12 at 08:45
  • What exactly have you tried so far? Please show your code. Did you overload `>>` to take your `Fraction` class as a parameter? If so, then I would probably have it use `>>(int)`, `>>(char)`, and `>>(int)` to read the individual values separately instead of as a single line. – Remy Lebeau Dec 13 '12 at 08:46
  • The input consists of an integer, then a character ("/"), and then another integer. Do you know how to read each of these things? – j_random_hacker Dec 13 '12 at 08:52

3 Answers3

2

You can take any data from the std::istream and put it into any type as long as it can be converted.

You want to read an integer, ignore a char, then read another integer.

For the one you want to ignore, read it into a dummy variable.

Here's some test code for you to fill in the gaps:

#include <sstream>
#include <iostream>

class Fraction {};

using std::istream;

istream& operator>>(istream& is, Fraction& fraction) {
    int numerator;
    int denominator;
    char dummy;

    is >> numerator;
    is >> dummy;
    is >> denominator;
}

using std::iostringstream;
using std::cout;
using std::endl;

int main() {
    iostringstream input;
    input << "8/27";

    Fraction fraction;
    input >> fraction;

    return 0;
}
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
1

Something like this would do it...

struct Fraction
{
    int numerator;
    int denominator;
};
std::istream & operator >> ( std::istream &s, Fraction & f ) {
    s>>f.numerator;
    char c;
    s>>c;
    s>>f.denominator;
    return s;
}


int main()
{
    std::stringstream s( "1/3 3/4");
    Fraction f1, f2;
    s>>f1;
    s>>f2;

    return 0;
}
Russ Freeman
  • 1,480
  • 1
  • 8
  • 6
1

Just to force-fit std::regex class here :).

int main() {
#ifdef APPROACH_1
    int num, den;
    char c;
    std::cin >> num >> c >> den;
    std::cout << static_cast<double>(num)/den;

#else
    std::regex regex("( *)([0-9]+)( *)/( *)([0-9]+) *");
    std::string inputstr;
    std::smatch match;

    std::cout << "Enter string";
    std::getline(std::cin, inputstr);

    bool matched = std::regex_search(inputstr, match, regex);

    if(matched)
    {
        std::string nums = std::string(match[2].first, match[2].second);
        std::string dens = std::string(match[5].first, match[5].second);

        std::cout << nums << " " << dens << std::endl;

        std::stringstream ss1(nums), ss2(dens);
        int num, den;

        ss1 >> num;
        ss2 >> den;

        std::cout << static_cast<double>(num)/den;
    }
    else
        std::cout << "Oops" << std::endl;
#endif

}
Chubsdad
  • 24,777
  • 4
  • 73
  • 129