Struct variable.

Hey guys , why can't I use the tried struct in " k " function ? I declared the struct as a global variable and I still can't use it , why ?

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
45
46
  #include <iostream>
using namespace std;
struct tried {

	char x[20];
	int age;
	double salary;

};

int k(struct tried){

	tried o= {


		"Hope",
		19,
		999.9


	};


int main()
{

	tried y = {


		"Hope",
		19,
		999.9


	};

	cout << y.salary;




	cin.get();
	cin.get();
	return 0;
}
instead of int k try void k since you are not returning any integer from the function.

Try this code:
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
#include <iostream>

struct tried {
    char x[20];
    int age;
    double salary;
};

void k(struct tried) {
    tried o = {
        "Hope",
        19,
        999.9
    };
}

int main() {
    tried y = {
        "Hope",
        19,
        999.9
    };
    std::cout << y.salary;
    std::cin.get();
    std::cin.ignore();
    return 0;
}
Thanks buddy , but I could use int and return an int like " age " , right ?

also why wouldn't work and gives me this error " binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) " when I change the array from char[] to string ? even though I declared using namespace std; before the struct. ?
If you return an int such as age then you can use int k.

I think you get that error when you change to string because you didn't add #include <string> at the beginning of the code after #include <iostream> . Try adding it and then tell me what happens.
Last edited on
Oh boy , stupid me , I forgot it ^^

thank you so much.
Topic archived. No new replies allowed.