2d数组带来垃圾值

时间:2016-08-24 07:54:26

标签: c++ arrays loops multidimensional-array

数组保持带来垃圾值,而它应该是空的。以下是我的

     /*
    Name: Hotel Room Reservation 
    Author: Ali Naseem, M. Abbas, M. Hammad, Raza Zaidi
    Date: 23-08-16 01:20
    Description: This software is about reserving a room in hotel. User will provide its information and his requirement after which software will reserve a room for him. 
*/

    #include <iostream>
    #include <cstring>
    using namespace std;

    int n=0,j=0,k=0,l=0,c=1,r=1;

    void customer();
        void roomType();

    void admin();
        void status();
        void bill();

    struct credentials{
        string user, pass, USER, PASS;
        }cr = {} ;



    struct rooms         //Stucture Variables for Room Details
    {
        int roomT, noRoom[30], confirm=0;
        int roomId[3][10]={{}};
        int singleR[10] = {101,102,103,104,105,106,107,108,109,110};
        int doubleR[10] = {111,112,113,114,115,116,117,118,119,120};
        int suitR[10]   = {121,122,123,124,125,126,127,128,129,130};
    } room;

    struct custDet       //Stucture Variables for Customer Details
    {
        string name[30]={""};
    int cnic[30], ID[30], noPer[30], day[30];   

} guest;


void customer()      //Customer Module 
{
    bool keep=true;
    int opt;
    while(keep)
    {

        if(k>10 && j>10 && l>10)
        {
            cout<<"\nSorry We dont have any room available";
            goto skipCust;
        }



        cout<<"\nEnter Your Full Name: ";
        cin.ignore();
        getline(cin,guest.name[n]);

        cout<<"\nEnter Your CNIC Number: ";
        cin>>guest.cnic[n];

        cout<<"\nNumber Of Rooms Required: ";
        cin>>room.noRoom[n];

        roomType();
        if(room.roomT==0)
        {
            goto skipCust;
        }

        cout<<"\n\nPlease Enter The Numbers of Persons Will Stay: ";
        cin>>guest.noPer[n];

        cout<<"\nNumber of Days To Stay: ";
        cin>>guest.day[n];  

        invalid:
        cout<<"\n\nPress 1 To Add More Coustomers or 0 To Go To Previous Menu: ";
        cin>>room.confirm;
        if(room.confirm==1)
        {
            keep=true;
        }
        else if(room.confirm==0)
        {
            keep=false;
        }
        else
        {
            cout<<"'\nInvalid Selection.";
            goto invalid;
        }
    }
    skipCust:;
}

void roomType()             //Reservation Module
{
    bool keep=true;

    while(keep)
    {
        cout<<"\nFollowing is the list of types of Room:"<<endl<<endl;
        cout<<"Code\tRoom Type  \tFares (Per Day)"<<endl;
        cout<<"1-  \tSingle Room\t1000"<<endl;
        cout<<"2-  \tDouble Room\t1500"<<endl;
        cout<<"3-  \tSuit       \t3000"<<endl<<endl;
        cout<<"(Press 0 to go back)"<<endl;
        cout<<"\nSelect Your Room Type: ";
        cin>>room.roomT;


        if(room.roomT==1)
        {
            room.roomId[1][c]=room.singleR[j];
            cout<<"\nYour Room No. is: "<<room.roomId[1][c];
            n++;
            j++;
            c++;
            keep=false;
        }
        else if(room.roomT==2)
        {
            room.roomId[2][c]=room.doubleR[k];
            cout<<"\nYour Room No. is: "<<room.roomId[2][c];
            n++;
            k++;
            c++;
            keep=false;
        }
        else if(room.roomT==3)
        {
            room.roomId[3][c]=room.singleR[l];
            cout<<"\nYour Room No. is: "<<room.roomId[3][c];
            n++;
            l++;
            c++;
            keep=false;

        }
        else if(room.roomT==0)
        {
            keep=false;
        }
        else
        {
            cout<<"\nInvalid Selection";
        }
    }
}




void admin()        //Admin Module
{
    int opt;
    bool keep=true;
    if(cr.USER.empty())   //To set Login credentials
    {
        cout<<"\nSIGN UP";

        cin.ignore();
        cout<<"\nUser: ";
        getline(cin,cr.user);
        cr.USER=cr.user;

        cout<<"\nPass: ";
        getline(cin,cr.pass);
        cr.PASS=cr.pass;
    }
    else                 //If Login credentials are already set
    {
        cout<<"\nSIGN IN";
        cin.ignore();
        cout<<"\nUser: ";
        getline(cin,cr.user);
        cout<<"\nPass: ";
        getline(cin,cr.pass);
    }

    if((cr.user==cr.USER) && cr.pass==cr.PASS)
    {
        cout<<"\nSigned In Successfully";
    }
    else
    {
        cout<<"\nusername or password is incorrect Try Again";

    }

    while(keep)
    {
        cout<<"\nRooms Status     \t(Press 1)";
        cout<<"\nBill             \t(Press 2)";
        cout<<"\nFor Previous Menu\t(Press 0)"<<endl;
        cout<<"Choice: ";
        cin>>opt;
        if(opt==1)
        {
            status();
        }
        else if (opt==2)
        {
            bill();
        }
        else if (opt==0)
        {
            keep=false;
        }
        else
        {
            cout<<"\nInvalid Selection";
            keep=true;
        }
    }
}

void status()           //Status Module
{
    int opt;
    bool keep=true;
    while(keep)
    {
        cout<<"\nSelect the room for there status:";
        cout<<"\nCode\tRoom Type"<<endl;
        cout<<"1-  \tSingle Room"<<endl;
        cout<<"2-  \tDouble Room"<<endl;
        cout<<"3-  \tSuit       "<<endl<<endl;
        cout<<"(Press 0 to go back)"<<endl;
        cout<<"\nSelect Your Room Type: ";
        cin>>opt;

        if(opt==1)
        {
            cout<<"\nRoom\tStatus\n";
            for(int i=1; i<=10; i++ )
            {
                if(room.roomId[1][i]>0)
                {
                    cout<<room.roomId[1][i]<<"\t Occupied\n";
                }
            }
        }
        else if(opt==2)
        {
            cout<<"\nRoom\tStatus\n";
            for(int i=1; i<=10; i++ )
            {
                if(room.roomId[2][i]>0)
                {
                    cout<<room.roomId[2][i]<<"\t Occupied\n";
                }
            }
        }
        else if(opt==3)
        {
            cout<<"\nRoom\tStatus\n";
            for(int i=1; i<=10; i++ )
            {
                if(room.roomId[3][i]>0)
                {
                    cout<<room.roomId[3][i]<<"\t Occupied\n";
                }
            }
        }
        else if(opt==0)
        {
            keep=false;
        }
        else
        {
            cout<<"\n Invalid Choice";
            keep=true;
        }
    }
}

void bill()
{
    int opt;
    cout<<"\nThis is Bill Module";
    cout<<"\nPress 0 to go back to previous menu";
    cin>>opt;
}

int main()
{
    int opt;
    bool keep = true;
    while(keep)
    {
    cout<<"\n*************************"<<endl;
    cout<< " Hotel Room Reservation "<<endl;
    cout<<"*************************"<<endl;

    cout<<"Customer (Press 1)"<<endl;
    cout<<"Admin    (Press 2)"<<endl;
    cout<<"Exit     (Press 0)"<<endl;
    cout<<"Choice: ";
    cin>>opt;




    if(opt==1)
    {
        customer();

    }
    else if(opt==2)
    {
        admin();

    }
    else if(opt==0)
    {
        cout<<"Invlid Selection"<<endl;
        keep=false;
    }
    else
    {
        cout<<"Invalid Selection"<<endl;
    }


    }

    cout<<endl;
    return 0;
}

问题在于以下几行

        if(opt==1)
        {
            cout<<"\nRoom\tStatus\n";
            for(int i=1; i<=10; i++ )
            {
                if(room.roomId[1][i]>0)
                {
                    cout<<room.roomId[1][i]<<"\t Occupied\n";
                }
            }
        }
        else if(opt==2)
        {
            cout<<"\nRoom\tStatus\n";
            for(int i=1; i<=10; i++ )
            {
                if(room.roomId[2][i]>0)
                {
                    cout<<room.roomId[2][i]<<"\t Occupied\n";
                }
            }
        }
        else if(opt==3)
        {
            cout<<"\nRoom\tStatus\n";
            for(int i=1; i<=10; i++ )
            {
                if(room.roomId[3][i]>0)
                {
                    cout<<room.roomId[3][i]<<"\t Occupied\n";
                }
            }
        }

room.roomId [1] [i]是空的但是room.roomId [2] [i]和room.roomId [3] [i]带来了一些垃圾值。如果你需要,我可以分享我的整个源代码。

这个数组应该从另一个变量中取值,但是如果没有赋值给它,它怎么能取值。

1 个答案:

答案 0 :(得分:1)

您已声明int roomId[3][10],但尝试访问roomId[3][10]。这就是您获取垃圾值的原因,因为您只能访问从0到(size-1)索引。最后一个roomId应为roomId[2][9]

您可以在声明中增加数组的大小。

int roomId[4][11];

或者,

不使用1到10之间的数组索引,而是使用0到9。