Hi,

my goal is to implement a program (in OO style) for cruise booking system that can accept and maintain customers’ reservation. Whenever a customer visits, that program should show him/her the current availablility of cabins. A customer can choose any available cabin with specified level (0 – 2, down to up), row (0-3, left to right) and cabin number (0-12, front to back). Once a cabin is reserved, it should be marked as unavailable.

My program should be able to read from a text file with the information of cabin distribution and availability. It should be able to save the information of cabin distribution and availability to a text file.

I've created a main function that takes information from customers and output required information.

However, my problem is that I have too many errors from this code. Can you tell me what is wrong with it? I tried to look at these errors, I have absolutely no idea how to resolve them. (I am a beginning of C++)

#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<cstring>
#include<iomanip>
using namespace std;

const int MaxLvl = 3;
const int MaxRow = 4;
const int MaxCab = 12;
const int MaxRom = 156;

class cruise {
      private:
              char cabins[MaxCab][MaxRow][MaxLvl]
              //bool validate(char);
              int L3, R4, C12;
      
      public:
             void readInformation();
             void writeInformation();
             void reserveCabin();
             void showAvailability(int, int, int, int);

};

void cruise :: readInformation()
{
     cout << "Cabin Informations" << endl;
     ifstream file;
     file.open ("cInformation.txt", ifstream::in);
     while (file.good())
     cout << (char) file.get();
     file.close();
     cout << "Close the file." << endl;
}

void cruise :: writeInformation();
{
     ifstream file;
     file.open ("cInformation.txt");
     if(!file)
     {
              cout << "Error! The file cannot open.";
              exit(1);
     }
     for (int l = 0; L3 < MaxLvl; L3++)
         {
              for (int C12 = 0; C12 < MaxCab; C12++)
                  {
                       for (int R4 = 0; R4 < MaxRow; R4++)
                  }
         }
     }
     file.close();
}

void cruise :: reserveCabin()
{
     string fullName;
     char YN;
     
     cout << "Welcome to our Queen Cruise. Please register to receive a ticket." << endl;
     do {
         cout << "Full name: ";
         cin >> fullName;
         cout << "Please book available cabin" << endl;
         cout << "There are three levels in our cruise. Which level would you like to book?" << endl;
         cout << "Enter 1 for the ground level, 2 for the second level and 3 for the top level. Please enter 1-3: ";
         cin >> L3;
         cout << "Which row would you like to book? 1 from left and 4 from right. Enter 1-4: ";
         cin >> R4;
         cout << "Which cabin would you like to take (1 to 12)? ";
         cin >> C12;
         
         if (cabin[L3][R4][C12] != 'X')
            {
                cabin[L3][R4][C12] != 'X';
            }
            else
            cout << "Unfortunately, the cabin is unavailable." << endl;
            cout << "Please book another cabin (Y/N): ";
            cin >> YN;
            }
            while (YN == 'y' || YN == 'Y');
}

void cruise :: showAvailability(int l, int r, int c, int o)
{
     if (l > 0 && l <= MaxLvl && r > 0 && r <= MaxRow && c > 0 && c <= MaxCab && o = 0 && o <= MaxRom && cabins[l][r][c][o] != 'X') {
           return true;
     }
     else {
          return false;
          }
}

Look at the first error, fix that and several others will probably disappear the next time you compile it. For example vc++ 2008 express complained about line 18. Well, the actual problem is on the line above it -- line 16. Look carefully and you will see that it is missing the semicolon. Fix that then recompile. Then fix the next error.

Look at the first error, fix that and several others will probably disappear the next time you compile it. For example vc++ 2008 express complained about line 18. Well, the actual problem is on the line above it -- line 16. Look carefully and you will see that it is missing the semicolon. Fix that then recompile. Then fix the next error.

Okay I fixed some errors so far. Until that error code

void cruise :: reserveCabin()
{
     string fullName;
     char YN;
     
     cout << "Welcome to our Queen Cruise. Please register to receive a ticket." << endl;
     do {
         cout << "Full name: ";
         cin >> fullName;
         cout << "Please book available cabin" << endl;
         cout << "There are three levels in our cruise. Which level would you like to book?" << endl;
         cout << "Enter 1 for the ground level, 2 for the second level and 3 for the top level. Please enter 1-3: ";
         cin >> L;
         cout << "Which row would you like to book? 1 from left and 4 from right. Enter 1-4: ";
         cin >> R;
         cout << "Which cabin would you like to take (1 to 12)? ";
         cin >> C;
         /*
         L=L-1;
         R=R-1;
         C=C-1;
         */
         if (cabin[L][R][C] != 'X')
            {
                cabin[L][R][C] != 'X';
              /*L=L+1;
                R=R+1;
                C=C+1;
              */
            }
            else
            cout << "Unfortunately, the cabin is unavailable." << endl;
            cout << "Please book another cabin (Y/N): ";
            cin >> YN;
            }
            while (YN == 'y' || YN == 'Y');
}

void cruise :: showAvailability(int l, int r, int c, int o)
{
     if (l > 0 && l <= MaxLvl && r > 0 && r <= MaxRow && c > 0 && c <= MaxCab && o = 0 && o <= MaxRom && cabins[l][r][c][o] != 'X') {
           return true;
     }
     else {
          return false;
          }
}

There are many errors in that part of code.

My incomplete main function code is here for test only (so you may compile it):

#include <iostream>
#include "cruise.h"
using namespace std;

int main() {
    int temp;
    cruise cruise1
    
    cin >> temp;
    cout << cruise1.reserveCabin(temp)
    
    system("pause");
    return 0;
}

Do you have any idea what is wrong with these errors?

>>Do you have any idea what is wrong with these errors?

Nope. You need to post the errors and tell us which lines they occur on.

I can see one problem without using a compiler. Its line 25. I'll let you figure out what is wrong with that line.

>>Do you have any idea what is wrong with these errors?

Nope. You need to post the errors and tell us which lines they occur on.

I can see one problem without using a compiler. Its line 25. I'll let you figure out what is wrong with that line.

Oh right I fixed that error in line 25. I changed that to

#
if (cabins[L][R][C] != 'X')
#
{
#
cabins[L][R][C] != 'X';

Ok I am stupid for not reading that error carefully. But again I got error with another code in 6 line.

#
void cruise :: showAvailability(int l, int r, int c, int o)
#
{
#
if (l > 0 && l <= MaxLvl && r > 0 && r <= MaxRow && c > 0 && c <= MaxCab && o = 0 && o <= MaxRom && cabins[l][r][c][o] != 'X') {
#
return true;
#
}
#
else {
#
return false;
#
}
#
}

That might be wrong format. But I don't know what to fix it.

Edited 6 Years Ago by boiishuvo: n/a

>>cabins[L][R][C] != 'X';
Is array cabins an array of booleans? The above will set the array element to either 0 or 1, depending on whether it is equal to 'X' or not.

>>cabins[L][R][C] != 'X';
Is array cabins an array of booleans? The above will set the array element to either 0 or 1, depending on whether it is equal to 'X' or not.

I really don't get what you said, but I'll show you the original code here:

#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<cstring>
#include<iomanip>


using namespace std;

const int maxlvl=3;
const int maxcar=12;
const int maxrow=4;
const int maxroom=156;

class cruise{
      private:
              char cabin[maxlvl][maxcar][maxrow];
              bool validate (char);
              int b, i, w;
              int l, r, c;
      public:
             int getMaxcar(){return maxcar;}

             int getMaxrow(){return maxrow;}

             int getMaxlvl(){return maxlvl;}
             
             void readCarbinConfig();

             void writeCabinConfig();

             void reserveCabin(); 

             void checkAvailability();
             
             void showAvailability();

            
};
void cruise::readCarbinConfig()
{
    cout<< "\n\nTHIS IS CABIN CONFIGURATION\n\n";      
  
  ifstream infile;
  infile.open ("testfile.txt", ifstream::in);

  while (infile.good())

  cout<<(char) infile.get();
  

  infile.close();
        
}


void cruise::writeCabinConfig()
{
  ifstream infile;
  infile.open("testfile.txt");
     if(!infile)
     {
      cout<<"File cant open.\n";
      exit(1);
      }
       for (int l=0; l<maxlvl; l++)
       {
           for(int c=0; c<maxcar; c++ )
           {
              for(int r=0; r<maxrow; r++)
              {
                 infile>>cabin[l][c][r];
              } 
           }
          }
       
        infile.close();
}

void cruise::reserveCabin()                  
{
      string lName;
      string fName;
      
      int NoCus;
      char ch;
      cout<<"---------------RESERVE CABIN--------------------\n";
      do
      {
        cout<<"PLease enter first name: ";
        cin>>fName;
        cout<<"Please enter last name: ";
        cin>>lName;
     
         cout<<"Enter level(1-3): ";
          cin>>l;
        
           cout<<"Enter row(1-4): ";
           cin>>r;
        
           cout<<"Enter cabin(1-12): ";
           cin>>c;
          
          l=l-1;
          r=r-1;
          c=c-1;
          
          if(cabin[l][c][r]!='X')
             {
              cabin[l][c][r]='X';
              l=l+1;
              c=c+1;
              r=r+1;
              }
             else
             cout<<"Cabin is unavailable.\n";
        cout<<"Do u want to book another cabin(Y/N): ";
        cin>>ch;
        
      }while(ch=='y' || ch=='Y');
                       
}

void cruise::checkAvailability()
{
     b=0;
     i=0;
     w=0;
   
   for (int l=0; l<maxlvl; l++)
       {
           for(int c=0; c<maxcar; c++ )
           {
              for(int r=0; r<maxrow; r++)
              {
               if(c<5 &&(r==0) || (r==3) )
               {
                if(cabin[l][c][r]=='X')
                b++;
               }
               else if((r==0 || r==3) && c>4)
               {
                if(cabin[l][c][r]=='X')
                w++;
               }
               else if(r==1 || r==2)
               {
                if(cabin[l][c][r]=='X')
                i++;
               }
               else
               cout<<"Invalid.";
               }
               cout<<endl;
               }
               
             }
             cout<<"=======ROOM AVAILABLE=======\n";
             cout<<"Amount reserved room: "<<b+i+w<<endl;
             cout<<"Amount of available room: "<<maxroom-(b+i+w)<<endl;
             cout<<"Unavailable Cabin type B: "<<b<<endl;
             cout<<"Available Cabin type B: "<<30-b<<endl;
             cout<<"Unavailable cabin type W: "<<w<< endl;
             cout<<"Available cabin type W: "<<48-w<<endl;
             cout<<"Unavailable cabin type I: "<<i<<endl;
             cout<<"Available cabin type I: "<<78-i<<endl; 
             showAvailability();
 } 
    
void cruise::showAvailability()
{
     ofstream out("roomAvailable.txt");
     b=0;
     i=0;
     w=0;
   
   for (int l=0; l<maxlvl; l++)
       {
           for(int c=0; c<maxcar; c++ )
           {
              for(int r=0; r<maxrow; r++)
              {
               if((r==0 || r==3) && c<5)
               {
                if(cabin[l][c][r]=='X')
                b++;
               }
               else if((r==0 || r==3) && c>4)
               {
                if(cabin[l][c][r]=='X')
                w++;
               }
               else if(r==1 || r==2)
               {
                if(cabin[l][c][r]=='X')
                i++;
               }
               else
               cout<<"Invalid.";
               cout<<cabin[l][c][r];
               }
               cout<<endl;
               }
               
             }
             out<<"=======ROOM AVAILABLE=======\n";
             out<<"Amount reserved room: "<<b+i+w<<endl;
             out<<"Amount of available room: "<<maxroom-(b+i+w)<<endl;
             out<<"Unavailable Cabin type B: "<<b<<endl;
             out<<"Available Cabin type B: "<<30-b<<endl;
             out<<"Unavailable cabin type W: "<<w<< endl;
             out<<"Available cabin type W: "<<48-w<<endl;
             out<<"Unavailable cabin type I: "<<i<<endl;
             out<<"Available cabin type I: "<<78-i<<endl; 
             b=0;
             i=0;
             c=0;
             out.close();
}

bool cruise::validate(char c)
{
     if (c=='B' || c == 'I' || c== 'W')
	return true;
else 
	return false;

}

I copied that and changed some codes. I removed the showAvailablility() part and put my own code that I want the program to show cabin available for customers.

Let me explain clearly:

for readInformation() part - the program should read the cabin information whether it is available or not.

for writeInformation() part - the program should be wrote when a customer wants to book a cabin so that cabin will be marked as unavailable.

for reserveCabin() part - that allows a customer to fill in the full name and which cabin they want to book.

Actually I got what bool means now. Nah I don't want bool function for that showAvailability.

I want to write a code that to show cabins available like the original code

#
void cruise::showAvailability()
#
{
#
ofstream out("roomAvailable.txt");
#
b=0;
#
i=0;
#
w=0;
#
 
#
for (int l=0; l<maxlvl; l++)

but I don't want b, i, w (that function asks a customer what type of cabin but I don't need that). I only want to show customers cabins availablility so customers may have select a cabin (using reserveCabin() part one). I hope that explains clearly. Sorry, my English is not very good.

Edited 6 Years Ago by boiishuvo: n/a

>>cabins[L][R][C] != 'X';
Is array cabins an array of booleans? The above will set the array element to either 0 or 1, depending on whether it is equal to 'X' or not.

Forget about it! I fixed everything and it works well. BUT I have one problem.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <windows.h>
using namespace std;

const int MaxLvl = 3;
const int MaxRow = 4;
const int MaxCab = 12;
const int MaxRom = 156;

class cruise {
      private:
              char cabins[MaxLvl][MaxCab][MaxRow];
              int L, R, C;
              int B, I, W;
      
      public:
             void readInformation();
             void writeInformation();
             void reserveCabin();
             void showAvailability();

};

void cruise :: readInformation()
{
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
     cout << "Welcome to our ";
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
     cout << "Little Swan©";
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
     cout << "\n\nAvailable Cabin Information:" << endl;
     ifstream file;
     file.open ("textfile.txt", ifstream::in);
     while (file.good())
     cout << (char) file.get();
     file.close();
}

void cruise :: writeInformation()
{
     ifstream file;
     file.open ("textfile.txt");
     if(!file)
     {
              cout << "Error! The file cannot open.";
              exit(1);
     }
     for (int L = 0; L < MaxLvl; L++)
         {
              for (int C = 0; C < MaxCab; C++)
                  {
                       for (int R = 0; R < MaxRow; R++)
                       {
                           file >> cabins[L][C][R];
                       }
                  }
         }
     file.close();
}

void cruise :: reserveCabin()
{
     string first, last;
     char YN;
     
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
     cout << "\n\nPlease register to receive a cabin ticket.\n" << endl;
     do {
         cout << "What is your surname? ";
         cin >> last;
         cout << "What is your first name? ";
         cin >> first;
         cout << "\nThere are three levels in our cruise. Which level would you";
         cout << " like to book?" << endl;
         cout << "Enter 1 for the ground level, 2 for the second level and 3 for the top level.\n";
         cout << "Please enter 1-3: ";
         cin >> L;
         cout << "\nWhich row would you like to book? 1 from left and 4 from right.\n"; 
         cout << "Please enter 1-4: ";
         cin >> R;
         cout << "\nWhich cabin would you like to take? (1 to 12): ";
         cin >> C;
         cout << "\nYou've booked your cabin. Thank you. Please enjoy our cruise, Little Swan!\n";
         cout << "--------------------------------------------------------------------------\n\n";

         L = L - 1;
         R = R - 1;
         C = C - 1;
         
         if (cabins[L][R][C] != 'X')
            {
                cabins[L][R][C] = 'X';
                
                L = L + 1;
                R = R + 1;
                C = C + 1;
            }
            else
            cout << "Unfortunately, the cabin is unavailable." << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << "Would you like to book another cabin? (Y/N): ";
            cin >> YN;
            }
            while (YN == 'y' || YN == 'Y');
            cout << "\n\nCurrent Available Cabin Information:\n";
}

void cruise :: showAvailability()
{
     ofstream out("roomAvailable.txt");
     B = 0;
     I = 0;
     W = 0;
   
     for (int L = 0; L < MaxLvl; L++)
     {
         for(int C = 0; C < MaxCab; C++)
         {
                 for(int R = 0; R < MaxRow; R++)
                 {
                         if((R == 0 || R == 3) && C < 5)
                         {
                               if(cabins[L][C][R] == 'X')
                               B++;
                         }
                         else if((R == 0 || R == 3) && C > 4)
                         {
                              if(cabins[L][C][R] == 'X')
                              W++;
                         }
                         else if(R == 1 || R == 2)
                         {
                              if(cabins[L][C][R] == 'X')
                              I++;
                         }
                         else
                         cout << "Invalid.";
                         cout << cabins[L][C][R];
                 }
                 cout << endl;
         }
               
     }
     out << ": Room Available :\n";
     out << "------------------\n";
     out << "Amount reserved room:     " << B + I + W << endl;
     out << "Amount of available room: " << MaxRom -(B+I+W) << endl;
     out << "\n";
     out << "Unavailable Cabin type B: " << B << endl;
     out << "Available Cabin type B:   " << 30 - B << endl;
     out << "\n";
     out << "Unavailable cabin type W: " << W << endl;
     out << "Available cabin type W:   " << 48 - W << endl;
     out << "\n";
     out << "Unavailable cabin type I: " << I << endl;
     out << "Available cabin type I:   " << 78 - I << endl; 
             
     B = 0;
     I = 0;
     W = 0;
             
     out.close();
}

and the main function code here:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cstring>
#include <iomanip>
#include <windows.h>
#include "cruise.h"
using namespace std;

int main()
{
    cruise c;
    
    c.readInformation();
    c.writeInformation();
    c.reserveCabin();
    c.showAvailability();
    
    system("pause");
    return 0;
 
}

When I ran the program using command, I copied it and paste it here:

Welcome to our Little Swan®
Please register to receive a cabin ticket.

What is your surname? David
What is your first name? Sam

There are three levels in our cruise. Which level would you like to book?
Enter 1 for the ground level, 2 for the second level and 3 for the top level.
Please enter 1-3: 2

Which row would you like to book? 1 from left and 4 from right.
Please enter 1-4: 2

Which cabin would you like to take? (1 to 12): 2

You've booked your cabin. Thank you. Please enjoy our cruise, Little Swan!
--------------------------------------------------------------------------

Would you like to book another cabin? (Y/N): y
What is your surname? Sam
What is your first name? David

There are three levels in our cruise. Which level would you like to book?
Enter 1 for the ground level, 2 for the second level and 3 for the top level.
Please enter 1-3: 2

Which row would you like to book? 1 from left and 4 from right.
Please enter 1-4: 2

Which cabin would you like to take? (1 to 12): 2

You've booked your cabin. Thank you. Please enjoy our cruise, Little Swan!
--------------------------------------------------------------------------

Unfortunately, the cabin is unavailable.
Would you like to book another cabin? (Y/N): N

See the green highlight - I booked a cabin successfully and it says THANK YOU.

Then see the red highlight - I booked the same cabin again, it says THANK YOU and UNFORTUNATELY, THE CABIN IS UNAVAILABLE.

It doesn't make sense, right? If so, how do I fix it?

>>If so, how do I fix it?

The same way I would -- use your compiler's debugger and step through the program one line at a time so that you can easily see what its doing.

>>If so, how do I fix it?

The same way I would -- use your compiler's debugger and step through the program one line at a time so that you can easily see what its doing.

Okay then.

#
void cruise :: reserveCabin()
#
{
#
string first, last;
#
char YN;
#
 
#
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
#
cout << "\n\nPlease register to receive a cabin ticket.\n" << endl;
#
do {
#
cout << "What is your surname? ";
#
cin >> last;
#
cout << "What is your first name? ";
#
cin >> first;

That red code works properly. But I want you to type a full name only, not first name and last name.

So I wrote a code:

string fullName;
#
do{
#
cout << "What is your name?";
cin >> fullName;

and so on. I complied it and got no error. However, only that code part makes the program stuffs up. I searched to find to resolve that issue but couldn't find one :confused: What is wrong with it?

If you want spaces in the name then you will have to use getline(), not the >> operator.

Okay, it works. Thanks! So far I have the very last problem then I will be finished. That problem is very strange to me.

if (cabins[L][R][C] != 'X')
            {
                cabins[L][R][C] = 'X';
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
                cout << "\nYou've booked your cabin. Thank you. Please enjoy our cruise, Little Swan!\n";
                cout << "--------------------------------------------------------------------------\n";
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0);
                
                L = L + 1;
                R = R + 1;
                C = C + 1;
            }
            else
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
            cout << "\nUnfortunately, the cabin is unavailable." << endl;
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
            cout << "Would you like to book another cabin? (Y/N): ";

The first line of the red highlight, when without that line and "Unfortunately, the cabin is unavailable." won't be appeared after you book a cabin successfully. But I added the red color text function, "Unfortunately, the cabin is unavailable." appears even you book a cabin successfully :confused: Do I make sense for you? It is hard to explain though.

This article has been dead for over six months. Start a new discussion instead.