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.