Hello,
I need to write a program that simulates a copmuter lab (ie Login, Logout, etc). So the problem I am having a in the IsValidLab function. This functin is designed to go through the rows that I've allocated in my dynamic array and see if it the user inputed a valid lab number. I've designed it in a way where if it isnt null it should return a number 1 that would in turn tell the main to move on, but its not returning a 1. It stays at 0 even if the input is valid.
#include<iostream>
#include <cstdlib>
using namespace std;
int** Allocate(int lab[]);
void Init2D(int** twoD, int lab[]);
int* IsValidLab(int** twoD, int row, int &IsValLab);
int* IsValidComputer(int** twoD, int row, int col, int **rowWalker);
void Login(int** twoD, int row, int col, int ID);
void Print(int** twoD, int lab[]);
main()
{
int lab[] = {5, 6, 4, 3, NULL};
char ans = ' ';
int** twoD;
char choice=' ';
int row, col, num, IsValLab = 0;
twoD = Allocate(lab);
Init2D(twoD, lab);
Print(twoD, lab);
cout << "\n\n\t***************************"
<< "\n\t| Log[i]n Log[o]ut [Q]uit |"
<< "\n\t***************************" << endl;
cin >> choice;
while (toupper(choice) != 'Q')
{
switch (toupper(choice))
{
case 'I': while (IsValLab == 0)
{
cout << IsValLab << endl;
cout << "Lab: ";
cin >> row;
row = row - 1;
IsValidLab(twoD, row, IsValLab);
}
cout << "Computer Station: ";
cin >> col;
col = col - 1;
cout << "ID number: ";
while (num < 10000 || num > 99999)
{
cin >> num;
cout << "Invalid ID number, Please enter a 5 digit ID number: ";
}
Login(twoD, row, col, num);
break;
case 'O':
break;
}
system("CLS");
Print(twoD, lab);
cout << "\n\n\t***************************"
<< "\n\t| Log[i]n Log[o]ut [Q]uit |"
<< "\n\t***************************" << endl;
cin >> choice;
}
//delete two d array...
delete [] twoD;
cout << endl << endl;
system("pause");
}
int** Allocate(int lab[])
{
int** hold;
hold = new int*[5];
for (int i = 0; i < 5; i++)
hold[i] = new int[lab[i]];
hold[5] = NULL;
return hold;
}
void Init2D(int** twoD, int lab[])
{
for (int i = 0; i < 5; i++)
{
int* stationWalker = twoD[i];
for (int j = 0; j < lab[i]; j++)
{
*stationWalker++ = 0; //*stationWalker = 0; stationWalker++;
}
*stationWalker = -1;
}
}
int* IsValidLab(int** twoD, int row, int &IsValLab)
{
int** rowWalker = twoD;
int rowSteps = 0;
while ((*rowWalker != NULL) && //the thing pointed to rowWalker!= NULL
(rowSteps != row)) //have not taken row steps...
{
rowWalker++;
rowSteps++;
}
if (*rowWalker == NULL) //we have reached the end...
{
cout << "\nInvalid Lab number." << endl;
IsValLab == 0;
}
else
{
IsValLab == 1;
return *rowWalker;
}
}
int* IsValidComputer(int** twoD, int row, int col, int **rowWalker)
{
int* colWalker = *rowWalker;
int colSteps = 0;
while ((*colWalker != -1) && //the thing pointed to colWalker!= NULL
(colSteps != col)) //have not taken col steps...
{
colWalker++;
colSteps++;
}
if (*colWalker == -1) //we have reached the end...
{
cout << "\nInvalid Computer Station." << endl;
return NULL;
}
else
return colWalker;
}
void Login(int** twoD, int row, int col, int ID)
{
//find my row:
int **rowWalker = twoD;
for (int i = 0; i < row; i++) //rowWalker += rows;
{
rowWalker ++;
}
//rowWalker poitns to the beginning of the row
//find my col (cell):
int *colWalker;
colWalker = *rowWalker;
//pointing to the first cell in the row.
for (int j = 0; j < col; j++)
{
colWalker++;
}
//colWalker points to the cell
*colWalker = ID;
}
void Print(int** twoD, int lab[])
{
int rows = 4;
int** rowWalker = twoD;
int* colWalker;
cout << "Computer Lab" << endl
<< "*************\n" << endl
<< "Lab Number " << "Computer Stations" << endl;
for (int i = 0; i < rows; i++)
{
cout << i + 1 << "\t ";
colWalker = *rowWalker;
for (int j = 0; j < lab[i]; j++)
{
if (*colWalker == 0)
{
cout << " " << (j + 1) << ": " << "empty";
}
else
{
cout << " " << (j + 1) << ": " << *colWalker;
}
colWalker++;
}
rowWalker ++;
cout << endl;
}
}