2016-08-24 106 views
-5

陣列保持使垃圾值而應當empty.following是我2D陣列將垃圾值

 /* 
    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] [我]是空的,但是room.roomId [2] [i]和room.roomId [3] [i]帶來了一些垃圾值。如果你需要,我可以分享我的整個源代碼。

這個數組應該從另一個變量中獲取值,但是如何將值分配給他而不取值。

+1

哪裏是「房間」 –

+1

解決這樣的問題的合適的工具的聲明是調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –

+0

顯示您將值設置爲roomId的代碼。 – Shahid

回答

1

您已聲明int roomId[3][10],但試圖訪問roomId[3][10]。這就是爲什麼你得到垃圾值,因爲你只能從0到(大小-1)索引訪問。最後一個roomId應該是roomId[2][9]

您可以在聲明中增加數組的大小。

int roomId[4][11]; 

或者,

代替使用數組索引從1至10的,用0到9

+0

我沒有明白。你能解釋一下嗎? –

+0

'int roomId [4] [11];'清理垃圾問題。 :) –