-5

array keeps bringing garbage value while it should be empty.following is my

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

problem is with the following lines

        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] is empty but room.roomId[2][i] and room.roomId[3][i] brings some garbage value. i could share my whole source code if u need.

this array is supposed to take values from another variable but how can it takes values without assigning it to him.

  • 1
    where is the declaration of "room" – Humam Helfawi Aug 24 '16 at 07:56
  • 1
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Aug 24 '16 at 07:59
  • show the code where you set value into roomId. – Shahid Aug 24 '16 at 08:13
  • @HumamHelfawi "room" is a struct variable. let me update whole program. then tell me – Raza Zaidi Aug 24 '16 at 08:14
  • code is updated. Kindly please help me with this. – Raza Zaidi Aug 24 '16 at 08:18
  • @Shahid values for roomId is set in the function roomType() – Raza Zaidi Aug 24 '16 at 08:20
  • All your array indexing is one-based. I think you should start [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Aug 24 '16 at 08:22

2 Answers2

1

You have declared int roomId[3][10], but trying to access roomId[3][10]. That's why you are getting garbage values as you can access only from 0 to (size-1) index. The lastmost roomId should be roomId[2][9].

You can increase the size of array in declaration.

int roomId[4][11];

Or,

Instead of using array index from 1 to 10, use 0 to 9.

Shahid
  • 2,288
  • 1
  • 14
  • 24
0

Just throwing this out there since I was confused. In my for loops filling my 2d array I switched the rows and cols i.e: i and j. So when I went to read them i was getting what looked like good output for the first half, then trash second half.