Friday, December 23, 2011

cs201 3 Introduction to Programming Assignment Solution fall december 2011

#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <fstream.h>
using namespace std;
int random(int i)
{
    return rand()%i;
}
struct Reservation
{
       string Passenger_Name;
       string Passenger_Address;
       string Date_of_Flight;
       string Destination_to_travel;
       int Flight_No;
       int Seat_No;
};
void GetData(Reservation[],int);
void WriteData(Reservation[], int);
void ReadData(Reservation[], int); 
main()
{
      Reservation *res;
      int i;
      cout<<"How many reservation you want: ";
      cin>>i;
      res = new Reservation[i];
    
      GetData(res, i);
     
      system("pause");
     
}

void GetData(Reservation st[], int nn)
{
     string n;
     char ch;
     int l;
     for(int j=1;j<=nn;j++)
     {
             cout<<"Enter Passenger Name for Passenger "<<j<<" : ";
             cin>>n;
             st[j-1].Passenger_Name=n;
             cout<<"Enter Passenger Address for Passenger "<<j<<" : ";
             cin>>n;
             st[j-1].Passenger_Address=n; 
             cout<<"Enter Date of Flight for Passenger "<<j<<" : ";
             cin>>n;
             st[j-1].Date_of_Flight=n;
             cout<<"Enter Destination_to_travel for Passenger "<<j<<" : ";
             cout<<"Please Select from given"<<endl;
             cout<<"k       for         karachi"<<endl;
             cout<<"p       for         peshawar"<<endl;
             cout<<"L       for         lahore"<<endl;
             cin>>ch;
             if( ch == 'p')
             {
                 st[j-1].Destination_to_travel="Peshawar";
                 st[j-1].Flight_No=233; 
                 st[j-1].Seat_No=random(200);
             }
             if( ch == 'k')
             {
                 st[j-1].Destination_to_travel="Karachi";
                 st[j-1].Flight_No=201;
                 st[j-1].Seat_No=random(200); 
             }
             if( ch == 'l')
             {
                 st[j-1].Destination_to_travel="Lahore";
                 st[j-1].Flight_No=241; 
                 st[j-1].Seat_No=random(200);
             }
             else if( !(ch == 'p' || ch == 'k' || ch == 'l'))
             {
                       cout<<"Please select from list:"<<endl;
             }      
     }
     WriteData(st, nn);
}

void WriteData(Reservation abc[], int nn)
{
     ofstream myfile ("abc.txt");
      if (myfile.is_open())
      {
           for(int j=1;j<=nn;j++)
           {  
                myfile <<abc[j-1].Passenger_Name<<endl;
                myfile <<abc[j-1].Passenger_Address<<endl;
                myfile <<abc[j-1].Date_of_Flight<<endl;
                myfile <<abc[j-1].Destination_to_travel<<endl;
                myfile <<abc[j-1].Flight_No<<endl;
                myfile <<abc[j-1].Seat_No<<endl;
          
           }
           myfile.close();
      }
      else cout << "Unable to open file";
     ReadData(abc, nn);
}

void ReadData(Reservation[], int) 
{
       string line;
      ifstream myfile ("abc.txt");
      if (myfile.is_open())
      {
        while ( myfile.good() )
        {
          getline (myfile,line);
          cout << line << endl;
        }
        myfile.close();
      }
   
      else cout << "Unable to open file";
   
    
}

No comments:

Post a Comment