Skip to content
Permalink
fe409bcb3a
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
351 lines (269 sloc) 9.16 KB
// TimeTable.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <Windows.h>
#include <mysql.h>
#include <iomanip>
#include <stdlib.h>
#include <sstream>
#include <stdio.h>
#include <iostream>
#include <list>
#include <chrono>
#include <ctime>
using namespace std;
const char* hostname = "localhost";
const char* username = "admin";
const char* password = "admin";
const char* database = "tt";
const int port = 3306;
const char* unixsocket = "NULL";
unsigned long clientfag = 0;
MYSQL* connectDatabase()
{
MYSQL* conn;
conn = mysql_init(0);
conn = mysql_real_connect(conn, hostname, username, password, database, port, unixsocket, clientfag);
return conn;
}
int date_cmp(const char* d1, const char* d2)
{
int rc;
// compare years
rc = strncmp(d1 + 6, d2 + 6, 4);
if (rc != 0)
return rc;
// compare months
rc = strncmp(d1 + 3, d2 + 3, 2);
if (rc != 0)
return rc;
// compare days
return strncmp(d1, d2, 2);
}
string selected_Date(MYSQL* conn)
{
string selected_date="";
MYSQL_ROW row;
MYSQL_RES* res;
list <string> dates;
int q;
q = mysql_query(conn, "SELECT slot_ID, date FROM slot GROUP By date");
if (!q)
{
res = mysql_store_result(conn);
int counter = 1;
while (row = mysql_fetch_row(res))
{
//cout << " Slot_ID : " << row[0] << endl;
//IDs.push_back(row[0]);
cout <<" " << row[1] << endl;
dates.push_back(row[1]);
counter++;
}
}
string date ;
cout << "\nEnter any date (in exact/same format) from the list shown above: " << endl;
cin >> date;
list <string> ::iterator it;
for (it = dates.begin(); it != dates.end(); ++it)
{
//cout << '\t' << *it;
if (date==*it)
{
selected_date = *it;
cout << "\n Selected Date: " << *it;
}
else
{
//
}
}
if (selected_date =="")
{
cout << "\nYou have entered false date." << endl;
}
return selected_date;
}
string selected_Module(MYSQL* conn)
{
string selected_Mod = "";
MYSQL_ROW row;
MYSQL_RES* res;
list <string> mod_IDs;
int q;
q = mysql_query(conn, "SELECT module_ID, mod_name, mod_code FROM module");
if (!q)
{
res = mysql_store_result(conn);
int counter = 1;
while (row = mysql_fetch_row(res))
{
cout << "\nModule ID : " << row[0] << endl;
mod_IDs.push_back(row[0]);
cout << "Name: " << row[1] << endl;
cout << "Code: " << row[2] << endl;
cout << "" << endl;
counter++;
}
}
string id;
cout << "\nEnter the exact ID of Module you want to take: " << endl;
cin >> id;
list <string> ::iterator it;
for (it = mod_IDs.begin(); it != mod_IDs.end(); ++it)
{
//cout << '\t' << *it;
if (id == *it)
{
selected_Mod = *it;
cout << "\n Selected Module: " << *it;
}
else
{
//
}
}
if (selected_Mod == "")
{
cout << "\nYou have entered wrong module ID." << endl;
}
return selected_Mod;
}
void showAllClasses(MYSQL* conn, string selected_Date)
{
MYSQL_ROW row;
MYSQL_RES* res;
int qstate;
if (conn)
{
string query = "SELECT start_time, end_time, module.mod_name, module.mod_code FROM slot INNER JOIN module ON slot.fk_module = module.module_ID WHERE date = '" + selected_Date + "'";
const char* q = const_cast<char*>(query.c_str());
qstate = mysql_query(conn, q);
if (!qstate)
{
res = mysql_store_result(conn);
while (row = mysql_fetch_row(res))
{
cout << "\n" << endl;
cout << " Start_Time : " << row[0] << endl;
cout << " End_Time: " << row[1] << endl;
cout << " Name: " << row[2] << endl;
cout << " Code: " << row[3] << endl;
cout << "\n" << endl;
}
}
}
}
void showAllSlots(MYSQL* conn, string select_Mod)
{
MYSQL_ROW row;
MYSQL_RES* res;
int qstate;
// current date/time based on current system
time_t now = time(0);
//cout << "Number of sec since January 1,1970 is:: " << now << endl;
tm* ltm = localtime(&now);
// print various components of tm structure.
//cout << "Year:" << 1900 + ltm->tm_year << endl;
//cout << "Month: " << 1 + ltm->tm_mon << endl;
//cout << "Day: " << ltm->tm_mday << endl;
//cout << "Time: " << 5 + ltm->tm_hour << ":";
//cout << 30 + ltm->tm_min << ":";
//cout << ltm->tm_sec << endl;
int y = (1900 + ltm->tm_year);
int m = (1 + ltm->tm_mon);
int day = ltm->tm_mday;
string dd = "" + to_string(y) +"-"+ to_string(m) +"-"+ to_string(day);
cout << "\nToday's date: " << dd << endl;
if (conn)
{
string query = "SELECT slot.date, slot.start_time, slot.end_time FROM slot INNER JOIN module ON slot.fk_module = module.module_ID WHERE module.module_ID = '" + select_Mod + "' AND slot.date >= '"+dd+"'";
const char* q = const_cast<char*>(query.c_str());
qstate = mysql_query(conn, q);
if (!qstate)
{
res = mysql_store_result(conn);
while (row = mysql_fetch_row(res))
{
cout << "\n" << endl;
cout << " Date : " << row[0] << endl;
cout << " Start_Time : " << row[1] << endl;
cout << " End_Time: " << row[2] << endl;
//cout << "\n" << endl;
}
}
}
}
int main()
{
MYSQL* conn = connectDatabase();
MYSQL_ROW row;
MYSQL_RES* res;
char option;
do {
string stud_name;
/**< Get student's name */
cout << "Enter your name:" << endl;
cin >> stud_name;
cout << "Name: " + stud_name << endl;
/**< Compare student name from database */
int qstate;
if (conn)
{
cout << "Connection Successfull!\n";
qstate = mysql_query(conn, "SELECT * FROM student");
if (!qstate)
{
res = mysql_store_result(conn);
while (row = mysql_fetch_row(res))
{
//cout << " ID : " << row[0] << endl;
cout << " Name: " << row[1] << endl;
if (stud_name == row[1])
{
cout << "\nSelect any one of the following:" << endl;
cout << "1. Check classes by DATE" << endl;
cout << "2. Check classes by MODULES" << endl;
cout << "\nEnter 1 for DATE and 2 for MODULES ..." << endl;
char op;
cin >> op;
if (op == '1') {
// Ask for date
cout << "\nSelect any date from the following: " << endl;
string select_Date = selected_Date(conn);
showAllClasses(conn, select_Date);
}
else if (op == '2') {
// Ask for modules
cout << "\nSelect any module from the following: " << endl;
string select_Mod = selected_Module(conn);
showAllSlots(conn, select_Mod);
}
else {
cout << "\nEnter correct option!!" << endl;
}
break;
}
else
{
cout << "Enter correct name." << endl;
}
}
}
}
else
{
cout << "Connection Unsuccessfull!\n";
}
cout << "\nContinue? (y/n) ";
cin >> option;
} while (option == 'y' || option == 'Y');
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file