Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

2D Arrays & assigning values

Last post 04-10-2008 12:25 PM by Levinia. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 04-09-2008 4:05 PM

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

    2D Arrays & assigning values

    Hi guys,

    I've previously posted a question re a booking system.  After major changes I'm glad to say that I've made some progress (Thanks, Mohammad Rastkar)

    I'm using a 2D array.  When the user books the 4th seat of the 3rd row (adding 1 to array indexes) then seats[2][3] ='U' changes to seats[2][3] ='!' I'm trying to include an option to reserve successive seats.  For example, the user enters the row number, seat number and then the number of successive seats required. 

    #include "stdafx.h"

    #include <iostream>

    using namespace std;

    char answer;//made global so promptUser function can access answer

    //function to output a visual diagram of the seating plan

    //outputs the array with 10 rows and 4 columns

    void printDiagram (char seats [][5])

    {

    cout<<"\tSEATS"<<endl;//header

    cout<<"\t1 2 3 4"<<endl;//to output header of numbers to table

     

    //looping through the array's rows

    for (int i = 1; i <11; i++)//for loop to output table, starting from row 1

    { cout<<"Row "<<i<<"\t";

     

    //looping through the columns of the row

    for (int j = 1; j<5; j++)

    cout<<seats[i][j]<<" ";//to ouput the U's and !'s for the rows

    cout<<endl;

    }

    }

    void bookConsec (char seats[][5])

    {

    int row, number, require; //row no, seat no and number of consecutive seats required.

     

    cout<<
    "Please enter row (1-10),\nthen a seat number (1-4) \nand number of seats required: \n";

    cin>>row;

    while (row<1 || row>10)//while loop to test if row is between 1 and 10

    {

    cout<<
    "This row doesnt exist, please enter a new row: \t";

    cin>>row;

    }

    cout<<"\n";//used to line up the input for the seat nr

    //cout<< setw(48);

    cin>>number;

    while (number < 1 || number > 4)//while loop to test if seat number is between 1 and 4

    {

    cout<<
    "This seat doesnt exist, please enter a new seat number: \t";

    cin>>number;

    }

    cout<<
    "\n";

    cin>>require;

    while (number < 1 || number > 40)//while loop to test if seat number is between 1 and 40

    {

    cout<<
    "Maximum number of seats allowed for this booking: 40\t";

    cin>>require;

    }

     

    if ( seats[row][number] == 'U' )//this checks if seat is available, if it is, sets the seat to reserved

    {

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

    {

    cout<<
    "Your reservation details: "<<endl<<endl;

    seats[row][number]= '!';//reserved

    }

    }

    else cout<<"Seat already booked. Please select another seat."<<endl<<endl;

    }

    int main()

    {

    char seats [11][5]; //declare array seats, 10 rows accross, 4 rows down

    char menu; //variable used to ask if user wants to see the menu again

    int choice; //variable for menu answer

     

    for (int i = 1; i <11; i++)//initialize all seats to 'U'

    {

    for (int j = 1; j<5; j++)seats[i][j] = 'U';

    }

     

    do //use a do while loop here so user can view many more than one time if necessary

    {

    //Print menu

    cout<<"\n\t\t\tMenu"<<endl;

    cout<<" ---------------------------------------------"<<endl;

    cout<<"\t1) Reserve consecutive seats"<<endl;

    cin>>choice;

    switch (choice)//switch statement to process user input from the menu above

    {

    case 1:

    {

    bookConsec (seats);//call promptUser function

    printDiagram (seats);//call printDiagram function (prints diagram to screen)

    break;

    }

    case 7: //function 'main' will end if user enters 7 (return 0)

    return 0;

    break;

    default: return 0; //Any other numbers entered by the user will end the function

    }

    cout<<endl<<"Do you wish to see the menu again?: (y/n) ";//allows user to view the menu again

    cin>>menu;

    cout<<endl<<endl;

    }

    while (toupper(answer)=='Y');

    return 0;

    }

     

    • Post Points: 10
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 04-09-2008 8:36 PM In reply to

    Big Smile [:D] Something for Looking!

    Hi Levinia,

    Glad to see your progress! Smiley Face

    => Please take a look at this :

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

    {

          if ( seats[row][number] == 'U' )

          {

                cout<<"Your reservation details: "<<endl<<endl;

                seats[row][number]= '!';//reserved

          }

          else cout<<"Seat already booked. Please select another seat."<<endl<<endl;

     

          if ( number == 4 )

          {

                number = 1;

                row++;

          }

    }

    - I don't have Time To waste The Time!
    • Post Points: 10
  • 04-10-2008 12:25 PM In reply to

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

    Re: Something for Looking!

    Hi Mohammad,

    thanks for taking the time to look at this.  All working now (a slightly altered approach did the trick) so the first free seat on this coach is all yours Wink

    • Post Points: 5
Page 1 of 1 (3 items)