Tuesday, March 20, 2018

Programming Challenge 17.2 - Course Information

Example Files: CourseInformation.7z

CourseInformation.cpp


#include <iomanip>
#include <iostream>
#include <map>
#include <string>

void courseSearch(const std::map<std::string, std::string> &,
                        const std::map<std::string, std::string> &,
                        const std::map<std::string, std::string> &);

void print(const std::map<std::string, std::string>::const_iterator,
                      std::map<std::string, std::string>,
                      std::map<std::string, std::string>);

int main()
{
    std::map<std::string, std::string> rooms =
    { { "CS101", "3004" }, { "CS102", "4501" }, { "CS103", "6755" },
      { "NT110", "1244" }, { "CM241", "1411" } };

    std::map<std::string, std::string> instructors =
    { { "CS101", "Haynes" }, { "CS102", "Alvarado" }, { "CS103", "Rich" },
      { "NT110", "Burke" }, { "CM241", "Lee" } };

    std::map<std::string, std::string> timetables =
    { { "CS101", "08:00 a.m." }, { "CS102", "09:00 a.m." }, { "CS103", "10:00 a.m." },
      { "NT110", "11:00 a.m" }, { "CM241", "01:00 p.m." } };

    char again{};

    std::cout << "M.I.T. COMPUTER SCIENCE DEPARTMENT COURSE SEARCH\n";

    do
    {
        courseSearch(rooms, instructors, timetables);

        std::cout << "\nDo you wish to repeat your search? (Y/N): ";
        std::cin >> again;

        while (toupper(again) != 'Y' && toupper(again) != 'N')
        {
            std::cout << "\nPlease repeat your input: ";
            std::cin >> again;
        }
    } while (toupper(again) != 'N');

    std::cout << "\nM.I.T. - The Union Of Knowledge And Mechanical Arts.";

    std::cin.ignore();
    std::cin.ignore();
    return 0;
}

/* **********************************************************
            courseSearch() : const map &, const map &,
                                  const map &
    The user is asked to enter a course number. If it exists
    in the map, it will be output. Otherwise the user is
    informed, that the course does not exist.
   ********************************************************** */

void courseSearch(const std::map<std::string, std::string> &rooms,
                       const std::map<std::string, std::string> &instructors,
                       const std::map<std::string, std::string> &timetables)
{
    std::string courseID{};

    std::cout << "\nEnter a course ID: ";
    std::cin >> courseID;

    std::map<std::string, std::string>::const_iterator it = rooms.find(courseID);

    if (it != rooms.end())
    {
        print(it, instructors, timetables);
    }
    else
    {
        std::cout << courseID << " does not exist ..." << std::endl;
    }
}

/* **********************************************************
            print() : const map<>::iterator, map, map
    The user is asked to enter a course number. If it exists
    in the map, it will be output. Otherwise the user is
    informed, that the course does not exist.
   ********************************************************** */

void print(const std::map<std::string, std::string>::const_iterator it,
                      std::map<std::string, std::string> instructors,
                      std::map<std::string, std::string> timetables)
{
    std::cout << "\n" << std::setw(15) << std::left << "Course:"
                            << std::setw(12) << std::left << "Room:"
                            << std::setw(23) << std::left << "Instructor:"
                            << std::setw(5) << std::left << "Time:\n";
   
    std::cout << std::setw(14) << std::left
                 << std::setw(15) << std::left << it->first
                 << std::setw(12) << std::left << it->second
                 << std::setw(23) << std::left << instructors[it->first]
                 << std::setw(18) << std::left << timetables[it->first] << std::endl;
}

Example Output:



No comments:

Post a Comment