bobby101
asked on
3D array reading and writing into *.txt files
I need help with 3d arrays and how update and read from text files.
there a booking sytem it have 3 level 12rows and 3 column
Each level has 52 cabins with the following configuration:
1. ten outside cabins with balcony
2. sixteen outside cabins with window
3. twenty-six inside cabins.
ie
B I I B
B I I B
B I I B
B I I B
B I I B
W I I W
W I I W
W I I W
W I I W
W I I W
W I I W
W I I W
W I I W
this is what i had:
int cabin[13][4][3];
int x=0;
for (int i=0;i<=12;i++){
for(int j=0;j<=4;j++){
for (int k=0;k<=2;k++){
cabin[i][j][k]=x++;
cout<<cabin[i][j][k];
}
}
cout<<endl;
}
now i need to know well i can write this into a txt file but i need to know how to a users' selection of the level 0-2, row 0-12 ans column 0-3 of the room and write that into a txt file????
there a booking sytem it have 3 level 12rows and 3 column
Each level has 52 cabins with the following configuration:
1. ten outside cabins with balcony
2. sixteen outside cabins with window
3. twenty-six inside cabins.
ie
B I I B
B I I B
B I I B
B I I B
B I I B
W I I W
W I I W
W I I W
W I I W
W I I W
W I I W
W I I W
W I I W
this is what i had:
int cabin[13][4][3];
int x=0;
for (int i=0;i<=12;i++){
for(int j=0;j<=4;j++){
for (int k=0;k<=2;k++){
cabin[i][j][k]=x++;
cout<<cabin[i][j][k];
}
}
cout<<endl;
}
now i need to know well i can write this into a txt file but i need to know how to a users' selection of the level 0-2, row 0-12 ans column 0-3 of the room and write that into a txt file????
ASKER
void Cruise::showAvailability()
int level, row, column, passenagers, cruiseNumber;
char seat;
// cout<<"Select Cruise Number (0-51): ";
// cin>>cruiseNumber;
cout<<"Select Level (0-2): ";
cin>>level;
cout<<"Select Row (0-12): ";
cin>>row;
cout<<"Select Column (0-3): ";
cin>>column;
// picks up row
ifstream myfile ("roomBookings.txt");
if (myfile.fail()) cout<<"Unable to read File";
char cabin [13][4][3];
for (int i=0; i<13;i++) //rows(should be 12 in the full program)
for(int j=0; j<4;j++)//column
for(int k=0; k<3;k++) //Levels
myfile>>cabin [i][j][k];
complete code addatched i kwon how to write now but its not allowing me to write to the correct position with the correct letter, i have tried if and switch but its only writing to the cabin [0][0][0]. ill attach the txt file as wel
void Cruise::showAvailability(){
int level, row, column, passenagers, cruiseNumber;
char seat;
// cout<<"Select Cruise Number (0-51): ";
// cin>>cruiseNumber;
cout<<"Select Level (0-2): ";
cin>>level;
cout<<"Select Row (0-12): ";
cin>>row;
cout<<"Select Column (0-3): ";
cin>>column;
// picks up row
ifstream myfile ("roomBookings.txt");
if (myfile.fail()) cout<<"Unable to read File";
char cabin [13][4][3];
for (int i=0; i<13;i++) //rows(should be 12 in the full program)
for(int j=0; j<4;j++)//column
for(int k=0; k<3;k++) //Levels
myfile>>cabin [i][j][k];
seat = cabin [row][column][level];
cout << "\n\n\n";
if (seat == 'B' || seat =='I' || seat=='W'){
cout <<seat << " Room Available\n";
//writing into the file
/*
noe trying to change B to b, I to i and W to w
uppercase available and lower case un available
*/
cabin [row][column][level] = 'b';
ofstream outputFile("roomBookings.txt");
for (int i=0; i<13;i++){
for(int j=0; j<4;j++)//column
for(int k=0; k<3;k++) //Levels
outputFile << cabin[i][j][k];
outputFile << "\n";
}
outputFile.close();
//max 0f 4 passenages
cout<<"Number of Passenagers: ";
cin>>passenagers;
while(passenagers > 4){
cout<< "***ONLY 4 PASSENAGERS PER CABIN***\n";
cout<<"Number of Passenagers: ";
cin>>passenagers;
}
}
else{
cout <<"Unavailable";
}
roomBookings.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
the program so far...
i know the one location of cabin [row][column][level] = 'b';
i have tried to do if and switch statements for the different room types but only change the [0][0][0] location
lets say if a user selects cabin[1][3][0] and its a I than i placed cabin [row][column][level] = 'i'; but this seems to not wrking in the txt file it changes the cabin[0][0][0]
i know the one location of cabin [row][column][level] = 'b';
i have tried to do if and switch statements for the different room types but only change the [0][0][0] location
lets say if a user selects cabin[1][3][0] and its a I than i placed cabin [row][column][level] = 'i'; but this seems to not wrking in the txt file it changes the cabin[0][0][0]
class Cruise {
private:
// const int MAXBALCONY, MAXWINDOW, MAXINSIDE;
public:
void readInfo();
void writeInfo();
void showAvailability();
void reserveCabin();
};
void Cruise::writeInfo(){
// enter the bookings
}
// reads cabinConfig the basic layout of floor plan of ship with cabin types
void Cruise::readInfo(){
string layout;
ifstream myfile ("cabinConfig.txt");
if (myfile.is_open()){
while (! myfile.eof() ){
getline (myfile,layout);
myfile;
cout << layout << endl;
}
myfile.close();
}
else {
cout << "Unable to open file";
}
}
// user selects level, shows what available
void Cruise::showAvailability(){
int level, row, column, passenagers, cruiseNumber;
char seat;
// cout<<"Select Cruise Number (0-51): ";
// cin>>cruiseNumber;
cout<<"Select Level (0-2): ";
cin>>level;
cout<<"Select Row (0-12): ";
cin>>row;
cout<<"Select Column (0-3): ";
cin>>column;
// picks up row
ifstream myfile ("roomBookings.txt");
if (myfile.fail()) cout<<"Unable to read File";
char cabin [13][4][3];
for (int i=0; i<13;i++) //rows(should be 12 in the full program)
for(int j=0; j<4;j++)//column
for(int k=0; k<3;k++) //Levels
myfile>>cabin [i][j][k];
seat = cabin [row][column][level];
cout << "\n\n\n";
if (seat == 'B' || seat =='I' || seat=='W'){
cout <<seat << " Room Available\n";
//writing into the file
/*
noe trying to change B to b, I to i and W to w
uppercase available and lower case un available
*/
cabin [row][column][level] = 'b';
ofstream outputFile("roomBookings.txt");
for (int i=0; i<13;i++){
for(int j=0; j<4;j++)//column
for(int k=0; k<3;k++) //Levels
outputFile << cabin[i][j][k];
outputFile << "\n";
}
outputFile.close();
//max 0f 4 passenages
cout<<"Number of Passenagers: ";
cin>>passenagers;
while(passenagers > 4){
cout<< "***ONLY 4 PASSENAGERS PER CABIN***\n";
cout<<"Number of Passenagers: ";
cin>>passenagers;
}
}
else{
cout <<"Unavailable";
}
/* continue with ticket and writing into txt file for user with increament
fare total here on page 3
ticket number 10001 etc to a file for later checking by user.
each ticket is a individual file
this file containes Name of user, cruise number, cabin location, cabin type
number of passengers.
*/
cout << "TICKET NUMBER:";
}
>> i have tried to do if and switch statements for the different room types but only change the [0][0][0] location
>> lets say if a user selects cabin[1][3][0] and its a I than i placed cabin [row][column][level] = 'i'; but this seems to not wrking in the txt file it changes the cabin[0][0][0]
I can't find any sign of that in the code you posted ... Unless I'm overlooking something ?
Could you please post the ENTIRE code (all files, including the main function, ie. compilable into an executable), and make sure that it is the code that has the problem you describe.
Could you also post the output you get when you run that code and observe the problem you describe ?
>> lets say if a user selects cabin[1][3][0] and its a I than i placed cabin [row][column][level] = 'i'; but this seems to not wrking in the txt file it changes the cabin[0][0][0]
I can't find any sign of that in the code you posted ... Unless I'm overlooking something ?
Could you please post the ENTIRE code (all files, including the main function, ie. compilable into an executable), and make sure that it is the code that has the problem you describe.
Could you also post the output you get when you run that code and observe the problem you describe ?
I made a roaming.txt which was
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIIIIIIBBB
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
(Note, there must no trailing spaces in no line)
Then, I tried your program and typed Level 1, Row 4 and Column 1.
My file now was
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIbIIIIBBB
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
what looks quite good (beside that it should be a 'i' rather than a 'b').
Nevertheless you should close your inputfile after reading it as Infinity already told you. It worked nevertheless with my compiler but there might be other compilers which can't open a file for output (and truncate it) as long as it was still opened as inputfile.
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIIIIIIBBB
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
(Note, there must no trailing spaces in no line)
Then, I tried your program and typed Level 1, Row 4 and Column 1.
My file now was
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIIIIIIBBB
BBBIbIIIIBBB
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
WWWIIIIIIWWW
what looks quite good (beside that it should be a 'i' rather than a 'b').
Nevertheless you should close your inputfile after reading it as Infinity already told you. It worked nevertheless with my compiler but there might be other compilers which can't open a file for output (and truncate it) as long as it was still opened as inputfile.
ASKER
Yeah well that what i'm trying to do so if the have picked a cabin type W [note: W id available and w unavailable] than i wanted it to switch or if statement that cabin[row][column][level] to equal to the that cabin type ie W and convert it into lowercase w
ASKER
YEAH!!!! i made it work by the if and else if statements but i bet ill have problems doing the ticket lol thanks guys!!!
>>>> i made it work by the if and else if statements
Why not by
cabin [row][column][level] = tolower(cabin [row][column][level]);
?
Why not by
cabin [row][column][level] = tolower(cabin [row][column][level]);
?
ASKER
Please elaborate
>>>>cabin [row][column][level] = tolower(cabin [row][column][level]);
>>>>cabin [row][column][level] = tolower(cabin [row][column][level]);
>>>> Please elaborate
>>>> >>>>cabin [row][column][level] = tolower(cabin [row][column][level]);
The tolower function would turn capital letters to small letters, so a 'B' goes to 'b', a 'W' to 'w' and a 'I' to 'i'. As far as I understood that is what you wanted to do in case of a booking.
>>>> >>>>cabin [row][column][level] = tolower(cabin [row][column][level]);
The tolower function would turn capital letters to small letters, so a 'B' goes to 'b', a 'W' to 'w' and a 'I' to 'i'. As far as I understood that is what you wanted to do in case of a booking.
Since you said the dimensions are 3x12x3, why did you choose 13 and 4 for the first two ?
>> for (int i=0;i<=12;i++){
>> for(int j=0;j<=4;j++){
>> for (int k=0;k<=2;k++){
Make sure that your loop termination condition matches the dimensions of the array ! It's best to use < rather than <=, since it makes things clearer :
for (int i = 0; i < 12; ++i) {
for (int j = 0; j < 3; ++j) {
for (int k = 0; k < 3; ++k) {
(for a 12x3x3 array)
>> now i need to know well i can write this into a txt file but i need to know how to a users' selection of the level 0-2, row 0-12 ans column 0-3 of the room and write that into a txt file????
What do you mean ? You can ask the user what level, row and column he wants, and then use those 3 values as indexes into the array.