User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,607 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,492 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1883 | Replies: 4
Reply
Join Date: Aug 2007
Posts: 6
Reputation: wjyeo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wjyeo wjyeo is offline Offline
Newbie Poster

Help overloading operator >>

  #1  
Nov 8th, 2007
Hi i've have this problem in my code when i do the overloading of operator >>. My problem for this code isnt encounter error but my input like say i input 7, the num.a output will change this 7 to 55 and num.b output for 5 to 53. below is the code.

class Complex
{
	friend istream& operator>>(istream&, Complex&);
	friend ostream& operator<<(ostream&, const Complex&);
public:
	Complex(int a=0, int b=0) {};		// constructors
	Complex operator +(Complex);
	Complex operator *(Complex);
	bool operator ==(const Complex&) const;
	bool operator >(const Complex&) const;

private:
	int a, b;	
};

istream& operator>>(istream &in, Complex &num)
{
    string line="";
    fflush(stdin);
    in>>line;
    for(int i=0; i<line.length(); i++)
    {
            if(i==1)
            {
                 num.a = line[i];
            }
            else if(i==4)
            {
                 num.b = line[i];
            }     
    }         
	return in;
}

my format of input is (7+i3). This is a complex number format. Hope to hear the reply soon.Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: overloading operator >>

  #2  
Nov 8th, 2007
Step by step.

1. Don't fflush(). There is no need and you shouldn't be mixing C and C++ I/O anyway.

2. You are mis-indexing the string:
01234
7+i3
Hence, line[1] == '+' and line[4] == '\0'. BTW, i never reaches 4 if your input is only four characters long.

3. You are not converting your numbers. The ASCII value of '+' is 43.

4. Your constructor should look like this:
Complex( int _a=0, int _b=0 ): a( _a ), b( _b ) {};
or
Complex( int _a=0, int _b=0 ) {
a = _a;
b = _b;
}
The first way is preferred.
The reason num.b is some weird number is because your constructor never initializes the private fields a and b. Having arguments with the same name makes no difference.

PS. Complex numbers are normally written a + bi. If you do the same you will have an easier time collecting and verifying input.

Hope this helps.
Last edited by Duoas : Nov 8th, 2007 at 3:20 am.
Reply With Quote  
Join Date: Aug 2007
Posts: 6
Reputation: wjyeo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wjyeo wjyeo is offline Offline
Newbie Poster

Re: overloading operator >>

  #3  
Nov 8th, 2007
Thanks alot. actually my problem lies with the line[i] thing, like you have explain. It turn out to be taken as ASCii '7' instead of int '7' thats y it output to 55. I actually did my changes by adding line[i] - 48 to it to make it back to '7'. Really thanks alot abt the constructor thing.
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: overloading operator >>

  #4  
Nov 8th, 2007
> actually did my changes by adding line[i] - 48 to it to make it back to '7'.
line[i] - '0' would be much better; you program would then be not dependant on a particular character encoding.
Reply With Quote  
Join Date: Aug 2007
Posts: 6
Reputation: wjyeo is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
wjyeo wjyeo is offline Offline
Newbie Poster

Re: overloading operator >>

  #5  
Nov 8th, 2007
Thanks alot for this '0' didnt realise can do it this way.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:10 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC