structures not working

Hey fellas, I was learning about structures. Obviously, its not working.
I just made a simple program which prints out the area of a rectangle (using a function, which has the structure as its parameters.)

I am getting tons of error, in this short program.
Please help me out.

Thanks in advance :D

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
 #include<iostream>
using namespace std;



struct distance{


int length;
float breadth;
int area;

} ;

int main(){
    distance l, l2;
void practice(distance &a, distance &b);

cout<<"please enter the length\n\n ";


cin>>l.length;
cout<<"please enter the breadth....\n";

cin>>l2.breadth;

practice(l,l2);

return 0;
}

void practice(distance &a, distance &b){

    distance d3;
    d3.area=a.length*b.breadth;
    cout<<"therefore your area is "<<d3.area;
}
Function prototype not placed correctly, needs to be outside of main before main. You are making two box objects and not sure why one object has a height and a breadth just manipulate those.

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
#include<iostream>

using namespace std;

struct Distance{

float length, breadth, area;

} ;

void practice(Distance&);//prototype

int main(){
    
    Distance box;//Create object

    cout<<"please enter the length\n\n ";
    cin>>box.length;
    
    cout<<"please enter the breadth....\n";
    cin>>box.breadth;
    
    practice(box);//Pass obj to function

    return 0;
}

void practice(Distance& obj){

    obj.area = obj.length * obj.breadth;
    cout << "Your area is: " << obj.area;

}
Last edited on
using namespace std; - this exposes all names in the std namespace to your program.

There is already an std::distance, so this clashes with your use of distance as a type name.

You can choose a different name like "Distance", or get rid of the using directive, and either explicitly qualify all std objects like std::cout<<"therefore your area is "<<d3.area;, or use using declarations like using std::cout; // now you can use cout without fully qualifying it , and so on.

Also, your function declaration (line 17) is in the wrong place. You can't declare a function within a function (AFAIK, I'm not up to speed with C++11). Maybe move the declaration to the line before main().
Topic archived. No new replies allowed.