Yogesh Gaikwad Yogesh Gaikwad - 3 months ago 7
C++ Question

error: no match for ‘operator>>’

I was just trying an example on exception handling.
I am getting the error error: no match for ‘operator>>’
What is wrong in my program.
I am using linux gcc.

#include<iostream>
using namespace std;
void div(double, double);
void read (double *, double *);
main()
{
double a,b;
cout<<"enter the two nos";
cin>>a>>b;
read(&a,&b);
div(a,b);
}

void div(double a, double b)
{
double d;
try {
if(b==0)
throw "divide by zero condition occured";
d=a/b;
cout<<"divsion of two no is"<<d<<endl;
}catch(const char *e)
{
cout<<"exception is "<<e<<endl;
}
}
void read(double *a,double *b)
{
cout<<"Enter the 2 no.";
cin>>a>>b;
}


I also tried another function for read function

void read(double *a,double *b)
{
double c,d;
cout<<"Enter the 2 no.";
cin>>c>>d;
a=c;
b=d;
}


but it also has the error:


cannot convert ‘double’ to ‘double*’ in assignment

Answer
void read(double *a,double *b)
{
    double c,d;
    cout<<"Enter the 2 no.";
    cin>>c>>d;
    a=c;
    b=d;
}

This won't work, because you want to change the values pointed to by the pointers, not the pointer itself, result in the error. Fix it to this:

void read(double *a,double *b)
{
    double c,d;
    cout<<"Enter the 2 no.";
    cin>>c>>d;
    *a=c;
    *b=d;
}

Of course, in such a case, I wouldn't recommend using pointers, better to use references, so no dereferencing is required, and you can call it like read(a,b):

void read(double &a,double &b)
{
    double c,d;
    cout<<"Enter the 2 no.";
    cin>>c>>d;
    a=c;
    b=d;
}

The double& b notation means pass-by-reference. You also have the same problem for your other read function, fix it to this:

void read(double *a,double *b)
{
    cout<<"Enter the 2 no.";
    cin>>*a>>*b;
}

or better:

void read(double &a,double &b)
{
    cout<<"Enter the 2 no.";
    cin>>a>>b;
}