•
•
•
•
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
![]() |
•
•
Join Date: Aug 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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.
my format of input is (7+i3). This is a complex number format. Hope to hear the reply soon.Thanks
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
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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:
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:
or
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.
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:
012347+i3Hence, 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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Overloading operator= (C++)
- problem with operator overloading (C++)
- overloading operator [] (C++)
- overloading Operator << to accept endl (C)
- need help with simple overloading operator+ for adding two object data. (C++)
Other Threads in the C++ Forum
- Previous Thread: segmentation fault
- Next Thread: Candidate Program



Linear Mode