Need help with Structs

Hello,

I have to write structs with some functions, and I am not sure if did them correctly!


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
47
48
49
50
51
52
53
54
55
  struct ABC	{
	string s;
	unsigned n;
			};

bool same(const ABC & a, const ABC & b);
//same's job is to return whether the 2 args agree in at least one field;
//in other words, return true if the s fields are the same or if the n fields are //the same(or both)


void set(ABC & abc, const string & sVal, unsigned nVal);
//set's job is to set abc's s field to the second arg, and its n field to the third //arg.

void show(const ABC abc[], unsigned els, unsigned threshold);
//show is only interested in ABC's whose n is >= threshold.
//For those ABC's, it outputs both fields, one struct per line.



int main()
{
	ABC abc = { "Tom",1 };
	ABC abc2 = { "Jack", 1 };
	ABC abc3[] = { "Sam", 3 };
	cout << "True or False: " << same(abc,abc2) << endl;
	set(abc,"Jack",2);
	show(abc3, 1, 1);
}

bool same(const ABC & a, const ABC & b)
{
	
	if (a.s == b.s)
		return 1;
	if (a.n == b.n)
		return 1;
	if (a.s == b.s && a.n == b.n)
		return 1;
	
	return 0;
}

void set(ABC & abc, const string & sVal, unsigned nVal)
{
	abc.s = sVal;
	abc.n = nVal;
	cout << "abc's s field: " << abc.s << ", abc's n field: " << abc.n << endl;
}

void show(const ABC abc[], unsigned els, unsigned threshold)
{
	for (unsigned i = 0; i < els; i++)
		if (abc[i].n >= threshold)
		cout << "Field 1: " <<abc[i].s <<endl<< "Field 2: " << abc[i].n << endl;
}
Last edited on
So what is the problem?
Can we see the errors?
Sorry for replying late to you, actually there's no error but I was suspecting that I did something wrong

I am unsure if I did same function properly
It is fine as written, if redundant. There is no way line 37 can be reached if line 33 or line 35 evaluate to true. One should prefer to use true and false instead of 1 and 0.

It could be rewritten as:

1
2
3
4
bool same(const ABC& a, const ABC& b)
{
    return a.s == b.s || a.n == b.n ;
}
Last edited on
Thank you,

I having difficulty with writing the showRichPeople

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
47
48
49
50
51
52
53
54
55
56
57
58
class DEF{
public:
	DEF(const string & name, double salary);
	string getName() const;
	double getSalary() const;
	DEF & setName(const string & name);
	DEF & setSalary(double salary);
	const DEF & show() const;
private:
	string myName;
	double mySalary;
};

void showRichPeople(const DEF staff[], unsigned els);

int main()
{
	DEF group[] = { DEF("Mike", 2000), DEF("Soop", 1100000), DEF("Tom", 111110), DEF("Sam", 100) };
	showRichPeople(group,3);
}

DEF::DEF(const string & name, double salary) :myName(name), mySalary(salary){}

string DEF::getName() const
{
	return myName;
}

double DEF::getSalary() const
{
	return mySalary;
}

DEF & DEF::setName(const string & name)
{
	myName = name;
	return *this;
}

DEF & DEF::setSalary(double salary)
{
	mySalary = salary;
	return *this;
}

const DEF & DEF::show() const
{
	cout << myName << endl;
	return *this;
}

void showRichPeople(const DEF staff[], unsigned els)
{
	for (unsigned i = 0; i < els; i++)
		if (staff[i].getSalary >= 1000000)
			a= staff[i].show();
	
}
Call the function:

staff[i].getSalaray()

Note the parentheses.
showRichPeople's job is to output the names of all the class objects in the array whose salary is at least one million, one name per line.

1
2
3
4
5
6
void showRichPeople(const DEF staff[], unsigned els)
{
	for (unsigned i = 0; i < els; i++)
		if (staff[i].getSalary() >= 1000000)
			staff[i].show();
}


can I cout it without calling the show function ?
Last edited on
You could use cout << staff[i].getName() << '\n';, but if show's job is to display the name, why wouldn't you want to use it?
Thanks cire for being so helpful, I don't want to use the show function because I can simply save some lines
Topic archived. No new replies allowed.