partially filled array.

Write your question here.
hi. I am new with programming and I stuck on this project.
This project has 4 option:
1. register student
2. unregister student
3. print registered list
4. print waitlist.
(ignore unregister and print waitlist for now)

the way I write this program is I ask for user input to enter student Id number to be stored in array until user enter negative number.

once user enter negative number. the program will ask the user if he wants to go back to main menu (the 4 option above).

here is my problem.
Somehow, my array that contain registered student id is not working properly.

let say at first, I enter 2 student id into the array, the I go back to main menu.
then I enter 3 student id into the array, then go back to main menu.
(so right now I supposed to have 5 student id number in my array)

when I choose menu "print registered list.", its only print 3 registered student, the other 2 that is being registered earlier is not exist anymore.

I wonder why is that? I really don't know what to do anymore, I cant even find any good tutorial on google. I really appreciate anyone who can show me what did I do wrong.




#include <iostream>

using namespace std;

const int maxCap = 10;
void showOption();
void addToRear(int arr[], int& howMany, int value);
int removeFromFront(int arr[], int& howMany);
int remove(int arr[], int& howMany, int value);
void printArray(int arr[], int howMany);
bool isFull(int capacity, int howMany);
bool isEmpty(int capacity, int howMany);
void getID(int& value);



int main()
{
bool repeat = false;
bool repeatOption = false;

int reg[maxCap];
int waitlist[maxCap];

int option, howMany, value, capacity;


do
{
showOption();

cout << "please enter your option: 1 ~ 5 " << endl;
cin >> option;


switch (option)
{
case 1:

addToRear(reg, howMany, maxCap);


break;
case 3:
printArray(reg, howMany);
break;

case 5:
cout << "thank you for using this program. " << endl;
break;

default:
cout << "thank you for using this program. " << endl;
break;
}

//cout << "there are " << howMany << " registered student in this class. " << endl;

cout << "back to main menu? enter 1 for yes: " << endl;
cin >> repeatOption;
}while(repeatOption);
}

void showOption()
{
cout << "Main Menu: " << endl;
cout << "1) Register Student. " << endl;
cout << "2) Unregister Student. " << endl;
cout << "3) Print list of registered student. " << endl;
cout << "4) Print list of waitlisted student. " << endl;
cout << "5) Exit. " << endl;
cout << " " << endl;
}
//void getID(int& value)


void addToRear(int arr[], int& howMany, int value)
{
cout << "there are only " << maxCap << " spot for student to be registered." << endl;
cout << "if there are " << maxCap << " students registered in this class, " << endl;
cout << "then the rest of the student will be put on waitlist. " << endl;
cout << " " << endl;
cout << "enter student ID. " << endl;
cout << "enter negative value to stop. " << endl;

int id, size = 0;

cin >> id;

while((id >= 0) && (size < maxCap))
{
arr[size] = id;
size++;
cin >> id;
}

howMany = size;
}

void printArray(int arr[], int howMany)
{
cout << "there are " << howMany << " number of student in registered list. " << endl;
cout << "the student ID number is " << endl;

for (int index = 0; index < howMany; index++)
{
cout << arr[index];
cout << " " << endl;
}
}
Last edited on
The problem is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void addToRear(int arr[], int& howMany, int value)
{
cout << "there are only " << maxCap << " spot for student to be registered." << endl;
cout << "if there are " << maxCap << " students registered in this class, " << endl;
cout << "then the rest of the student will be put on waitlist. " << endl;
cout << " " << endl;
cout << "enter student ID. " << endl;
cout << "enter negative value to stop. " << endl;

int id, size = 0; // You use size as an index

cin >> id;

while((id >= 0) && (size < maxCap))
{
arr[size] = id; // size starts with 0 hence you start from 0 whenever you call addToRear
size++;
cin >> id;
}

howMany = size; // reset howMany
}
One solution could be: int id, size = howMany;.

Or you use howMany instead of size.
coder777. Thank you for your explanation. it really help me understand what is going on.

now my code for adding student is this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void addToRear(int arr[], int& howMany, int value)
{
	cout << "there are only " << maxCap << " spot for student to be registered." << endl;
	cout << "if there are " << maxCap << " students registered in this class, " << endl;
	cout << "then the rest of the student will be put on waitlist. " << endl;
	cout << " " << endl;
	cout << "enter student ID. " << endl;
	cout << "enter negative value to stop. " << endl;

	int id;
	cin >> id;

	while((id >= 0) && (howMany < maxCap))
	{
		arr[howMany] = id;
		howMany++; 
		cin >> id;
	}


how the other problem will be how to assign student that try add the class into waitlist.

this piece of code works fine as long the array for my registered student is not up to 10.

once my reg[] is filled by 10 student id, whenever i choose menu 1 ( register a student), it keep repeating the same menu option from 1 to 5.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
case 1:
				//cout << "Enter a student id" << endl;
				int student_id;
				//cin >> student_id;
				if (howmany < MAX_SIZE) {
					//cout << "adding to class" << endl;
					addToRear(registered_student, howmany, MAX_SIZE);
				}
				if (isEmpty == false) {
					//cout << "adding to waitlist" << endl;
					addToRear(waitlisted_student, howmany, MAX_SIZE);
				}
				//else {
					//cout << "No space in either class nor waitlist" << endl;
				//}
				break;
			


this is my switch case for adding student
thank you.
Last edited on
You cannot use howmany for both registered_student and waitlisted_student. For each list you need another variable.

I would guess that anything after line 5 should not be executed if the count exceeds the capacity. And further I guess that you should use the third parameter of addToRear(...) to check wether the limit is actually exceeded.

I am totally lost. If i can use another function to store the number in array waitlist, it is not a problem. but i have to use the same function.
the book that i use doesn't even explain it very well or give a good example.
Last edited on
It's not that complicated:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int howmany = 0;
int registered_howmany = 0;
int waitlisted_howmany = 0;

...

case 1:
				//cout << "Enter a student id" << endl;
				int student_id;
				//cin >> student_id;
				if (howmany < MAX_SIZE) {
					//cout << "adding to class" << endl;
					addToRear(registered_student, registered_howmany, MAX_SIZE); // Note: for each array another 'howmany'
				}
				if (isEmpty == false) {
					//cout << "adding to waitlist" << endl;
					addToRear(waitlisted_student, waitlisted_howmany, MAX_SIZE); // Note: for each array another 'howmany'
				}
				//else {
					//cout << "No space in either class nor waitlist" << endl;
				//}
				break;
Last edited on
Topic archived. No new replies allowed.