Programmed-define type: Struct
It's been a week, so a new post for DILI. That's what I'll call this from now on because it has a ring to it! Anyways, you might be wondering, where's part 2 for scoped enumerations. Well ... it's kinda the same the, but simply better. So, struct it is. To go in specific wouldn't be fun, would it? So I'll leave a link if you want to get a good look at the concept with Alex.
Different from any other data types that I've learnt until now, struct is a powerhouse ... for management. It can house different data types in it: int, double, string, .... Sounds underwhelming but when you're faced with huge amount of information for one entity like a person's ID, you will be so happy that this exist. A few dozens variables can just be compacted into a few shorter lines.
But imagine what you might be able to do with this. I've said something simiar before in the enumeration posts but when I actually attemped to do something like a Hotel Management System, I ultimately failed (Most likely my bren is not good enough). But with this, voilà:
#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;
struct Hotel
{
string name{};
string phoneNumber{};
string DOB{};
bool booked{};
};
Hotel roomNum[10];
int a = 0;
int rNo;
void checkRooms()
{
cout << "Rooms are available if 0 are shown and book if 1 are shown:\n";
for (int i = 0; i < 10; i++){
cout << "Room " << i << ")" << roomNum[i].booked << "\n\n ";
}
}
void bookRoom ()
{
cout << "Which room would you like to book ? (Type a number) \n";
cin >> rNo;
if (!roomNum[rNo].booked){
roomNum[rNo].booked = true;
cout << "What is your name? \n";
cin >> roomNum[rNo].name;
cout << "What is your phone number? \n";
cin >> roomNum[rNo].phoneNumber;
cout << "What is your DOB? \n";
cin >> roomNum[rNo].DOB;
cout << "You have book Room " << rNo << " sucessfully!\n\n";
}
else{
cout << "This room is already booked. Please try again!\n\n";
bookRoom();
}
}
void checkInfo()
{
cout << "Which room would you like to check ? (Type a number) \n";
cin >> rNo;
if (roomNum[rNo].booked){
cout << "Room " << rNo <<"'s info:\n";
cout << "Name: " << roomNum[rNo].name <<"\n";
cout << "Phone number: " << roomNum[rNo].phoneNumber <<"\n";
cout << "DOB: " << roomNum[rNo].DOB <<"\n\n";
}
else {
cout << "This room is not booked yet. Please check again!\n\n";
checkInfo();
}
}
int main()
{
if (a == 0){
for (int i = 0; i < 10; i++){
roomNum[i].booked = false;
}
a = 1;
}
cout << "Type a number corresponding to an action: \n" ;
cout << "(1) Check rooms. (2) Book a room. (3) Check info. (4) Exit.\n" ;
int customerAction{};
cin >> customerAction;
if (customerAction == 1) checkRooms();
if (customerAction == 2) bookRoom();
if (customerAction == 3) checkInfo();
if (customerAction == 4) return 0;
main();
}
A bit clanky, I know. Like the first few lines in the function main(). And I'd like a suggestion on how should I create a built-in ordering system for food and beverages. Any advices on the code or my blog writing skills or interesting facts about anything at all are much appreciated.
REFERENCE
Chapter 10.5-10.6
Top comments (0)