Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
OKay, I'm dont know how to fix the error where it says "mag cannot be used as a function" in the last line. AND if there are more errors please help me

What I have tried:

C++
#include<iostream>
#include<cmath>
using namespace std;

void polar_to_rect(double&, double&, double , double );

int main()                                           
{               
    char ch;                                         
    double mag, angle, x, y;
    
    
    cout<<"           This program converts  a polar vector to a rectangular vector.."<<endl<<endl;
    
    while (ch != 'n')
    {
          cout<<"Enter the value of mag: ";      
          cin>>mag;
          cout<<"Enter the value of angle: ";
          cin>>angle;
          
          
          polar_to_rect(mag,angle, x, y);
          cout<<"                            >>x = "<<x<<endl;
          cout<<"                            >>y= "<<y<<endl<<endl;
          cout<<"Another calculation? (y/n)... ";
          cin>>ch;
    }
    
    
    return 0;                                      
}  

void polar_to_rec(double _mag, double angle, double &x, double &y)
{     
      x = _mag(cos(angle));
      y = _mag(sin(angle));
      
      return;
}
Posted
Updated 21-Feb-16 10:06am
v3
Comments
Andreas Gieriet 21-Feb-16 15:33pm    
What don't you understand with the error message. It says it all!
_mag is a variable and not a function.
What do you intend to do with _mag?
Cheers
Andi

1 solution

C++ doesn't accept standard mathematical expressions (where the multiplication operator is omitted) you have to use valid C++ expressions

Quote:
x = _mag(cos(angle));
y = _mag(sin(angle));


should be
C++
x = _mag * cos(angle);
y = _mag * sin(angle);
 
Share this answer
 
v2
Comments
Member 12298677 21-Feb-16 17:28pm    
Thanks, but I got another error message "1d returned 1 exit status"
enhzflep 21-Feb-16 20:44pm    
You've stuffed up the forward declaration of polar_to_rect - you want the last two args to be passed by reference, not the first two. The function itself seems okay.

In such a trivial program, I'd be tempted to simply cut/paste the function so that it appears before main. No need for the forward-dec then. ;)
CPallini 22-Feb-16 3:33am    
Good catch, my virtual five.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900