Why it is Giving garbage value | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Why it is Giving garbage value

adding complex number https://code.sololearn.com/cmrkkHlau944/?ref=app

8th Jun 2017, 6:26 AM
Bahubali
Bahubali - avatar
5 Answers
+ 12
Your methods have return type double but returns nothing. Your methods contain cout instructions but your main function calls the methods within another cout statement, causing errors. This is what you should do: #include <iostream> using namespace std; class complex{ public: double addreal(double x,double y) {return (x+y);} double addimg(double p,double q) {return (p+q);} }; int main() { complex obj; cout<<obj.addreal(5,2)<<"+"<<"i"<<obj.addimg(5,3); return 0; }
8th Jun 2017, 6:50 AM
Hatsy Rei
Hatsy Rei - avatar
+ 12
Your function is of type double. Your function must have return value. Your function has no return value. Your function only has cout. Your function must return value, not cout. So, remove cout and replace with return statement.
8th Jun 2017, 7:05 AM
Hatsy Rei
Hatsy Rei - avatar
+ 12
If you use void, you don't need to cout. In main, do: obj.addreal(5,2); cout << "+i"; obj.addimg(5,3); // if return type is void.
8th Jun 2017, 7:07 AM
Hatsy Rei
Hatsy Rei - avatar
+ 4
thanks@hatsy rei
8th Jun 2017, 7:12 AM
Bahubali
Bahubali - avatar
+ 2
if I use void instead of double error is coming in cout statement in main function
8th Jun 2017, 7:03 AM
Bahubali
Bahubali - avatar