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;
    }
}

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.

It doesn't do anything. All the function does is increment a pointer and the steps.

And if (*rowWalker == NULL) is TRUE you don't actually return anything. A value is only returned when if (*rowWalker == NULL) is FALSE.

ok i get what you mean here

And if (rowWalker == NULL) is TRUE you don't actually return anything. A value is only returned when if (rowWalker == NULL) is FALSE.

but that function is not really supposed to do anything. All it does is go through the array and see if the user inputed a lab number thats in the array.

Lines 112 and 116 are using the comparision equal == not the set equal = operator. Your compiler should be warning you about that. If it is not either use the -wall flag or set you compiler to treat warning as errors.

when I change that the program crashes after I input any number.

Have you steped through your code to see where it is crashing? Have you put in any cout staements to see what values are before you use them?

but that function is not really supposed to do anything. All it does is go through the array and see if the user inputed a lab number thats in the array.

But it doesn't "see if the user inputed a lab number thats in the array". All it does it add 1 to the values I mentioned then exits. It never checks the array for any values at all.

In other word, if I want to check if my array list has the value 20 in it, testing using your technique I'd have:

i = 0;
while (i < TotalValues)
{
    i++;
}

Where is the test for 20? Or in your case, the Lab number?

It does though because in the allocation and intialization I made the last element a NULL so this makes it so that while rowSteps != NULL ++.

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;
    }
}

Yes, what you just claimed is correct. Nevertheless, the function doesn't do what you claim it does. Please show me the exact statement(s) that are used to

see if the user inputed a lab number thats in the array.

How is it actually testing the lab number and what is it testing with? In order to see something you have to look at something.

I think he is using the index of the row as the course number. It kind of looks that way from the way they ask for the course number but it should definitely be spelled out if that is what they are doing.

I'm testing it with a rowWalker and a rowStep. rowWalker is set to the "first dimension" of the twoD dynamic Array and rowStep just makes sure that we dont go past the row entered.

See my previous post and do what I ask. I claim you are not testing the lab. Prove me wrong instead and show me (don't tell, show) exactly how you are testing and why it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.