dimanche 28 février 2016

Program not going into tthe IF condition. Involve struct

    #include <iostream>
    #include <string>
    using namespace std;

    struct BBQ
    {
       int BBQNumber;  // Pit Number
       string cal_date; // calendar date
       bool available; // status of pit - true if available for booking
    };

    const int SIZE = 3;
    BBQ pits[365][SIZE]; //2-D array that store the pits information for    the year

    void initialize();
    void display();
    BBQ findBBQ(string);


    int main()
    {
        string choice = "-";

        initialize();
        display();
        while (choice != "e")
        {
            cout << "Enter Reservation Date: ";
            cin >> choice;

            findBBQ(choice);
        }
    }

    void initialize()
    {

        BBQ pt[3];
        pt[0].BBQNumber = 10;
        pt[0].available = 1;
        pt[0].cal_date = "jan";

        pt[1].BBQNumber = 11;
        pt[1].available = 1;
        pt[1].cal_date = "feb";

        pt[2].BBQNumber = 12;
        pt[2].available = 1;
        pt[2].cal_date = "mar";

       for (int i = 0; i < 3; i++)
       {
           for (int j = 0; j < SIZE; j++)
           {
                pits[i][j] = pt[i];
           }
       }
    }

    BBQ findBBQ(string date)
    {
          for (int i = 0; i < 3; i++)
          {
              for (int j = 0; j < SIZE; j++)
              {
                  if (pits[i][j].cal_date == date)
                  {
                      cout << "Avail" << endl;
                      return pits[i][j];
                  }
                  else
                  {
                      cout << "Unavail" << endl;
                      return pits[i][j];
                  }
              }
          }
     }

     void display()
     {
         for (int i = 0; i < 3; i++)
         {
              for (int j = 0; j < SIZE; j++)
              {
              cout << pits[i][0].BBQNumber << endl;
              cout << pits[i][j].cal_date << endl;
             }
             cout << endl;
         }
     }

My problem lies in the findBBQ() function. I am trying to enter a date, and have the program run through the array and print if such a date is available. For some reason, the program only prints 'avail' when i asked it to search for 'jan' but 'unavail' when i ask for the other 2 months despite having all 3 months in the array as i have initialised.

Aucun commentaire:

Enregistrer un commentaire