数组在应该为空的情况下一直产生垃圾值。以下是我的
/*
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<<"nnPlease Enter The Numbers of Persons Will Stay: ";
cin>>guest.noPer[n];
cout<<"nNumber of Days To Stay: ";
cin>>guest.day[n];
invalid:
cout<<"nnPress 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<<"CodetRoom Type tFares (Per Day)"<<endl;
cout<<"1- tSingle Roomt1000"<<endl;
cout<<"2- tDouble Roomt1500"<<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 Menut(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<<"nCodetRoom 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<<"nRoomtStatusn";
for(int i=1; i<=10; i++ )
{
if(room.roomId[1][i]>0)
{
cout<<room.roomId[1][i]<<"t Occupiedn";
}
}
}
else if(opt==2)
{
cout<<"nRoomtStatusn";
for(int i=1; i<=10; i++ )
{
if(room.roomId[2][i]>0)
{
cout<<room.roomId[2][i]<<"t Occupiedn";
}
}
}
else if(opt==3)
{
cout<<"nRoomtStatusn";
for(int i=1; i<=10; i++ )
{
if(room.roomId[3][i]>0)
{
cout<<room.roomId[3][i]<<"t Occupiedn";
}
}
}
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<<"nRoomtStatusn";
for(int i=1; i<=10; i++ )
{
if(room.roomId[1][i]>0)
{
cout<<room.roomId[1][i]<<"t Occupiedn";
}
}
}
else if(opt==2)
{
cout<<"nRoomtStatusn";
for(int i=1; i<=10; i++ )
{
if(room.roomId[2][i]>0)
{
cout<<room.roomId[2][i]<<"t Occupiedn";
}
}
}
else if(opt==3)
{
cout<<"nRoomtStatusn";
for(int i=1; i<=10; i++ )
{
if(room.roomId[3][i]>0)
{
cout<<room.roomId[3][i]<<"t Occupiedn";
}
}
}
的房间。roomId[1][i]是空的,但房间。roomId[2][i]和room。roomId[3][i]带来一些垃圾值。如果你需要,我可以分享我的整个源代码。
这个数组应该从另一个变量中获取值,但是它怎么能在不给他赋值的情况下获取值呢?
您已声明int roomId[3][10]
,但试图访问roomId[3][10]
。这就是为什么您得到的是垃圾值,因为您只能从0到(size-1)索引访问。最后一个roomId应该是roomId[2][9]
。
可以在声明中增加数组的大小。
int roomId[4][11];
或
不使用数组索引从1到10,而使用0到9
我只是把这个扔在那里,因为我很困惑。在我的for循环填充我的2d数组中,我交换了行和颜色,即:I和j。所以当我去读取它们时,我得到了前一半看起来不错的输出,然后丢弃了后一半。