Answer to Question #3294 in C++ for sya

Question #3294
Create a structure named Student. Include data fields to hold Student’s ID number, first name, last name and IC number. Create an array of five Student variables. Prompt the user to enter data for each Student. Do not allow duplicate ID numbers to be entered. Then prompt the user to choose whether to search for the Student by [1] ID number, [2] first name, or [3] IC number. After the user chooses the field on which to search, prompt the user for the search value. Display an error message if there is no Student with matching criteria; otherwise display all the data for every matching Student.
1
Expert's answer
2011-07-05T07:32:19-0400
#include <iostream>
#include <cstring>
using namespace std;

const int LENGTH = 15; & // Max number of charecters in strings
const int SIZE = 5; & // Number of students


//Structure containing student's information
struct Student
{
int ID;
char firstName[LENGTH];
char lastName[LENGTH];
int IC;
};

//Function to check whether students ID are similar
bool IsDuplicate (Student & st, Student * stArr, int max)
{
for (int i=0; i<max; i++){
& if (stArr[i].ID == st.ID)
& return true;
}
return false;
}


//Function to show full information about student
void ShowInfo (Student st)
{
cout<< "Student's ID:\t"<< st.ID<< endl;
cout<< "Student's first name:\t"<< st.firstName<< endl;
cout<< "Student's last name:\t"<< st.lastName<< endl;
cout<< "Student's IC:\t"<< st.IC<< endl;
cout<< endl;
}


//Function to show error message if any students weren't found
void ShowError ()
{
system ("cls");
cout<< "NO STUDENTS MATCHING GIVEN CRITERIA!!!"<< endl;
}

//Function to search students by thir ID number
void SearchID(Student * group)
{
int ID;
cout<< "Enter ID"<< endl<< "> ";
cin >> ID;
system ("cls");
for (int i=0; i<SIZE; i++){
& if (ID == group[i].ID){
ShowInfo(group[i]);
return ;
& }
}
& ShowError();
}

//Function to compare student's first name with name entered
bool Compare (char * str1, char * str2)
{
int i=0;
while (str2[i] != '\0')
{
& if (str1[i] != str2[i])
& return false;
& i++;
}
return true;
}

//Function to search student by name
void SearchName(Student * group)
{
bool flag = false;
char name[LENGTH];
system ("cls");
cout<< "Enter first name"<< endl<< "> ";
cin >> name;
system ("cls");
for (int i=0; i<SIZE; i++){
& if (Compare(name, group[i].firstName)){
ShowInfo(group[i]);
flag = true;
& }
}
if (!flag)
& ShowError();
}


//Function to search student by IC number
void SearchIC (Student * group)
{
bool flag = false;
int IC;
cout<< "Enter IC"<< endl<< "> ";
cin >> IC;
system ("cls");
for (int i=0; i<SIZE; i++){
& if (IC == group[i].IC){
ShowInfo(group[i]);
flag = true;
& }
}
if (!flag)
& ShowError();
}

int main ()
{
Student group[SIZE]; // Creating array of students
int choice;

for (int i=0; i<SIZE; i++){
& system ("cls");
& cout<< "& **Enter information about "<< i+1<< " student"<< endl;
& do
& {
cout<< "Enter student's ID (must be unique!)"<< endl<< "> ";
cin >> group[i].ID;
& }
& while (IsDuplicate (group[i], group, i));
& cout<< "Enter student's first name"<< endl<< "> ";
& cin >> group[i].firstName;
& cout<< "Enter student's last name"<< endl<< "> ";
& cin >> group[i].lastName;
& do
& {
cout<< "Enter student's IC number (non-negative)"<< endl<< "> ";
cin >> group[i].IC;
& }
& while (group[i].IC < 0);
}

system ("cls");

cout<< "& ** Choose search criteria"<< endl;
cout<< "ID number:\t1"<< endl;
cout<< "First name:\t2"<< endl;
cout<< "IC number:\t3"<< endl;
cout<< endl<< "> ";
cin >> choice;

switch (choice)
{
& case 1:
SearchID(group);
break;
& case 2:
SearchName(group);
break;
& case 3:
SearchIC(group);
break;
& default:
cout<< "CHOOSE VALUE BETWEEN 1 AND 3!!!";
system ("pause");
break;
}
system("pause");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS