Thread View: gnu.g++.help
3 messages
3 total messages
Started by Arthur Cinader
Mon, 15 Jan 1996 20:32
Operator Overloading - Result not Expected!
Author: Arthur Cinader
Date: Mon, 15 Jan 1996 20:32
Date: Mon, 15 Jan 1996 20:32
101 lines
3062 bytes
3062 bytes
I am teaching myself to program with "How To Program C++" by
Deitel & Deitel. A great book. In chapter eight, there is a
an example in the text on operator overloading. When I code
the example, exactly as it appears in the book, I don't get
the output that they show...
Using g++ v2.4 on BSDI I compile with:
g++ -o fig8.3 fig8.3.C
here is the source:
********begin source
// FIG8_3.CPP
// Overloading the stream-insertion and
// stream-extraction operators.
#include <iostream.h>
class PhoneNumber {
friend ostream &operator<<(ostream &, const PhoneNumber &);
friend istream &operator>>(istream &, PhoneNumber &);
private:
char areaCode[4]; // 3-digit area code and null
char exchange[4]; // 3-digit exchange and null
char line[5]; // 4-digit line and null
};
// Overloaded stream insertion operator (cannot be
// a member function).
ostream &operator<<(ostream &output, const PhoneNumber &num)
{
output << "(" << num.areaCode << ") "
<< num.exchange << "-" << num.line;
return output; // enables cout << a << b << c;
}
// overloaded stream extraction operator
istream &operator>>(istream &input, PhoneNumber &num)
{
input.ignore(); // skip (
input.getline(num.areaCode, 4); // input area code
input.ignore(2); // skip ) and space
input.getline(num.exchange, 4); // input exchange
input.ignore(); // skip dash (-)
input.getline(num.line, 5); // input line
return input; // enables cin >> a >> b >> c;
}
main()
{
PhoneNumber phone; // create object phone
cout << "Enter a phone number in the "
<< "form (123) 456-7890:" << endl;
// cin >> phone invokes operator>> function by
// issuing the call operator>>(cin, phone).
cin >> phone;
// cout << phone invokes operator<< function by
// issuing the call operator<<(cout, phone).
cout << "The phone number entered was:" << endl
<< phone << endl;
return 0;
}
*******end source
Here is the execution:
% ./fig8.3
Enter a phone number in the form (123) 456-7890:
(800) 555-1212
The phone number entered was:
(800) -
%
The output should be the full number.
I tested the value of the PhoneNumber object with
printExchange() and printLine() functions. Both print
funtions printed nothing leading me to conclude that the
culprit is the function:
friend istream &operator>>(istream &, PhoneNumber &);
I am stumped! Is there a switch I need to throw to make
operator overloading work with g++? A search of the man pages
has turned up nothing.
Thnaks for any light you can shed!
Best Regards,
Arthur
acinader@panix.com
Re: Operator Overloading - Result not Expected!
Author: UCLA Bruin
Date: Tue, 16 Jan 1996 05:57
Date: Tue, 16 Jan 1996 05:57
47 lines
2359 bytes
2359 bytes
acinader@panix.com (Arthur Cinader) wrote:
>I am teaching myself to program with "How To Program C++" by
>Deitel & Deitel. A great book.
Maybe... But if their book is so great, why didn't their code work?
=) I recommend The C++ Primer Plus by Prata, but anyways... I can
explain the problem in their code.
>Using g++ v2.4 on BSDI I compile with:
>g++ -o fig8.3 fig8.3.C
You can just type g++ fig8.3.cpp
>// overloaded stream extraction operator
>istream &operator>>(istream &input, PhoneNumber &num)
>{
> input.ignore(); // skip (
> input.getline(num.areaCode, 4); // input area code
> input.ignore(2); // skip ) and space
> input.getline(num.exchange, 4); // input exchange
> input.ignore(); // skip dash (-)
> input.getline(num.line, 5); // input line
The problem lies here. cin.getline is supposed to be used for reading
all the characters on a line (spaces included) up until the newline
and then ignoring the newline character. As a sefety measure to
ensure you don't fill your string beyond it's bounds, you can pass an
optional integer to cin.getline to tell it when to stop the input.
With g++, cin.getline skips over the remaining characters on the line
until it reads in and ignores the newline. However, with Borland C++,
it leaves the remaining characters and newline intact. In other
words, the author probably was using Borland C++ and failed to test
his program with g++ for UNIX. The two versions have some
differences.. =) So basically with your C++ compiler it read the
first four characters and then ignored the rest of the input, hence
the result you were getting...
But all is not lost... If you use cin.get instead of cin.getline this
code will work perfectly under both compilers. The author of your
book used the wrong method for the wrong purpose.
Hope this help!
Nate
Re: Operator Overloading - Result not Expected!
Author: Oliver Kurth
Date: Tue, 16 Jan 1996 10:04
Date: Tue, 16 Jan 1996 10:04
120 lines
4159 bytes
4159 bytes
Arthur Cinader wrote:
>
> I am teaching myself to program with "How To Program C++" by
> Deitel & Deitel. A great book. In chapter eight, there is a
> an example in the text on operator overloading. When I code
> the example, exactly as it appears in the book, I don't get
> the output that they show...
>
> Using g++ v2.4 on BSDI I compile with:
>
> g++ -o fig8.3 fig8.3.C
>
> here is the source:
>
> ********begin source
> // FIG8_3.CPP
> // Overloading the stream-insertion and
> // stream-extraction operators.
> #include <iostream.h>
>
> class PhoneNumber {
> friend ostream &operator<<(ostream &, const PhoneNumber &);
> friend istream &operator>>(istream &, PhoneNumber &);
> private:
> char areaCode[4]; // 3-digit area code and null
> char exchange[4]; // 3-digit exchange and null
> char line[5]; // 4-digit line and null
> };
>
> // Overloaded stream insertion operator (cannot be
> // a member function).
> ostream &operator<<(ostream &output, const PhoneNumber &num)
> {
> output << "(" << num.areaCode << ") "
> << num.exchange << "-" << num.line;
>
> return output; // enables cout << a << b << c;
> }
>
> // overloaded stream extraction operator
> istream &operator>>(istream &input, PhoneNumber &num)
> {
> input.ignore(); // skip (
> input.getline(num.areaCode, 4); // input area code
> input.ignore(2); // skip ) and space
> input.getline(num.exchange, 4); // input exchange
> input.ignore(); // skip dash (-)
> input.getline(num.line, 5); // input line
>
> return input; // enables cin >> a >> b >> c;
> }
>
> main()
> {
> PhoneNumber phone; // create object phone
>
> cout << "Enter a phone number in the "
> << "form (123) 456-7890:" << endl;
>
> // cin >> phone invokes operator>> function by
> // issuing the call operator>>(cin, phone).
> cin >> phone;
>
> // cout << phone invokes operator<< function by
> // issuing the call operator<<(cout, phone).
> cout << "The phone number entered was:" << endl
> << phone << endl;
> return 0;
> }
>
> *******end source
>
> Here is the execution:
>
> % ./fig8.3
> Enter a phone number in the form (123) 456-7890:
> (800) 555-1212
> The phone number entered was:
> (800) -
> %
>
> The output should be the full number.
>
> I tested the value of the PhoneNumber object with
> printExchange() and printLine() functions. Both print
> funtions printed nothing leading me to conclude that the
> culprit is the function:
>
> friend istream &operator>>(istream &, PhoneNumber &);
>
> I am stumped! Is there a switch I need to throw to make
> operator overloading work with g++? A search of the man pages
> has turned up nothing.
>
> Thnaks for any light you can shed!
>
> Best Regards,
>
> Arthur
> acinader@panix.com
Try out this for your input function:
// overloaded stream extraction operator
istream &operator>>(istream &input, PhoneNumber &num)
{
input.ignore(); // skip (
input.get(num.areaCode, 4, ')'); // input area code
input.ignore(2); // skip ) and space
input.get(num.exchange, 4, '-'); // input exchange
input.ignore(); // skip dash (-)
input.getline(num.line, 5); // input line
return input; // enables cin >> a >> b >> c;
}
The function getstring seems to read the whole line, so it was 'eaten'
when you read your areaCode.
Oliver Kurth
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads