3 Replies - 1051 Views - Last Post: 02 October 2012 - 02:07 PM Rate Topic: -----

#1 bennigan88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-April 12

Passing istream and ostream as formal parameters in member functions

Posted 02 October 2012 - 12:57 PM

I'm trying to write a program that uses a class called Fraction and takes in values using a member function and outputs values using another member function. The thing is, I want to be able to take in from any stream and output to any other stream, cin and cout being special cases that I'm using for this particular program. I am having problems passing streams as formal parameters; I'm getting an error message and a reference to a header file that I don't understand... any help would be greatly appreciated!

#include <iostream>
#include <istream>
#include <cstdlib>

using namespace std;

class Fraction
{
    public:
        //constructors
        Fraction();
        Fraction(int i);
        Fraction(int num, int denom);

        //input
        void Get(istream& in);

        //output
        void Show(ostream);

    private:
        int numerator;
        int denominator;

};

int main()
{
    Fraction A;
    A.Get(cin);
    A.Show(cout);
    
    return 0;
}

//input
void Fraction::Get(istream& in)
{
    char* hold=new char[20];

    in.getline(hold,20,'/');
    numerator=atoi(hold);
    in.getline(hold,20,'\n');
    denominator=atoi(hold);
    in.ignore();
}

//output
void Fraction::Show(ostream& out)
{
    out << numerator << "/" << denominator;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Passing istream and ostream as formal parameters in member functions

#2 bennigan88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-April 12

Re: Passing istream and ostream as formal parameters in member functions

Posted 02 October 2012 - 01:04 PM

Ah I see that I messed up the function declaration in the class definition. I fixed it. Now I need to adjust the getline() issue..... when I put 3/4 into the console, it reads that and then waits for another input, when what is supposed to happen is it reads numbers up until the '/', stores that into an integer, then resumes reading the rest of the numbers after the '/' and stores those into another integer. why isn't that part working?
Was This Post Helpful? 0
  • +
  • -

#3 bennigan88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-April 12

Re: Passing istream and ostream as formal parameters in member functions

Posted 02 October 2012 - 02:01 PM

how do i delete a post??

This post has been edited by bennigan88: 02 October 2012 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6032
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: Passing istream and ostream as formal parameters in member functions

Posted 02 October 2012 - 02:07 PM

You don't delete posts. We don't delete posts as a rule.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1