Answer to Question #3243 in C++ for iko

Question #3243
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 Sudent 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, [3] last name, and [4] IC number. After the user chooses the field or which to search, prompt the user for 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-01T18:20:47-0400
#include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;

struct Student
{
int id;
char first_name[50];
char last_name[50];
int icNumber;
};

const int array_size = 2;
Student students[array_size];
int entered_students = 0;

void search();
Student create_student();
bool is_id_correct(int id);
void fill_array();
void search_by_id(int id);
void search_by_first_name(char fn[80]);
void search_by_last_name(char ln[80]);
void search_by_1c(int ic);
void print_fields(Student st);

int main()
{
fill_array();
search();
system("pause");
return 0;
}

void search()
{
cout << "Enter the kind of search:\n";
cout << "\t1. Search by Id\n";
cout << "\t2. Search by first name\n";
cout << "\t3. Seacrh by last name\n";
cout << "\t4. Search by IC number\n";
char choice;
do
{
& choice = getch();
}
while (!(choice <= '4' && choice >= '1'));
cout << endl << endl;
switch (choice)
{
case '1':
& cout << "Enter the id: ";
& int id;
& cin >> id;
& search_by_id(id);
& break;
case '2':
& cout << "Enter the first name: ";
& char fn[50];
& cin >> fn;
& search_by_first_name(fn);
& break;
case '3':
& cout << "Enter the last name: ";
& char ln[50];
& cin >> ln;
& search_by_last_name(ln);
& break;
case '4':
& cout << "Enter the 1C number: ";
& int ic;
& cin >> ic;
& search_by_1c(ic);
& break;
&
}
}

Student create_student()
{
Student student;
do
{
& cout << "Enter the student's id: ";
& cin >> student.id;
& if (is_id_correct(student.id))
& break;
& cout << "The student with such id has already been added\n";
}
while (true);

cout << "Enter the student's first name: ";
cin >> student.first_name;
cout << "Enter the student's last name: ";
cin >> student.last_name;
cout << "Enter the student's 1C number: ";
cin >> student.icNumber;
cout << "The student has been added!\n\n";
entered_students++;
return student;
}

void fill_array()
{
for (int i = 0; i < array_size; i++)
& students[i] = create_student();
}

bool is_id_correct(int id)
{
for (int i = 0; i < entered_students; i++)
& if (students[i].id == id)
& return false;
return true;
}

void search_by_id(int id)
{
for (int i = 0; i < array_size; i++)
& if (students[i].id == id)
& {
& print_fields(students[i]);
& return;
& }
cout << "There is no such student in the array!\n";
}

void search_by_first_name(char fn[80])
{
for (int i = 0; i < array_size; i++)
& if (strcmp(fn, students[i].first_name) == 0)
& {
& print_fields(students[i]);
& return;
& }
cout << "There is no such student in the array!\n";
}

void search_by_last_name(char ln[80])
{
for (int i = 0; i < array_size; i++)
& if (strcmp(ln, students[i].last_name) == 0)
& {
& print_fields(students[i]);
& return;
& }
cout << "There is no such student in the array!\n";
}

void search_by_1c(int ic)
{
for (int i = 0; i < array_size; i++)
& if (students[i].icNumber == ic)
& {
& print_fields(students[i]);
& return;
& }
cout << "There is no such student in the array!\n";
}

void print_fields(Student st)
{
cout << "The student: \n";
cout << "id: " << st.id << "\n";
cout << "first name: " << st.first_name << "\n";
cout << "last name: " << st.last_name << "\n";
cout << "1C number " << st.icNumber << "\n\n";
}

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