Void with string function

Why isn't my "void display(string id) not showing up in the output?

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

struct student_t
{
	string id;
	double tma;
	double quiz;
	double exam;
	double total;
};
student_t db[3];
void initialize();
void display(string id);
int main()
{
	initialize();
	string id;
	display(id);
}

void initialize()
{
	for (int i=0;i<3;i++)
	{
		cout<<"Enter id: ";
		cin>>db[i].id;
		cout<<"Enter tma: ";
		cin>>db[i].tma;
		cout<<"Enter quiz: ";
		cin>>db[i].quiz;
		cout<<"Enter exam: ";
		cin>>db[i].exam;
		db[i].total = db[i].tma * 0.25 + db[i].quiz * 0.25 + db[i].exam * 0.50;
	}
}

void display (string id)
{
	for (int i=0;i<3;i++)
	{
		if (db[i].id == id)
		{
			cout<<"ID: "<<id<<endl;
			cout<<"TMA: "<<db[i].tma<<endl;
			cout<<"Quiz: "<<db[i].quiz<<endl;
			cout<<"Exam: "<<db[i].exam<<endl;
			cout<<"Total: "<<db[i].total<<endl;
			break;
		}return;
	}
}
Because of your if statement in the display function.

if (db[i].id == id)

"id" is garbage. Look how you send it in.

1
2
string id;
display(id);


You never give "id" a value. It is equal to nothing. It is empty, garbage. So how can db[i].id ever be equal to it? It cant. What exactly are you trying to do here, so I can help you with it.
Q4. Given the following information, define a C++ record type, Student.
id TMA Score Quiz Score Exam Score Total Score
S001 80 65 76
S002 56 62 66
S003 88 76 82
a) declare a global variable, db, to hold 3 student records. Studentdb[[3];
b) Using the variable defined above, write the function, void initialise(), to initialise each student record. Assume that the weightage is TMA is 25%; Quiz is 25% and Exam is 50%. The final total score is out of 100.
c) Write a function, display(string id),to display the student’s details for student id.

I've figured out how to do (a) and (b) but I can't seem to get the output for (c).
I would really appreciate your help.
Thank you!
You almost got it right in that case. I think c) wants the user to be able to enter an ID. And if that ID exists, it will display the information. You are almost there, you only need an input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void display(string id)
{
	cout << "\nDo you want to display details of a student? Enter their ID: ";
	cin >> id; // User enter's their ID.

	for (int i = 0; i<3; i++)
	{
		if (db[i].id == id) // if the user's ID matches with a student's ID. The details will be displayed.
		{
			cout << "\nID: " << id << endl;
			cout << "TMA: " << db[i].tma << endl;
			cout << "Quiz: " << db[i].quiz << endl;
			cout << "Exam: " << db[i].exam << endl;
			cout << "Total: " << db[i].total << endl;
			break;
		} // removed the return; that was here. It only ruined things.
	}
}


This will only allow you to check out 1 student though. If you want to be able to check the details for multiple students, then you can create a loop that keeps going until the user enters "done" or something similar.
Last edited on
Thank you so much! :)
Topic archived. No new replies allowed.