Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to overload the >> operator so that when the user inputs a fraction, cin would store the fraction into an Object of type Fraction.
Excerpt from header file:
// Calculator.h
friend istream &operator>>( istream &, Fraction &);
private:
signed int numo; // fraction numerator
signed int deno; // fraction denomenator
char ch; // the '/' character
Excerpt from Implementation file:
//Calculator.cpp
// insertion operator overloading
istream &operator>>( istream &input, Fraction fraction)
{
input >> fraction.numo;
input >> fraction.ch;
input >>fraction.deno;
return input;
}
When I try to compile Calculator.cpp and compile a driver function inside another file, I run into many errors. Why is this not working? Please explain your answer thoroughly, I would like to learn.
UPDATE:
**Errors: variables, numo deno and ch are 'private'
You have a simple mismatch. The function that's declared as a friend takes a reference to a Fraction as its second parameter. The function you've implemented takes a fraction by value instead, so it's not a friend of the Fraction class. You want the version that takes a reference since when you use the function you normally want to pass a variable, and you want it to modify that variable.
I usually prefer to implement such things in place:
class Fraction {
friend std::istream &operator>>(std::istream &is, Fraction &f) {
return is >> f.numo >> f.ch >> f.deno;
}
// ...
};
Even though its body is inside the class definition, this is still a non-member function simply by virtue of the fact that it's declared as a friend.
Andy in Calculator.h header file, you are using Fraction& (ref type) as function second argument. So in function definition it must be same, (don't use value type).
Prototype:
friend istream& operator>>(istream&, Fraction );
Function definition:
istream& operator>>(istream& input, Fraction& fraction)
{
input >> fraction.numo;
input >> fraction.ch;
input >>fraction.deno;
return input;
}
Related
istream& operator>>(istream& input, const complex& P) {
input >> P.real >> P.imaginary;
return input;
}
I have the code above and for some reason I get this error:
Invalid operands to binary expression ('istream' (aka 'basic_istream<char>') and 'double').
complex& P is an object which basically stores the real number part and imaginary number part of a complex number. So if you have 2 + 3i, P.real will return to you back 2 and P.imaginary will return to you back 3. And the real and imaginary numbers are both double.
Can someone help me figure this out?
When overloading the >> operator, the variable to which you want to assign the input values has to be mutable, so you need to remove the "const"
istream& operator>>(istream& input, complex& P) {
input >> P.real >> P.imaginary;
return input;
}
So I'm failing a simple input test for one of assignment questions. I'm to parse two integers separated by a white space. The two integers are used in the private members num and denom of the Rationalnumber type I have defined as a class. Is there something obviously unconventional I am using in or missing from this code? This code works for all my tests.
std::istream &operator>>( std::istream &is, Rationalnumber &r ) {
Rationalnumber::in++; // increment counter
// get the num
string n;
getline(cin,n,' ');
stringstream ssnum(n);
ssnum >> r.num;
// get the denom
string d;
getline(cin,d,'\n');
stringstream ssdenom(d);
ssdenom >> r.denom;
return is;
}
Your code fails in, at least, two obvious ways:
If a different whitespace than space is used, e.g., '\t', it isn't recognized by your code.
The input operator doesn't indicate failure when it is given invalid inputs, e.g., when the input is "a b" rather than numbers.
Conventionally, when input fails, the valuevread remains unchanged. This is also not true for your implementation. To fix things up the code can actually be simplified and made a lot fadter in the process. As this is clearly a homework assignment I don't think it is appropriate to provide the code, though.
This code passes the test!:
std::istream &operator>>( std::istream &is, Rationalnumber &r ) {
Rationalnumber::in++; // increment counter
int in;
is >> in;
r.numerator(in);
is >> in;
r.denominator(in);
return is;
}
I assigned myself some homework over the summer, and the project I am 98% finished with has come to a standstill due to this one problem.
I have a class called Mixed. It contains member data for a whole number, a numerator, and a denominator. I need to overload all of the common operators to allow multiplication, addition, comparison and streaming of objects of type Mixed. I have all the operators overloaded except for >> (the extraction operator).
All mixed numbers read in will be of format:
whole numerator/denominator
ex: 1 2/3, 0 7/8, -3 18/5, 0 -1/89
Header: friend istream& operator>> (istream &, Mixed);
CPP file: istream& operator>> (istream &in, Mixed m) {...}
For the assignment, I am limited to the iostream and iomanip libraries. My plan was to read in the values from the stream and assign them to temporary int variables (w, n, d) which I would then use with the Mixed constructor to create object m. Unfortunately, I cannot think of a way to separate the numerator and denominator. They are both ints, but they have a char (/) between them.
I cannot use getline() with its delimiter, because it assigns data to a char array, which I do not believe I can convert to an int without another library.
I cannot use a char array and then segment it for the same reason.
I cannot use a while loop with get() and peek() because, again, I do not think I will be able to convert a char array into an int.
I cannot use a string or c-string and then segment it because that requires external libraries.
Once again, I need to split a value like "22/34" into 22 and 34, using only iostream and iomanip. Is there some fairly obvious method I am overlooking? Is there a way to implicitly convert using pointers?
You could first extract the nominator, then the separating character, and then the denominator.
Example for illustration:
istream& operator>> (istream &in, Mixed &m) {
int num, denom;
char separ;
in >> num;
in.get(separ);
if (separ != '/')
in.setstate(ios::failbit);
in >> denom;
if (in) {
// All extraction worked
m.numerator = num;
m.denominator = denom;
}
return in;
}
Once again, I need to split a value like "22/34" into 22 and 34, using
only iostream and iomanip.
Couldn't you just read in the first integer, use get to get the next character, and then read the second integer? Something like this:
#include <iostream>
int main() {
using std::cin;
using std::cout;
int num;
int den;
while(cin) {
cin >> num;
if (cin.get() != '/') {
// handle error
}
cin >> den;
cout << num << "/" << den << std::endl;
}
return 0;
}
You can then make sure that the character read between the two integers was a '/' and handle appropriately if it isn't.
I'm working on a school project to implement a c++ class for polynomials. Something my class is supposed to do is read polynomials from standard in, or from a file. I thought about overloading >> until I read the following on my favorite c++ reference site:
Notice that the istream extraction operations use whitespaces as
separators, therefore this operation will only extract what can be
considered a word from the stream. To extract entire lines of text,
refer to the string overload of global function getline.
This got me all inspired to overload the global function getline for my polynomial class so that it can read whole lines from a file. There are lots of tutorials and articles describing how to overload the stream extraction operator, but I couldn't find any details about getline. Should I just overload it however I want? From the reference this appears to be how it's done.
In some of the overloaded getline functions I've seen (such as at the bottom of the page linked to), I noticed they return something like "basic_istream". Is it enough that I just return istream? What about for "char_type"? Would char suffice?
Basically I want to know: is this one of those anything goes overloads, or is there some finicky detail I should be worried about?
This is the header I've cooked up:
class Polynomial {
public:
friend istream& getline(istream& is, Polynomial & poly);
friend istream& getline(istream& is, Polynomial & poly, char delim);
};
friend istream& getline(istream& is, Polynomial & poly) {
return getline(is, poly, '\n');
}
friend istream& getline(istream& is, Polynomial & poly, char delim) {
// read enough tokens to make a term
// stop when we get to the delimiter
return is;
}
Thanks!
You should still overload operator >>. Within your operator implementation, you can extract as many 'words' as you need (I'm assuming one per coefficient or so). Don't try to overload getline, thats about getting a line not a Polynomial.
I have the following block of code trying to overload the >> operator:
istream &operator >> (istream &stream, const Currency &obj){
cout << "Dollars: ";
stream>>obj.dollars;
cout<< "Cents: ";
stream>> obj.cents;
return stream;
}
But when I call cin>>newMoney from my main program it keeps repeating "DOLLARS:" over and over again in an endless loop like below:
Any ideas why?
I bet you have a Currency constructor that takes an 'int'. So stream>>obj.dollars; is the same as stream>>Currency(obj.dollars);. The function you pasted outputs "Dollars: " and then calls itself, outputting "Dollars: " again and then repeating forever.
I'd suggest making the constructor explicit so it doesn't misfire on you.
There is no enough information in your code (as of now). Till you post more code, all I can say is this, which is also important for you to note down: since you overload >> to take input, the object obj should be passed by non-const reference, so remove const from the second parameter, and make it look like this:
istream &operator >> (istream &stream, Currency &obj)