#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;
}
3 Replies - 384 Views - Last Post: 02 October 2012 - 02:07 PM
#1
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!
Replies To: Passing istream and ostream as formal parameters in member functions
#2
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?
#3
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
#4
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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|