Thread: Array Problem

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Question Array Problem

    hi

    how can i revise the code below so that as well as being able to book a particular hour in the day, I can also request the first free hour of the day?

    thanks

    #include <iostream.h>
    #include <string.h>
    #include<conio.h>

    struct book
    {
    char time[5];
    bool status;
    };

    //the array
    void makebook(book z[])
    {
    strcpy(z[0].time,"0900");
    strcpy(z[1].time,"1000");
    strcpy(z[2].time,"1100");
    strcpy(z[3].time,"1200");
    strcpy(z[4].time,"1300");
    strcpy(z[5].time,"1400");
    strcpy(z[6].time,"1500");
    strcpy(z[7].time,"1600");
    strcpy(z[8].time,"1700");

    z[0].status=true;
    z[1].status=true;
    z[2].status=true;
    z[3].status=true;
    z[4].status=true;
    z[5].status=true;
    z[6].status=true;
    z[7].status=true;
    z[8].status=true;
    }

    void printbook(book z[])
    {
    for(int i=0;i<9;i++)
    {
    cout<<i+1<<" "<<z[i].time<<"\t";

    if(z[i].status!=true)
    cout<<"Booked"<<endl;
    else
    cout<<""<<endl;
    }
    }

    void main ()
    {
    book ob[9];
    makebook(ob);

    printbook(ob);

    char date[11];

    cout<<"enter the date in dd/mm/yy format : ";
    cin>>date;

    bool ask=true;

    while(ask==true)
    {
    cout<<"Enter number of hour to enter appointment : ";

    int num;
    cin>>num;
    num = num - 1;

    //checks to see if time already booked
    if(ob[num].status==false)
    cout<<ob[num].time<<" is already booked"<<endl;
    else
    {
    cout<<ob[num].time<<" is now booked "<<endl;
    ob[num].status=false;
    }
    cout<<"Enter another appointment ? (y/n) : ";
    char temp;
    cin>>temp;
    if(temp=='n')
    ask=false;
    }
    // display output on monitor
    cout<<"\nAppointments for "<<date<<endl;
    cout<<"\n";
    printbook(ob);
    cout<<"\n";
    }

  2. #2
    Unregistered
    Guest
    It looks like you need to make use of the FOR loop. Not only will this make initialization much cleaner, but you can also use it to detect for an unbooked hour.


    // initialization
    for( int i = 0; i < 8; i++ )
    x[i].status = true;

    I think you've got enough skill to figure out how to check for the first unbooked hour if you use FOR.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM