5 Replies - 347 Views - Last Post: 17 October 2019 - 05:47 PM Rate Topic: -----

#1 Deadweight77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 19

C++ computer sign-in/sign-out

Posted 17 October 2019 - 02:50 PM

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 crashes. Forgive me but this assignment I've been working on has been a lot for me.

#include <iostream>
using namespace 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] = new int[lab_slots[i]];//creating lab with labslots[i]
		for (int j = 0;j < lab_slots[i];)/>//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);
}



Is This A Good Question/Topic? 0
  • +

Replies To: C++ computer sign-in/sign-out

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15356
  • View blog
  • Posts: 61,566
  • Joined: 12-June 08

Re: C++ computer sign-in/sign-out

Posted 17 October 2019 - 03:01 PM

029        for (int j = 0;j < lab_slots[i]; )

missing the j increment?


015    int *lab[4];

Also make sure to instantiate your array else the computer will try and grab what ever garbage memory it can find.
Was This Post Helpful? 0
  • +
  • -

#3 Deadweight77   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 04-May 19

Re: C++ computer sign-in/sign-out

Posted 17 October 2019 - 03:22 PM

Thanks for the reply, but what should I do exactly with that array? Sorry if it's a dumb question I've been a little all over the place trying to see how and if it works.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15356
  • View blog
  • Posts: 61,566
  • Joined: 12-June 08

Re: C++ computer sign-in/sign-out

Posted 17 October 2019 - 03:59 PM

See about how to initialize arrays.

http://www.cplusplus...utorial/arrays/
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,230
  • Joined: 05-May 12

Re: C++ computer sign-in/sign-out

Posted 17 October 2019 - 05:38 PM

Also, it looks like in your code you have the lab_slots member variable, and then you also have the lab_slots local variable found on line 48 that you seem to be passing around everywhere as the lab_slots parameter. I suggest choosing what you to do: use the member variable or just always pass it around.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7132
  • View blog
  • Posts: 24,230
  • Joined: 05-May 12

Re: C++ computer sign-in/sign-out

Posted 17 October 2019 - 05:47 PM

Also, if you are writing C++ code, use C++ code. The modern way of teaching and using C++ has you using std::vector<> instead of dynamically allocated arrays. It's only later that dynamic allocated arrays are introduced so that you would understand older C++ code.

Why does this matter in this case? It matters because you could simply use:
std::vector<std::vector<int>> labs[4];


Instead of your current:
int * labs[4];


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1