So I've been trying to code a simulation in which you can sign into a computer in a computer lab. First you must enter in the lab you wish (1-4) and a seat. Each lab has their own specified seats available such as lab 1 has 5 seats while lab 2 has 6. I only have the login function for now because when I call upon the status function, which is supposed to display all seats that are taken and not taken, it doesn't work.
#include <iostream>
usingnamespace std;
class sign
{
public:
sign();
void login(int* lab[], int lab_slots[]);
void status(int *lab[],int lab_slots[]);
void generate(int* lab[], int lab_slots[]);
void main2();
private:
int lab_no;
int ID;
int *labs_slots;
int* lab[4];
};
sign::sign()
{
}
void sign::generate(int* lab[], int lab_slots[])
{
for (int i = 0;i < 4;i++)
{
lab[i] = newint[lab_slots[i]];//creating lab with labslots[i]
for (int j = 0;j < lab_slots[i];j++)//go through all slots and equal them to 0
{
lab[i][j] = 0;
}
}
}
int main()
{
sign enter;
enter.main2();
}
void sign::main2()
{
sign enter;
int anwser;
int lab_slots[] = { 5,6,4,3 };//contains number slots of each lab
cout << "MAIN MENU" << endl;
cout << "0.Quit" << endl;
cout << "1.login" << endl;
cout << "2.logoff" << endl;
cout << "3.Search" << endl;
cin >> anwser;
switch (anwser)
{
case 0:
cout << "GoodBye!" << endl;
exit(1);
break;
case 1:
enter.login(lab, lab_slots);
}
}
void sign::status(int* lab[], int lab_slots[])
{
cout << "Status";
for (int i = 0;i < 4;i++)//going through the labs
{
cout << i + 1;
for (int j = 0;j < lab_slots[i];j++)//inner loop going though the labs
{
if (lab[i][j] != 0)//if the seat is taken, it will display so
{
cout << " " << j + 1 << ": " << lab[i][j];
}
else
{
cout << " " << j + 1 << ":empty ";
}
}
cout << endl;
}
}
void sign::login(int* lab[], int lab_slots[])
{
int slot;
cout << "Enter in the 5 digit code to log in" << endl;
cin >> ID;
cout << "Enter in lab number (1-4)" << endl;
cin >> lab_no;
if (lab_no >= 5)
{
cout << "Invalid output!" << endl;
exit(1);
}
int slots = lab_slots[lab_no - 1];// get the available number of slots in the Lab entered by the user from array.
cout << "Input computer station # (1-" << slots << "): \n";
cin >> slot;
if (lab[lab_no - 1][slot - 1] != 0)//if this is set to true it is taken as seen with the !=0
{
cout << "Lab is taken" << endl;
}
else
{
lab[lab_no][slot - 1] = ID;//ID entered is now assigned to the lab and slot (or station)
}
status(lab, lab_slots);
}
I don't know if you're required to have your functions the way they are, but generate should be a constructor. Why? Because that way, it's harder to forget to call it before you call status, as you do in your program.
Are you allowed to use std::vector? I think it would help you here.