#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct room
{
    char type[3];
    int rmn;
    int terrif;
};
void add(room *,int);
void main()
{
    clrscr();
    int n,r,modifychoice,c,a,i;
    room rm[5];
    cout<<endl<<endl;
    while(1)
    {
        cout<<"\t\t\t"<<"HOTEL DAVIANS"<<endl<<endl;
        cout<<"\t\t   "<<"MENU FOR HOTEL DAVIANS -"<<endl;
        cout<<"\t\t"<<"1. Enter rooms record for the hotel  "<<endl;
        cout<<"\t\t"<<"2. Modify rooms records"<<endl;
        cout<<"\t\t"<<"3. Check room records"<<endl;
        cout<<"\t\t"<<"4. Exit"<<endl<<endl<<endl;
        cout<<"enter choice: ";
        cin>>n;
        switch(n)
        {
            case 1: cout<<"enter no. of rooms in the hotel at present: ";
                cin>>r;
                for(int i=0;i<r;i++)
                {
                    cout<<"Details of room no."<<i+1<<endl;
                    cout<<"Room no.: ";
                    cin>>rm[i].rmn;
                    cout<<"Room type: ";
                    gets(rm[i].type);
                    fflush(stdin);
                    cout<<"Room terrif";
                    cin>>rm[i].terrif;
                }
                break;
            case 2:cout<<"Enter room no. to be modified: ";
                   cin>>i;
                   i=i-1;
                   clrscr();
                   while(1)
                   {
                   cout<<endl<<endl;
                   cout<<"\t\t"<<"1. Change room no."<<endl;
                   cout<<"\t\t"<<"2. Change room terrif"<<endl<<endl<<endl;
                   cout<<"\t\t"<<"Enter choice: ";
                   cin>>modifychoice;
                   if(modifychoice<3)
                   {
                   switch(modifychoice)
                   {
                    case 1:cout<<"Enter new room no.: ";
                           cin>>rm[i].rmn;
                    break;
                    case 2:cout<<"Enter new room terrif: ";
                           cin>>rm[i].terrif;
                    break;
                    default: cout<<"false option";
                   }
                   break;
                   }
                   clrscr();
                   }
            case 3:cout<<"Enter room no. whose record u want to see: ";
                   cin>>a;
                   i=a-1;
                   cout<<"Room no. is: "<<rm[i].rmn<<endl;
                   cout<<"Room type is: ";
                   puts(rm[i].type);
                   cout<<endl;
                   cout<<"Room terrif is: "<<rm[i].terrif;
            break;
            case 4:exit(0);
            default: cout<<"false option" ;
        }
        clrscr();
    }
    getch();
}

I m making a program running hotel menu ......... in which i have put options to add hotel record in 1st option and to see the entered records in 3rd option. When i select 1st option , i add record but when the loop again runs and i get the screen of menu again, i select 3rd option to see records but the record comes blank i.e. I m not able to see the added record again :( ............ What is the error in my coding regarding this ??

I m bonded to use concepts of structure, functions, arrays and basics of c++, not more than that. PLEASE HELP

Recommended Answers

All 12 Replies

gets(rm[i].type);
fflush(stdin);

These lines are in the wrong order. I'll let someone else chastize you over using fflush() on an input stream, because it's obvious that you're using Turbo C++. I'll also let gets() slide, but keep in mind that it's the worst possible choice for string input.

i select 3rd option to see records but the record comes blank i.e. I m not able to see the added record again :( ............ What is the error in my coding regarding this ??

I'm not sure I completely understand your reproduction steps, but option 3 searches for a room index, but the prompts imply that you're searching for a room number. If the room number doesn't match the 1-based index of the room in your array, the search will fail to produce correct results.

Consider a search more like this instead:

cout << "Room #: ";

if (cin >> a) {
    // Find the matching room number
    for (i = 0; i < r; i++) {
        if (rm[i].rmn == a) {
            // Display the room
            cout << "Found room #" << rm[i].rmn << '\n'
                    << "\t* Type: " << rm[i].type << '\n'
                    << "\t* Terrif: " << rm[i].terrif << endl;

            // End the loop early
            break;
        }
    }

    if (i == r) {
        // The loop didn't end early, so no room was displayed
        cout << "Room #" << a << " not found" << endl;
    }
}

There few major problems with your code (except for mixing C and C++):
1. you confuse room number with entry number;
2. you define a certain size for your arrays from the beginning, while further in your code no condition is imposed (e.g., what if r at line 31 is asked to be 6?).

There few major problems with your code (except for mixing C and C++)

Why would you consider mixing C and C++ a major problem?

Why would you consider mixing C and C++ a major problem?

Not major, but a problem in coding style.

I didn't understand your 1st point of confusing room number with entry number....... CGSMCMLXXV

case 3:cout<<"Enter room no. whose record u want to see: ";
cin>>a;
i=a-1;
cout<<"Room no. is: "<<rm[i].rmn<<endl;
cout<<"Room type is: ";
puts(rm[i].type);
cout<<endl;
cout<<"Room terrif is: "<<rm[i].terrif;
break;

First line, "Enter room number..." which is connected to the entry number. The same in the case 2: branch.

Now, consider this. Some of the hotels put the floor number in front of the room number, having room numbers starting from 1 (or 0 for that matter) to as many room as there are at that floor. If room 3 is at a higher floor than the ground floor, the room number is bigger than your maximum allowed entry (5 in your case) and that will break your application for both cases 2 and 3. On the other hand, if you search by the entry number, you force your user to know which entry corresponds to which room in order to modify and/or to retrieve information about a certain room. And the examples can go on with whatever case where the entry number doesn't correspond to the room number minus 1 (e.g., think of the permutation problem). As deceptikon said in post 2 of this thread, there is no finder for the matching room.

Don't get me wrong, you started nicely by inserting a variable which should hold your room number in the room structure. You just didn't finalize it in the same way.

void main()

^^ No.

int main(int argc, char *argv[])

Please.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct room
{
    char type[3];
    int rmn;
    int terrif;
    char username[20];
    char useradd[50];
    char userph[10];
    char status;
};
void main()
{
    clrscr();
    int n,r,modifychoice,c,a,i,mrn,flag=0;
    room rm[35];
    cout<<endl<<endl;
    while(1)
    {
        clrscr();
        cout<<"\t\t\t"<<"HOTEL DAVIANS"<<endl<<endl;
        cout<<"\t\t   "<<"MENU FOR HOTEL DAVIANS -"<<endl;
        cout<<"\t\t"<<"1. Add rooms details for the hotel  "<<endl;
        cout<<"\t\t"<<"2. Modify rooms details"<<endl;
        cout<<"\t\t"<<"3. Check room records"<<endl;
        cout<<"\t\t"<<"4. Book a room"<<endl;
        cout<<"\t\t"<<"5. Exit"<<endl<<endl<<endl;
        cout<<"enter choice: ";
        cin>>n;


        switch(n)
        {
            case 1: cout<<" no. of rooms in the hotel at present(max 35): ";
                cin>>r;
                for(int i=0;i<r;i++)
                {
                    cout<<"Details of room no."<<i+1<<endl;
                    rm[i].rmn=i+1;
                    cout<<"Room type(DELuxe/PREmium: ";
                    gets(rm[i].type);

                    fflush(stdin);
                    cout<<"Room terrif:";
                    cin>>rm[i].terrif;
                    rm[i].status='E';
                    cout<<"*************************************************";
                    cout<<endl<<endl;
                }
                break;
            case 2:cout<<"Enter room no. to be modified: ";
                   cin>>mrn;
                   clrscr();

                   cout<<endl<<endl;
                   cout<<"\t\t"<<"1. Change room no."<<endl;
                   cout<<"\t\t"<<"2. Change room terrif"<<endl<<endl<<endl;
                   cout<<"\t\t"<<"Enter choice: ";
                   cin>>modifychoice;
                   switch(modifychoice)
                   {
                    case 1:cout<<"Enter new room no.: ";
                           cin>>rm[i].rmn;
                        break;
                    case 2:cout<<"Enter new room terrif: ";
                           cin>>rm[i].terrif;
                           break;
                    default: cout<<"false option";
                   }
                   break;
            case 3:cout<<"Enter room no.: ";
                   cin>>mrn;
                   for(i=0;i<r;i++)
                   {
                if(rm[i].rmn==mrn)
                {
                      cout<<"Room type is: ";
                      puts(rm[i].type);
                      cout<<"Room terrif is: "<<rm[i].terrif;
                      flag=1;
                }
                   }
                if(flag==0)
                    cout<<"invalid room number";

                break;
            case 4: flag=0;
                cout<<"room no. to be occupied"<<endl;
                for(i=0;i<r;i++)
                {
                    if(rm[i].status=='E')
                    {
                        flag=1;
                        break;
                    }
                }
                   if(flag==0)
                cout<<"All rooms Booked";
                   else
                   {
                   cout<<"Romm number is:"<<rm[i].rmn;
                   cout<<"______________________________________________________________________";
                   cout<<"\t\t Occupents' name  ";
                   gets(rm[i].username);
                   cout<<" user address  ";
                   gets(rm[i].useradd);
                   cout<<" phone no  ";
                   gets(rm[i].userph);
                   rm[i].status='B';
                   }
                   break;



            case 5:exit(0);
            default: cout<<"false option" ;
        }
        getch();

    }

}

guys i have recorreced the program but still case 3 is not working properly ............ i.e. it displays 'invalid room number' even if i input correct room number .......... PLEASE HELP

Can you print value of r in case 3? I am sorry, but I have no Turbo C++ compiler here.

On the other hand, if you consider this will help, here is something which I made it fast for you and which works under Linux with g++ compiler (it's a little bit long, but it does the basics of what you need):

#include <iostream>
#include <vector>
#include <string>
#include <limits>

// Header for system() borrowed from C
#include <stdlib.h>

using namespace std;

struct Room
{
    string type;
    int nr;
    int tariff;
    string customer_name;
    string customer_surname;
    string customer_phone;
    bool occupied;
    bool booked;
};

// emulates clrscr() from Turbo C++ under Linux using "evil" system()
// use "cls" for MS DOS or remove it if "conio.h" is included
extern "C" {
    void clrscr() {system("clear");};
};

int getMenuOption();
void addRooms(vector<Room> &rooms);
void modifyRoom(vector<Room> &rooms);
void checkRoom(vector<Room> rooms);
void bookRooms(vector<Room> &rooms);

int main()
{
    vector<Room> rooms;
    int option = 1;

    // when option is other number than 1, 2, 3 or 4
    // program exits
    while (option > 0 && option < 5)
    {
        option = getMenuOption();
        switch (option)
        {
            case 1: addRooms(rooms); break;
            case 2: modifyRoom(rooms); break;
            case 3: checkRoom(rooms); break;
            case 4: bookRooms(rooms); break;
        };
    };

    return 0;
};

int getMenuOption()
{
    int choice = 0;

    clrscr();
    cout << endl << endl;
    cout << "\t\t\tHOTEL DAVIANS" << endl << endl;
    cout << "\t\t MENU FOR HOTEL DAVIANS -" << endl;
    cout << "\t\t1. Add rooms details for the hotel " << endl;
    cout << "\t\t2. Modify rooms details" << endl;
    cout << "\t\t3. Check room records" << endl;
    cout << "\t\t4. Book a room" << endl;
    cout << "\t\t5. Exit" << endl << endl << endl;
    cout << "enter choice: ";
    cin  >> choice;

    return choice;
}

int findEntryByRoomNumber(int room_number, vector<Room> rooms)
{
    int entry_number = -1;

    for (int i=0; i<rooms.size(); i++)
    {
        if(rooms[i].nr == room_number)
        {
            entry_number = i;
            break;
        };
    };

    return entry_number;
}

void addRooms(vector<Room> &rooms)
{
    int choice = 1;
    Room room;

    room.customer_name = "";
    room.customer_surname = "";
    room.customer_phone = "";
    room.occupied = false;
    room.booked = false;

    while (choice == 1)
    {
        clrscr();
        cout << endl << endl;
        cout << "\t\t\tHOTEL DAVIANS" << endl << endl;
        cout << "\t\t ADD MENU" << endl;
        cout << endl << endl;
        cout << "\t\tRoom number: ";
        cin >> room.nr;
        if (findEntryByRoomNumber(room.nr,rooms) == -1)
        {
            cout << "\t\tRoom type: ";
            cin >> room.type;
            cout << "\t\tRoom tariff: $";
            cin >> room.tariff;
            rooms.push_back(room);
        } else {
            cout << "\t\tAn entry for this room number already exists." << endl;
        };
        cout << endl << endl;
        cout << "\t\tWhat would you like to do now?" << endl;
        cout << "\t\t1. Add entry for a new room." << endl;
        cout << "\t\t2. Return to the previous menu." << endl << endl << endl;
        cout << "enter choice: ";
        cin >> choice;
    };

    return;
}

void printRoomInfo(Room room)
{
    cout << "\t\tRoom information" << endl;
    cout << "\t\t - room number: " << room.nr << endl;
    cout << "\t\t - room type: " << room.type << endl;
    cout << "\t\t - room tarrif: $" << room.tariff << endl;
    cout << "\t\t - occupied: ";
    if (room.occupied)
    {
        cout << "yes" << endl;
        cout << "\t\t - customer first name(s): " << room.customer_name << endl;
        cout << "\t\t - customer last name(s): " << room.customer_surname << endl;
        cout << "\t\t - customer phone: " << room.customer_phone << endl;
    } else {
        cout << "no" << endl;
    };
    cout << "\t\t - booked: ";
    if (room.booked)
    {
        cout << "yes" << endl;
        cout << "\t\t - customer first name(s): " << room.customer_name << endl;
        cout << "\t\t - customer last name(s): " << room.customer_surname << endl;
        cout << "\t\t - customer phone: " << room.customer_phone << endl;
    } else {
        cout << "no" << endl;
    }

    return;
}

void insertCustomer(vector<Room> &rooms, int entry_number)
{
    string stemp;

    cout << "\t\tCustomer first name(s): ";
    cin >> stemp;
    rooms[entry_number].customer_name = stemp;
    cout << "\t\tCustomer last name(s): ";
    cin >> stemp;
    rooms[entry_number].customer_surname = stemp;
    cout << "\t\tCustomer phone number: ";
    cin >> rooms[entry_number].customer_phone;

    return;
}

void modifyRoom(vector<Room> &rooms)
{
    int choice = 1;
    int entry_number;
    int room_number;
    int itemp;
    string stemp;

    while (choice == 1)
    {
        clrscr();
        cout << endl << endl;
        cout << "\t\t\tHOTEL DAVIANS" << endl << endl;
        cout << "\t\t MODIFY MENU" << endl;
        cout << endl << endl;
        cout << "\t\tRoom number: ";
        cin >> room_number;
        entry_number = findEntryByRoomNumber(room_number,rooms);
        if(entry_number != -1)
        {
            printRoomInfo(rooms[entry_number]);
            cout << "\t\tRoom type (default/type): ";
            cin >> stemp;
            rooms[entry_number].type = stemp == "default" ? rooms[entry_number].type : stemp;
            cout << "\t\tRoom tariff (0 for default): $";
            cin >> itemp;
            rooms[entry_number].tariff = itemp == 0 ? rooms[entry_number].tariff : itemp;
            cout << "\t\tOccupied [yes/no/default]: ";
            cin >> stemp;
            while (!(stemp == "yes" || stemp == "no" || stemp == "default"))
            {
                cout << "\t\tInvalid option. Try again." << endl;
                cout << "\t\tOccupied [yes/no/default]: ";
                cin >> stemp;
            };
            if (stemp == "yes")
            {
                rooms[entry_number].occupied = true;
                insertCustomer(rooms, entry_number);
            } else if (stemp == "no") {
                rooms[entry_number].occupied = false;
            };
            cout << "\t\tBooked [yes/no/default]: ";
            cin >> stemp;
            while (!(stemp == "yes" || stemp == "no" || stemp == "default"))
            {
                cout << "\t\tInvalid option. Try again." << endl;
                cout << "\t\tBooked [yes/no/default]: ";
                cin >> stemp;
            };
            if (stemp == "yes")
            {
                rooms[entry_number].booked = true;
                insertCustomer(rooms, entry_number);
            } else if (stemp == "no") {
                rooms[entry_number].booked = false;
            };
            if (!rooms[entry_number].occupied && !rooms[entry_number].booked)
            {
                rooms[entry_number].customer_name = "";
                rooms[entry_number].customer_surname = "";
                rooms[entry_number].customer_phone = "";
            };
        } else {
            cout << "\t\tNo entry found for this room number." << endl;
        };
        cout << endl << endl;
        cout << "\t\tWhat would you like to do now?" << endl;
        cout << "\t\t1. Modify another room details." << endl;
        cout << "\t\t2. Return to the previous menu." << endl << endl << endl;
        cout << "enter choice: ";
        cin >> choice;
    };

    return;
};

void checkRoom(vector<Room> rooms)
{
    int room_number;
    int entry_number;
    int choice = 1;

    while (choice == 1)
    {
        clrscr();
        cout << endl << endl;
        cout << "\t\t\tHOTEL DAVIANS" << endl << endl;
        cout << "\t\t CHECK RECORDS MENU" << endl;
        cout << endl << endl;
        cout << "\t\tRoom number: ";
        cin >> room_number;
        entry_number = findEntryByRoomNumber(room_number,rooms);
        if(entry_number != -1)
        {
            printRoomInfo(rooms[entry_number]);
        } else {
            cout << "\t\tNo entry found for this room number." << endl;
        };
        cout << endl << endl;
        cout << "\t\tWhat would you like to do now?" << endl;
        cout << "\t\t1. Check another room records." << endl;
        cout << "\t\t2. Return to the previous menu." << endl << endl << endl;
        cout << "enter choice: ";
        cin >> choice;
    };

    return;
};

int availableRooms(vector<Room> &rooms)
{
    int available = 0;

    for (int i=0; i<rooms.size(); i++)
    {
        if (!rooms[i].occupied && !rooms[i].booked)
            available++;
    };

    return available;
}

void bookRoom(string name, string surname, string phone, vector<Room> &rooms, int &requested_number_rooms)
{
    for (int i=0; i<rooms.size(); i++)
    {
        if ((!rooms[i].occupied && !rooms[i].booked) && requested_number_rooms > 0)
        {
            rooms[i].booked = true;
            rooms[i].customer_name = name;
            rooms[i].customer_surname = surname;
            rooms[i].customer_phone = phone;
            requested_number_rooms--;
        };
    };

    return;
}

void bookRooms(vector<Room> &rooms)
{
    string name, surname, phone;
    int requested_number_rooms;
    int entry_number;
    int choice = 1;
    int available_rooms = 0;

    while (choice == 1)
    {
        clrscr();
        cout << endl << endl;
        cout << "\t\t\tHOTEL DAVIANS" << endl << endl;
        cout << "\t\t BOOKING MENU" << endl;
        cout << endl << endl;
        available_rooms = availableRooms(rooms);
        if (available_rooms < 1)
        {
            cout << "\t\tNo available rooms." << endl << endl << endl;
            cout << "Press ENTER to turn back to the previous menu..." << flush;
            cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
            cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
            break;
        } else {
            cout << "\t\tAvailable rooms: " << available_rooms << endl << endl << endl;
        };
        cout << "\t\tCustomer first name(s): ";
        cin >> name;
        cout << "\t\tCustomer last name(s): ";
        cin >> surname;
        cout << "\t\tCustomer phone number: ";
        cin >> phone;
        cout << "\t\tRequested number of rooms: ";
        cin >> requested_number_rooms;
        while (requested_number_rooms > available_rooms || requested_number_rooms < 1)
        {
            cout << "\t\tInvalid request for number of rooms." << endl;
            cout << "\t\tRequested number of rooms: ";
            cin >> requested_number_rooms;
        };
        bookRoom(name, surname, phone, rooms, requested_number_rooms);
        cout << endl << endl;
        cout << "\t\tWhat would you like to do now?" << endl;
        cout << "\t\t1. Book room(s)." << endl;
        cout << "\t\t2. Return to the previous menu." << endl << endl << endl;
        cout << "enter choice: ";
        cin >> choice;
    };

    return;
};

Don't chastize me for the length because mostly it's cout and some basic input checking. I didn't optimized the code to be easier understandable. I couldn't comment it properly because I have no time (sorry). I leave to you to understand and modify it for your compiler and needs.

Good luck!

Thanks a lot bro :) ..............I appreciate your knowledge about programming languages but I have no knowledge of Linux g++ language .......... I am a skul student of 11th class and have studied c++ till now for one year only. I have no knowledge of converting linux language to c++. But I m re-editing my programme in c++ language only and will soon put my re-edited programme on this discussion so that I can again take your help for more modification if it will be needed in the programme .................... THANKS AGAIN :D

By the way how many programming languages do u knoe efficiently ??

Linux is operating system like MS Windows. It has different flavors like Debian, Debian-based variety, RedHat and RedHat-based variety. GCC/G++ is GNU compiler for C/C++ and a small other variety of programming languages. ;)

OK thanks :) ............. I will soon put my re-edited and final programme to know your views about it since i m a beginner in c++ language :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.