+ 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
8 odpowiedzi
+ 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}
+ 4
He meant to change :
void accept(complex_number c1,complex_number c2)
with :
void accept(complex_number& c1,complex_number& c2)
+ 3
Im guessing you forgot to change the prototype.
+ 2
@Echo One Please can you share your edited program
+ 1
They are passed by value. Pass them by reference in the first function.
Checked. It worked.
+ 1
@Baptiste E.Prunier I understood by reference he referred to &. Thing is I tried doing it but it didnt work. Thats why
+ 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;
}
+ 1
I can see you trying this again when you learn classes. Your init function is basically the constructor