We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

C++ Arrays and elements

Last post 04-04-2008 8:12 AM by Mohammad Rastkar. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 04-01-2008 6:37 PM

    • Levinia
    • Not Ranked
    • Joined on 04-01-2008
    • United Kingdom
    • New Member
    • Points 25

    C++ Arrays and elements

    Hi guys,

    could anyone point in me the right direction, please? I'm trying to get my booking system to prompt the user for his desired seat number, and also to print out "Booked" next to the seat numbers that's been reserved when option 3 is selected, rather than 1's and 0's

    As you've probably noticed already - I'm a complete novice to C++, but this is what I have so far:

    #include "stdafx.h"

    #include <iostream>

    #include <cstdlib>

    using namespace std;

    #include <iomanip>

    using std::setw;

    const int seatNo = 10;

    // function to call when Option 1 has been fully booked

    void opt2(int tempSeat[]){

    for(int seatCounter=0; seatCounter<seatNo; seatCounter++){

    if(tempSeat[seatCounter]==0){

    tempSeat[seatCounter] = 1;

    cout<<
    "\n********** Bus Pass **********"<<endl;

    cout<<"Your Seat Number is "<<seatCounter+1<<endl;

    cout<<"Departure: 11 March at 8:00"<<endl;

    cout<<"\n******************************"<<endl;

    break;

    }

    }

    }

    // function to call when Option 2 is fully booked

    void opt1(int tempSeat[]){

    for(int seatCounter=0; seatCounter<seatNo; seatCounter++){

    if(tempSeat[seatCounter]==0){

    tempSeat[seatCounter] = 1;

    cout<<
    "\n********** Bus Pass **********"<<endl;

    cout<<"Your Seat Number is "<<seatCounter+1<<endl;

    cout<<"Departure: 10 March at 8:00"<<endl;

    cout<<"\n******************************"<<endl;

    break;

    }

    }

    }

     

    void main(){

    char answer = 'Y';

    int opt1SeatNo [seatNo] = {0}; // initailizing seats for the first option (not booked)

    int opt2seatNo[seatNo] = {0}; // initailizing seats for the second option (not booked)

    int num = 0; // exit loop if user enters 4

    int selection = 0; // select options

    bool full = true; // boolean function to check whether option 1 && option 2 have seats available

    bool Opt1CEmpty = false; // check whether option 1 have seats available

    bool Opt2Empty = false; // check whether option 2 have seats available

    cout<<"<*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*"<<endl;

    cout<<"Welcome to British Adventures Coach Travel "<<endl;

    cout<<"<*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*"<<endl;

     

    while(num != 4){

     

    cout<<
    "\n\n*** Please select an option ***" << endl;

    cout<<"\n<1> Journey on 10 March 08 at 8:00 from London to Birmingham" << endl;

    cout << "\n<2> Journey on 11 March 08 at 8:00 from London to Birmingham" << endl;cout << "\n<3> Display Bookings\n" << endl;

    cin>>selection;

    if (selection == 1){

     

    bool empty = false;

    for(int seatCounter=0; seatCounter<seatNo; seatCounter++){

    if(opt1SeatNo[seatCounter] == 0)

    empty = true;

    }

    if(empty){for(int i=0; i<seatNo; i++)

    {

    if(opt1SeatNo[i] == 0){

    opt1SeatNo[i] = 1;

    cout<<"\n********** Bus Pass **********"<<endl;

    cout<<" Your Seat Number is "<<i+1<<endl;

    cout<<" Bus departs 10 March at 8:00"<<endl;

    cout<<"\n******************************\n"<<endl;

    system( "pause" ); // to display "Press any key to continue . . ."

    break;

    }

    }

    }

    else

    {

    cout<<
    "Sorry, the trip for 10 March has been fully booked"<<endl;cout<<"Would you like to book a trip for 11 March instead ?[y/n]";

    cin>>answer;

     

    if(toupper(answer)=='Y'){

    for(int Ino=0; Ino <seatNo; Ino++){

    if(opt2seatNo[Ino]==0)

    Opt2Empty = true;

    }

    if(Opt2Empty == true)

    opt2(opt2seatNo);

    else{

    cout<<"Sorry, this coach is now fully booked."<<endl;

    do{

    cout<<"\n\nNo more seats available."<<endl;

    cout<<"Press (4) to exit."<<endl;

    cin>>num;

    }
    while(num != 4);

    }

    }

    else

    cout<<"\nThank You. Please call again. "<<endl<<endl;

    }

    }

     

    if(selection == 2){

     

    bool empty = false;

    for(int seatCounter = 0; seatCounter<seatNo; seatCounter++){

    if(opt2seatNo[seatCounter] == 0)empty = true;

    }

    if(empty){

    //cout<<" Please enter the seat number you'd like to book: "<<endl;

    //cin>>opt2seatNo;

    for(int i=0; i<seatNo; i++)

    {

    if(opt2seatNo[i] == 0){

    opt2seatNo[i] = 1;

    cout<<
    "\n********** Bus Pass **********"<<endl;

    cout<<" Your Seat Number is "<<i+1<<endl;

    cout<<" Bus departs 11 March at 8:00 "<<endl;

    cout<<"\n******************************\n"<<endl;

    system( "pause" );break;

    }

    }

    }

    else

    {

    cout<<
    "Sorry, the trip for 11 March has been fully booked"<<endl;cout<<"Would you like to book a trip for 10 March instead ?[y/n]";

    cin>>answer;

    if(toupper(answer)=='Y'){

    for(int Tno=0; Tno <seatNo; Tno++){

    if(opt1SeatNo[Tno]==0)

    Opt1CEmpty = true;

    }

    if(Opt1CEmpty == true)

    opt1(opt1SeatNo);

    else

    {

    cout<<
    "\n\nThere are no seats available."<<endl;

     

    do{

    cout<<"\n\nSorry, our coaches are now fully booked."<<endl;

    cout<<"Press (4) to exit."<<endl;

    cin>>num;

    }
    while(num !=4);

    }

    }

    //else

    // cout<<"The next bus leaves in four hours."<<endl<<endl;

    }

    }

    //Display the reservations

    if (selection == 3){

    cout << "\nJourney on 10 March 08 at 8:00 \nFrom London to Birmingham\n" << endl;

    cout << "Seat no " << setw (14) << "Booked" << endl;for(int Tno=0; Tno <seatNo; Tno++)

    cout << setw (7)<< Tno << setw (15) << opt1SeatNo[Tno] << endl;

    cout << "\nJourney on 11 March 08 at 8:00 \nFrom London to Birmingham\n" << endl;

    cout << "Seat no " << setw (14) << "Booked" << endl;for(int Ino=0; Ino <seatNo; Ino++)

    cout << setw (7)<< Ino << setw (15) << opt2seatNo[Ino] << endl;

    system(
    "pause" );

    }

    if(num == 4){

    cout<<"\nThank You........"<<endl;

    }

    }

    }

    • Post Points: 10
  • 04-04-2008 8:12 AM In reply to

    Dog [&] Is there any specific Qs ?!

    Hi Levinia,

    => Your approach is good, but I think you should save the results in a file for later usage, for that, see these resources :

    Forum Thread : C++ add, delete, list and modify using binary file ".dat"

    Sample Code : [C++] Simple Multiplication Test - Involves with : files & strings & arrays & ...

    => For your code : Here viewing the code is somewhat hard! Then please ask some specific questions (about a little part of the whole code).
    But just a little thing : When you are using 'using namespace std;', you don't need to use 'using std::setw;' .

    Oh! just don't forget to reserve the first seat for me! Thanks!  Wink

    - I don't have Time To waste The Time!
    • Post Points: 5
Page 1 of 1 (2 items)