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????
LVL 4
bobby101Asked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
First of all, you should close myfile after you finished reading it. And before you start writing to it again using the outputFile stream.

Second, I see only one location where you modify the contents of the cabin array :

>>    cabin [row][column][level] = 'b';

So, after running your code, the character at the given position (row, column and level entered by the user) in the file should be a 'b'.

If that is not the case, could you please post the complete code (including main etc.) ? Could you also post the output you get when running the code ?
0
 
Infinity08Commented:
>> int cabin[13][4][3];

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.
0
 
bobby101Author Commented:



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";
                }

Open in new window

roomBookings.txt
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
bobby101Author Commented:
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]
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:";
      
}

Open in new window

0
 
Infinity08Commented:
>> 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 ?
0
 
itsmeandnobodyelseCommented:
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.
0
 
bobby101Author Commented:
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
0
 
bobby101Author Commented:
YEAH!!!! i made it work by the if and else if statements but i bet ill have problems doing the ticket lol thanks guys!!!
0
 
itsmeandnobodyelseCommented:
>>>> i made it work by the if and else if statements

Why not by

    cabin [row][column][level] = tolower(cabin [row][column][level]);

?
0
 
bobby101Author Commented:
Please elaborate
>>>>cabin [row][column][level] = tolower(cabin [row][column][level]);
0
 
itsmeandnobodyelseCommented:
>>>> 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.
0
All Courses

From novice to tech pro — start learning today.