Please can someone tell me where is the mistake in my program. It is not showing the desired output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please can someone tell me where is the mistake in my program. It is not showing the desired output

The below program is to accept and display two complex numbers. I have done it using structures as i just started learning structures. Problem is it isnt working; Please can someone tell me where I went wrong? Thanks for stopping by So here is the link https://code.sololearn.com/cutQQrFwIIav/#cpp

15th Dec 2017, 6:17 AM
RR2001
RR2001 - avatar
8 Answers
+ 4
void accept(complex_number&,complex_number&); void display(complex_number,complex_number); int main() {//runner} void accept(complex_number &c1,complex_number &c2) {//function}
15th Dec 2017, 7:11 AM
Echo One
Echo One - avatar
+ 4
He meant to change : void accept(complex_number c1,complex_number c2) with : void accept(complex_number& c1,complex_number& c2)
15th Dec 2017, 6:34 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
Im guessing you forgot to change the prototype.
15th Dec 2017, 7:12 AM
Echo One
Echo One - avatar
+ 2
@Echo One Please can you share your edited program
15th Dec 2017, 6:29 AM
RR2001
RR2001 - avatar
+ 1
They are passed by value. Pass them by reference in the first function. Checked. It worked.
15th Dec 2017, 6:24 AM
Echo One
Echo One - avatar
+ 1
@Baptiste E.Prunier I understood by reference he referred to &. Thing is I tried doing it but it didnt work. Thats why
15th Dec 2017, 6:40 AM
RR2001
RR2001 - avatar
+ 1
@Echo One Thanks I got it. This is what i wanted to do: #include<iostream> using namespace std; struct complex_number { double rn; double in ; }; void initcomplex_number(complex_number &c) { c.rn = 0; c.in =0; } void accept(complex_number &c) { cout<<"Enter real part"<<" ";cin>>c.rn; cout<<"Enter imaginary part"<<" ";cin>>c.in; } void add(complex_number c1,complex_number c2) { double R =0; double I=0; R= c1.rn + c2.rn; I= c1.in + c2.in; cout<<"The complex number is"<<R<<" "<<"+"<<I<<"i"<<endl; } int main() { complex_number c1,c2; initcomplex_number(c1); initcomplex_number(c2); accept(c1); accept(c2); add(c1,c2); return 0; }
15th Dec 2017, 7:41 AM
RR2001
RR2001 - avatar
+ 1
I can see you trying this again when you learn classes. Your init function is basically the constructor
15th Dec 2017, 7:43 AM
Echo One
Echo One - avatar