使用函数来填充数组中的数组。如果数组已满,则数字将填充在第二个数组上

时间:2015-10-07 03:29:26

标签: c++ arrays

我在一个名为registered的数组中注册了一个程序存储注册的学生id。然后,如果该数组已满,则会自动将学生ID存储在另一个名为waitlist的数组中。这个动作将通过一个函数完成。 当我的注册阵列已满时,我无法让我的计算机将学生ID存储到waitlist数组中。我只能使用特定的函数将存储的学生ID存入两个数组。我还需要使用相同的功能打印已注册和等候名单中的学生ID列表。 我不知道该怎么做,我找不到一个好的教程,书甚至都没有帮助。谢谢。

这是我的代码。

#include <iostream>

using namespace std;

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

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

    int reg[maxCap];
    int waitlist[maxCap];
    int option, value, capacity;
    int howMany = 0;
    int registered = 0;
    int waitlisted = 0;
    do
    {
        showOption();

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


        switch (option)
        {
        case 1:
            if(isEmpty(capacity, howMany) == true)
            addToRear(reg, howMany, maxCap);
            if(isFull(capacity, howMany) == true)
            addToRear(waitlist, howMany, maxCap);
            break;
        case 3:
            printArray(reg, howMany);
            break;
        case 4:
            printArray(waitlist, howMany);
            break;

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

            return 0;
            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)

//enter student id and store it in array register and waitlist
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;
    }


}

//print list of student that is registered or waitlisted
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;
    }
}

bool isFull(int capacity, int howMany)
{
/if((capacity < 1) || (howMany > 9))
        return true;
    else
        return false;
}

bool isEmpty(int capacity, int howMany)
{
    if((capacity > 9) || (howMany < 1))
        return true;
    else
        return false;
}

2 个答案:

答案 0 :(得分:0)

  1. 让单个数组同时包含已注册和等待列出的候选项会更容易。
  2. 添加时你只需拥有一个结束索引。因此,只要指数小于您的候选人的截止数,就会注册。
  3. 删除时,您必须在删除一个候选条目后将列表向上移动一个空格。这样,注册列表将自动更新,以将候补候选人减少1。
  4. 打印您必须检查索引是否大于截止时您已注册和等待列表。

答案 1 :(得分:0)

您的代码无法识别注册列表和等待列表的完整和空白条件。我在下面的代码中做了一些修改。你可以尝试下面的代码片段。

case 1:
            int len = sizeof(reg)/sizeof(reg[0]); // Get the length of reg list
            int waitlistLen = sizeof(waitlist)/sizeof(waitlist[0]); //get length of wait list
            if(maxCap > len)               // Check if reg list not full. Add to reg list
                addToRear(reg, howMany, maxCap);
            else if(maxCap > waitlistLen) // if reg list is full. Check if wait list is full or not
                addToRear(waitlist, howMany, maxCap);
            else                         // if both the list are full show user option menu and break.
                showOption();
            break;