Set_value functions in Structures

Hello,

I'm currently studying Object Oriented Programming in University, and not doing as well as I'd like to.

My question is regarding the "double set_value" function, I do not understand what "tmp" is, what it does, why it's needed and why it doesn't work in my code.

My teacher does not offer any consultations or help to students, I failed to find any explanation I was able to grasp on the internet.

Any links to lectures, articles tutorials about Structures and Classes in C++ would be appreciate as well. Thank you.

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
#include<iostream>
#include<iomanip>
#include<math.h>
#include<string>
#include<conio.h>
#include<stdlib.h>
using namespace std;
char *Subjects[3] = {"Math","Programming","Mechanics","Informatics"};
struct Candidat {
	string Name;
	string FkNumber;
	double Grades[3];
};

void Input(Candidat *c) {
	cout << "Name:" << endl;
	getline(cin, c->Name);
	cout << "FkNomer:" << endl;
	getline(cin, c->FkNumber);
	for (int i = 0; i < 3; i++) {
		cout << "Grade:";
		cin >> c->Grades[i];
	}
}

	void Output(Candidat c) {
		cout << "Name:/t" << c.Name << endl;
		cout << "FkNomer:/t" << c.FkNumber << endl;
		for (int i = 0; i < 3; i++) { cout << "Grade:/t" << c.Grades[i] << endl; }
	}

	double set_value(string Name, string FkNomer, double m0, double m1, double m2, double m3) {
		tmp.Name = Name;
		tmp.FkNomer = FkNomer;
		tmp.Grades[0] = m0;
		tmp.Grades[1] = m1;
		tmp.Grades[2] = m2;
		tmp.Grades[3] = m3;
		return tmp;
	}

	double AverageGrade(Candidat *c) {
	double Avg = 0;
	for (int i = 3; i < 3; i++) { Avg += c->Grades[i]; }
	Avg = Avg / 4;
	return Avg;
	}

	int main() {
		Candidat Ivan;
		//Input(Ivan);
		Output(Ivan);
		Candidat People[1];
		Candidat People[1] = set_value("Gosho", "172903", 4, 2, 5, 6);
		system("pause");
		return 0;
	}
I do not understand what "tmp" is.
Neither do I since it's not defined somewhere.

I guess it is a struct of type Candidate.
However trying to return tmp will not work.
Were you given the code?
Are you supposed to fix it?

1
2
Candidat People[1];
Candidat People[1] = set_value("Gosho", "172903", 4, 2, 5, 6);

The only valid index for an array with one element is 0.
Thanks for the reply!

I saw the function with "tmp" from a colleague who scored an A on his programming test, the rest I've written myself.
You could create a var called tmp and return it from set_value.
Topic archived. No new replies allowed.