Functions using Struct?

Hello guys, im quite desperate trying to bring this to work.

I have to write this function which transfers the numbers from karthesian to polar-Coordinates as homework for university with a function, and using structs for the coordinates. The calculations should be working, and in my opinion, my output should look something like this.. " cout << kart_in_polar(input.x, input.y);"

Also, I first declared my function as "float kart_in_polar(const float* numberx...", but then my complier started complaining in the "calculation-Part" and because of the const. AND im not sure if this is possible.

I would appreciate it if someone could tell me where i went wrong, and provide a solution for my issue.


(compiler error on bottom)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <cstdlib>
#include <iostream>
#include <math.h>


using namespace std;

struct kart{
   float x;
   float y;
};

struct polar{
   float phi;
   float r;
};
int i;

polar kart_in_polar(float numberx,float numbery);

int main(int argc, char *argv[])
{
    kart input;
    cout << "Please insert karthesian Coordinates:\n\nx: ";
    cin >> input.x;
    cout << "\ny: ";
    cin >> input.y;
    
    cout << kart_in_polar(input.x, input.y); // Why doesnt it work?
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


polar kart_in_polar(float numberx,float numbery)
{
      polar calculation;
      calculation.r = sqrt((numberx * numberx) + (numbery * numbery));
      calculation.phi = atan(numbery/numberx);
      return calculation;

}


Compiler Error says:

29 no match for 'operator<<' in 'std::cout << kart_in_polar(input.kart::x, input.kart::y)'
note C:\Dev-Cpp\include\c++\3.4.2\bits\ostream.tcc:63 candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]



Im just about starting with C++ and have the whole university to go, but my Professor didnt explain this well (or I didnt get it), and I hardly can find solutuions for "structures in fuctions". I hope this communtiy can help, because i dont think its going to be my last topic and one day I can help others when the knowledge is there.
Last edited on
cout << kart_in_polar(input.x, input.y);

You are saying...
cout << function@line37

function@ line 37 returns a polar structure , if you want to display the polar structures variables you must first create an instance of the struct

1
2
3
4
5
6
//somewhere in main
polar result;
//later on after input is had
result = kart_in_polar(input.x, input.y) //kart_in_polar returns a polar struct and result is a polar struct, so lets assign result the return value of our function
//displaying output
cout << result.phi << " " << result.r;
Last edited on
You are the master of all c++.

Now it works!

Thank you so much. I would kiss you if you were here! :)
Topic archived. No new replies allowed.