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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
// *********************************************************************
//
// This program uses dynamic arrays to store login information for
// four labs. Each of the four labs is referenced by the labs[] array
// which is indexed from 0-3. A pointer in the labs[] array then
// references a dynamic array that is of size for however many computers
// are in that lab.
//
// *********************************************************************
#include <iostream>
#include <cstdlib>
using namespace std;
// Type definition
typedef int* IntPtr;
// Constants
const int NUMLABS = 4;
// Function prototypes
void createArrays(IntPtr labs[], int labsizes[]);
// ======================
// createArrays:
// Creates the dynamic arrays for the labs.
// The first array is the array of labs,
// The second array contains the size (or number of computers)
// we will put in each lab. This dictates the size of the dynamic
// array.
// ======================
void freeArrays(IntPtr labs[]);
// ======================
// freeArrays:
// Releases memory we allocated with "new".
// ======================
void showLabs(IntPtr labs[], int labsizes[]);
// ======================
// showLabs:
// Displays the status of all labs (who is logged into which computer).
// ======================
void login(IntPtr labs[], int labsizes[]);
// ======================
// login:
// Simulates a user login by asking for the login info from
// the console.
// ======================
void logoff(IntPtr labs[], int labsizes[]);
// ======================
// logoff:
// Searches through the arrays for the input user ID and if found
// logs that user out.
// ======================
void search(IntPtr labs[], int labsizes[]);
// ======================
// search:
// Searches through the arrays for the input user ID and if found
// outputs the station number.
//=======================
int main()
{
IntPtr labs[NUMLABS]; // Array to reference each lab
int labsizes[NUMLABS]; // Number of computers in each lab
int choice = -1;
// Initialize labsizes to those given in the problem
labsizes[0] = 5; // 5 computers in lab 1
labsizes[1] = 6;
labsizes[2] = 4;
labsizes[3] = 3; // 3 computers in lab 4
// Create ragged array structure
createArrays(labs, labsizes);
// Main Menu
while (choice != 0)
{
cout << endl;
showLabs(labs, labsizes);
cout << "MAIN MENU" << endl;
cout << "0) Quit" << endl;
cout << "1) Simulate login" << endl;
cout << "2) Simulate logoff" << endl;
cout << "3) Search" << endl;
cin >> choice;
if (choice == 1)
{
login(labs, labsizes);
}
else if (choice == 2)
{
logoff(labs, labsizes);
}
else if (choice == 3)
{
search(labs, labsizes);
}
}
freeArrays(labs); // Free memory before exiting
return 0;
}
void createArrays(IntPtr labs[], int labsizes[])
{
labs[i][j] = -1; // all I was able to think of here
}
void freeArrays(IntPtr labs[])
{
//I'm assuming this is where I would delete the dynamic array but why make a function for it if I can just do delete [] ...;
}
void showLabs(IntPtr labs[], int labsizes[])
{
using namespace std;
int i;
int j;
cout << "LAB STATUS" << endl;
cout << "Lab # Computer Stations" << endl;
for (i=0; i < NUMLABS; i++)
{
cout << i+1 << " ";
for (j=0; j < labsizes[i]; j++)
{
cout << (j+1) << ": ";
if (labs[i][j] == -1)
{
cout << "empty ";
}
else
{
cout << labs[i][j] << " ";
}
}
cout << endl;
}
cout << endl;
return;
}
void login(IntPtr labs[], int labsizes[])
{
int id = -1, lab = -1, num = -1;
// Get input from the keyboard, validating data ranges
while ((id < 0) || (id > 99999))
{
cout << "Enter the 5 digit ID number of the user logging in:" << endl;
cin >> id;
}
while ((lab < 0) || (lab > NUMLABS))
{
cout << "Enter the lab number the user is logging in from (1-" <<
NUMLABS << "):" << endl;
cin >> lab;
}
while ((num < 0) || (num > labsizes[lab-1]))
{
cout << "Enter computer station number the user is logging in to " <<
"(1-" << labsizes[lab-1] << "):" << endl;
cin >> num;
}
// Check to see if this station is free
if (labs[lab-1][num-1]!=-1)
{
cout << "ERROR, user " << labs[lab-1][num-1] <<
" is already logged into that station." << endl;
return;
}
// Assign this station to the user
labs[lab-1][num-1] = id;
return;
}
void logoff(IntPtr labs[], int labsizes[])
{
int id = -1, i,j;
// Get input from the keyboard, validating data ranges
while ((id < 0) || (id > 99999))
{
cout << "Enter the 5 digit ID number of the user to find:" << endl;
cin >> id;
}
for (i=0; i<NUMLABS; i++)
{
for (j=0; j<labsizes[i]; j++)
{
if (labs[i][j]==id)
{
// Log the user off by setting the entry to -1
labs[i][j] = -1;
cout << "User " << id << " is logged off." << endl;
return;
}
}
}
cout << "That user is not logged in." << endl;
return;
}
void search(IntPtr labs[], int labsizes[])
{
//I should be able to do this once I am able to make the array
}
|