1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#include <iostream>
#include <cstdlib> //header for dynamic memory management
using namespace std;
void createArrays(int *labs[], int compStation[]); //create dynamic array for lab
void outputLabs(int *labs[], int compStation[]); //display lab
void releaseArrays(int *labs[]); //free array to reallocate space
void logIn(int *labs[], int compStation[]); //allow user login
void logOut(int *labs[], int compStation[]); //allow user logout
void searchLab(int *labs[], int compStation[]); //search data within lab parameters
const int numLabs = 4; //unchangable variable for 4 lab numbers
int main()
{
int *labs[numLabs]; //referencing each lab section
int compStation[numLabs]; //the number of computers in each lab
int choice = -1; //allow user choice
compStation[0] = 5; //initializing 5 computers in lab 1
compStation[1] = 6; //initializing 6 computers in lab 2
compStation[2] = 4; //initializing 4 computers in lab 3
compStation[3] = 3; //initializing 3 computesr in lab 4
createArrays(labs, compStation);
while (choice != 4) //aslong as choice does not equal choice of Quit
{
outputLabs(labs, compStation); //display the lab to user
cout << "Main Menu \n";
cout << "1) User Login \n";
cout << "2) User Logout \n";
cout << "3) Search lab information \n";
cout << "4) Quit program \n";
cin >> choice;
if (choice == 1)
{
logIn(labs, compStation);
}
else if (choice ==2)
{
logOut(labs, compStation);
}
else if (choice==3)
{
searchLab(labs, compStation);
}
else
cout << "You have entered an invalid option" << endl;
}
releaseArrays(labs); //reallocate memory space
return 0;
}
void createArrays(int *labs[], int compStation[])
{
for (int i = 0; i < numLabs; i++)
{
labs[i] = new int[compStation[i]]; //create new dynamic array for each lab section there is a computer station
for (int j =0; j <compStation[i]; j++)
{
labs[i][j] = -1; //simulating an unsed computer
}
}
}
void outputLabs(int *labs[], int compStation)
{
cout << "Your Lab: " << endl;
cout << "Lab # Computer Stations";
for (int i = 0; i < numLabs; i++)
{
cout << i + 1 << " "; //initializing space for format
for (int j = 0; j < compStation[i]; j++)
{
cout << j + 1 << " "; //initializing space
if (labs[i][j] == -1)
{
cout << "empty field"<<endl;
}
else
{
cout << labs[i][j] << endl;
}
}
cout << endl;
}
return;
}
void releaseArrays(int *labs[])
{
for (int i = 0; i < numLabs; i++)
{
delete[] labs[i];
}
return;
}
void logIn(int *labs[], int compStation[])
{
int userID;
int labnumb;
int compnumb;
userID = 0;
labnumb = 0;
compnumb = 0;
while (userID < 1 || userID > 99999) //5 digit user input
{
cout << "Enter the 5 digit user ID for logging in: "<<endl;
cin >> userID;
}
while (labnumb < 1 || labnumb > numLabs)
{
cout <<"Enter the lab number from 1-"<<numLabs << endl;
cin>>labnumb;
}
while (compnumb < 1 || compnumb > compStation[labnumb-1]) //-1 to decrement to reach first array)
{
cout <<"Enter the computer station number: " << "from 1-" <<compStation[labnumb-1]<<endl;
cin>>compnumb;
}
if (labs[labnumb-1][compnumb-1] != -1)
{
cout <<"User"<<labs[labnumb-1][compnumb-1]<<" is alerady logged in" << endl;
return;
}
labs[labnumb-1][compnumb-1] = userID;
return;
}
void logOut(int *labs[], int compStation[])
{
int userID;
userID = 0;
while (userID <1 || userID > 99999)
{
for (int i = 0; i <numLabs; i++)
{
for(int j = 0; j <compStation[i]; j++)
{
if(labs[i][j]==userID)
{
labs[i][j] = -1; //user log off by setting value to -1
cout <<"The User " <<userID<<"is logged off"<< endl;
return;
}
}
}
}
return;
}
void searchLab(int *labs[], int compStation[])
{
int userID;
userID = 0;
while(userID < 1 || userID >99999)
{
cout << "Enter the ID number of the user you wish to find: "<<endl;
cin>>userID;
}
for (int i = 0; i < numLabs; i++)
{
for (int j = 0; j < compStation[i]; j++)
{
if (labs[i][j]==userID)
{
cout << "The User " <<userID<< " is in lab " << i + 1 << " at computer station " << j + 1 << endl;
return;
}
else
cout <<"That user is not lgoged in";
}
}
return;
}
|