Help with Search Funtion

Hello,

Really new to this topic. I have to read the data from one file, which i was able to due, but when it comes to reading a second file and using a search function to find matches from the first file is where i am lost.

here is the type of data in the first file.

                                                                
CXF341 toyoTA caMRy s
rmj441 NiSSan verSA C
ZmW562 hYundai  Tucson S
987abcd ford fiEsta C


the second will only have plate numbers.

                                                                     
CXF341
rmj441 
rmzddd
987abcd 


How do create a search that will find a match and bring back the info from file one?


Here is the output that is needed.

===========================================
===========================================

License#  Make      Model     Car Type  Status    
RMJ441    Nissan    Versa     Compact   Available 
ZMW562    Hyundai   Tucson    Standard  Available 
987ABCD   Ford      Fiesta    Compact   Available 
CXF341    Toyota    Camry     Standard  Available 

Processing Rentals                  
License# CXF341   Toyota    Camry   Rented
License# RMJ441    Nissan    Versa  Rented
Error  RMZDDD Could not be found
License# 987ABCD   Ford      Fiesta  Rented

License#  Make      Model     Car Type  Status    
RMJ441    Nissan    Versa     Compact   Rented
ZMW562    Hyundai   Tucson    Standard  Available 
987ABCD   Ford      Fiesta    Compact   Rented
CXF341    Toyota    Camry     Standard  Rented


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

const int MAX=40; // max amounts of car rentals
const int N=10; // this helps with formating
const int L=20; // this helps with columns with

struct apttype //this will hold the data from the 1st input file                                                                              
{
  string Make;         //Make of the car                                                                
  string Model;        //average points per game                                                     
  string LicenseNum;      //Plate Numbers                                                                 
  char cartype;		//this will read the letter of compact or standard....
  string cartypename;  // this will hold the name of compact or standard....
  string Status;         // this will let hold the available status
};

struct apttype_Rented //this will hold the inventory data                                                                       
{
  string Make2;                                                                       
  string Model2;                                                           
  string LicenseNum2;                                                                      
  char cartype2;
  string cartypename2;
  string Status2;
};

int search(apttype[], int, string); // this will search the invertory list to the 1st input
void read_apt_data(apttype[],int&); // this will read the 1st input file
void read_apt_data_Rented(apttype[],apttype_Rented[],int&, int&); // this will read the inventory
void print_report(apttype[],apttype_Rented[], int, int); // this will print to the output file
void fixname(string&);// this will fix the names of make and models
void fixnameCapital(string&); // this will capitalize all plate numbers
void sort(apttype[],int); // this was added but not sure if ill sort the list. 

int main()
{
  apttype myapts[MAX]; //list max from 1st input file  
  apttype_Rented myapts2[MAX]; //list max from 2nd input file                                                             

  int n=0;               //# of elements in 1st input                                                                 
  int m=0;		//# of elements in 2nd input

  read_apt_data(myapts,n);
  read_apt_data_Rented(myapts,myapts2,n,m);
  sort(myapts,n);
  print_report(myapts,myapts2,n,m);

  return 0;
}
void fixname(string& name) // this will Capitalized only the first letter only
{
  name[0] = toupper(name[0]);
  for (int i=1;i<name.length();i++)
    name[i]=tolower(name[i]);
}
void fixnameCapital(string& name) // this will make all the letters capitalized.
{
  for (int i=0;i<name.length();i++)
    name[i]=toupper(name[i]);
}
void read_apt_data(apttype apts[],int& n) // this will ask for the 1st input file
{
  ifstream infile;
  string filename;
  ofstream out;


  cout << "Enter the Name of the Input file" << endl;
  cin >> filename;

  infile.open(filename.c_str());
  string cartypename;
  string Status = "Available";
  n = 0;
  if(infile.is_open())
    {
      infile >>apts[n].LicenseNum;
      while (infile)
        {
          fixnameCapital(apts[n].LicenseNum);
          infile >> apts[n].Make;
          fixname(apts[n].Make);
          infile >> apts[n].Model;
          fixname(apts[n].Model);
          infile >> apts[n].cartype;
          if (toupper(apts[n].cartype) == 'S')
            apts[n].cartypename = "Standard";
          else if (toupper(apts[n].cartype) == 'C')
            apts[n].cartypename = "Compact";
          else if (toupper(apts[n].cartype) == 'P')
            apts[n].cartypename = "Premium";
          apts[n].Status  = "Available";
          n++;
          infile >>apts[n].LicenseNum;
        }
      infile.close();


    }
  else
    cout<<"Error: file\"" <<filename<<"\" was not opened\n";

}
void read_apt_data_Rented(apttype apts[],apttype_Rented apts2[],int& n, int& m)
{
  ifstream infile2;
  string filename2;

  cout << "Enter the Name of the Inventory file" << endl;
  cin >> filename2;

  infile2.open(filename2.c_str());
  string cartypename2;
  string Status2 = "Rented";
  n = 0;
  m = 0;

  if(infile2.is_open())
    {
      infile2>>apts2[m].LicenseNum2;
      while (infile2)
        {
          fixnameCapital(apts2[m].LicenseNum2);
          apts2[m].Status2  = "Rented";
          m++;
          infile2>>apts2[m].LicenseNum2;
        }
      infile2.close();


    }
  else
    cout<<"Error: file\"" <<filename2<<"\" was not opened\n";

}

void print_report(apttype apts[], apttype_Rented apts2[] ,int n, int m)
{
  ofstream out;
  string outfilename;

  cout <<"Enter the name of the output file \n";
  cin >> outfilename;

  out.open(outfilename.c_str(),ios::app);

  out<< "==========================================="<< endl;
  out<< "==========================================="<< endl<<endl;

  out<<endl<<left<<setw(N)<<"License#"
     <<setw(N)<<"Make"
     <<setw(N)<<"Model"
     <<setw(N)<<"Car Type"
     <<setw(N)<<"Status"
     <<endl;

  for (int i=0; i<n; i++)
    {
      out<<left<<setw(N)<<apts[i].LicenseNum
         <<setw(N) <<apts[i].Make
         <<setw(N)<<apts[i].Model
         <<setw(N)<< apts[i].cartypename
         <<setw(N)<<apts[i].Status<<endl;
    }

/* This is where i need the search to be called were it would take input file 2
and find the matches inside of the 1st file. */

  int place;
  string target = "";
  string myapts;

  for(int j=0; j<m; j++)
    {
      apts2[j]>>target;
      out<<target<<"";
      place = search(myapts, n, target);
        if (place >= 0)
          out<<"found at position "<< place;
        else
          out<<"not found"; out<<endl;
    }

  out.close();

}

void sort(apttype myapts[],int n)
{
  apttype temp; //place holder when values are interchanged                                             \
                                                                                                         

  for (int i=0; i < n-1; i++)
    for (int j=0; j < n-(i+1); j++)
      if (myapts[j].Model < myapts[j+1].Model)
        {
          temp = myapts[j];
          myapts[j] = myapts[j+1];
          myapts[j+1] = temp;
        }
}

int search(string myapts[], int n, string target) //This will search the inventory 
{
  for(int i = 0; i < n; i++)
    if (myapts[i]==target)
      return i;
  return -1;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* This is where i need the search to be called were it would take input file 2
and find the matches inside of the 1st file. */

  int place;
  string target = "";
  string myapts;

  for(int j=0; j<m; j++)
    {
      apts2[j]>>target; //¿?
      out<<target<<"";
      place = search(myapts, n, target); //¿myapts?
        if (place >= 0)
          out<<"found at position "<< place;
        else
          out<<"not found"; out<<endl;
    }
you've got functions read_apt_data{,_Rented}() that parse your input files and fill your arrays.
once they are called, forget that the files even exist, work with the arrays.

so you have an array of cars (`apttype' is a terrible name, by the way) and you want to search according to one field, the `LicenseNum'
1
2
3
4
//pseudocode
for x in cars:
   if x.LicenseNum == target:
      return x

So your prototype should be something like int search(apttype /*cars*/[], int /*size*/, string /*target*/);
... wait, you do have that one prototype, ¿why then you implement int search(string myapts[], int n, string target)?


by the way, ¿what's the difference between `struct apttype' and `struct apttype_Rented'?
¿what's the difference between `struct apttype' and `struct apttype_Rented'?

apttype will hold the data from the 1st file. apttype_Rented will hold the plate numbers from the second file.

So I'm not sure how to use the search prototype to find the matched items and output this information.


Processing Rentals                  
License# CXF341   Toyota    Camry   Rented
License# RMJ441    Nissan    Versa  Rented
Error  RMZDDD Could not be found
License# 987ABCD   Ford      Fiesta  Rented

License#  Make      Model     Car Type  Status    
RMJ441    Nissan    Versa     Compact   Rented
ZMW562    Hyundai   Tucson    Standard  Available 
987ABCD   Ford      Fiesta    Compact   Rented
CXF341    Toyota    Camry     Standard  Rented
apttype will hold the data from the 1st file. apttype_Rented will hold the plate numbers from the second file.

That doesn't answer the question. What's the difference between them? As far as I can see, they both hold exactly the same types of data members, corresponding to exactly the same sorts of information. Why do you have two different structures, for what looks like the same data quantities. What is the difference between them?
Sorry, my initial thoughts were that there was some way of copying the data over to the corresponding matches. That is the reason for them being the same. As you can tell I am a complete newborn at this.

My point is that I don't understand why you need two different types, when you have identical data quantities, with identical meanings, in both. Why can't you use the same data type for both files?
Last edited on
so your right and it was a bad idea, i good example of how lost i am.

so my struct apttype allong with my void read_apt_data. build this table below with out issues.


===========================================

License#  Make      Model     Car Type  Status    
RMJ441    Nissan    Versa     Compact   Available 
ZMW562    Hyundai   Tucson    Standard  Available 
987ABCD   Ford      Fiesta    Compact   Available 
CXF341    Toyota    Camry     Standard  Available


how would i read the second file with only plate numbers and use the search function to find a match and create these out puts?



Processing Rentals                  
License# CXF341   Toyota    Camry   Rented
License# RMJ441    Nissan    Versa  Rented
Error  RMZDDD Could not be found
License# 987ABCD   Ford      Fiesta  Rented

License#  Make      Model     Car Type  Status    
RMJ441    Nissan    Versa     Compact   Rented
ZMW562    Hyundai   Tucson    Standard  Available 
987ABCD   Ford      Fiesta    Compact   Rented
CXF341    Toyota    Camry     Standard  Rented
I already gave you the pseudocode to return the car, if you want to return the index
1
2
3
4
5
search(cars, license_num):
   for x in range(0, size(cars)):
      if cars[x].LicenseNum == license_num:
         return x
   return -1
now, to rent the cars
1
2
3
4
5
6
7
8
for q in queries:
   index = search(cars, q)
   if index==-1:
      error("not found")
   elif cars[index].Status == "Rented":
      error("already rented")
   else:
      cars[index].Status = "Rented"
ne555,

So i did my best to add your pseudo-code and the link the search index to the 1st generated array but i am need of more help. as you can see my output is still broken.


===========================================
===========================================


License#  Make      Model     Car Type  Status    
RMJ441    Nissan    Versa     Compact   Available 
ZMW562    Hyundai   Tucson    Standard  Available 
987ABCD   Ford      Fiesta    Compact   Available 
CXF341    Toyota    Camry     Standard  Available 

Processing Rentals
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.
Error: could not be found.


this is what i need it to due.


===========================================
===========================================


License#  Make      Model     Car Type  Status    
RMJ441    Nissan    Versa     Compact   Available 
ZMW562    Hyundai   Tucson    Standard  Available 
987ABCD   Ford      Fiesta    Compact   Available 
CXF341    Toyota    Camry     Standard  Available 

Processing Rentals                  
License# CXF341   Toyota    Camry   Rented
License# RMJ441    Nissan    Versa  Rented
Error  RMZDDD Could not be found
License# 987ABCD   Ford      Fiesta  Rented


here is my updated code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

const int MAX=40; //The max of my array.                                            
const int N=10; //Used to fix the spacing 
const int L=20; // Used to fix the column with

struct Car_Information //This will hold the data from input 1 and the plates to search from input 2.                                                  
{
  string Make_of_Car;         // This will come from the 1st input file                              
  string Model_of_Car;        // This will come from the 1st input file                      
  string License_Plate;      // This will come from the 1st input file                                  
  char cartype; 	    //This will come from the 1st input file
  string cartypename;     // this will hold the name of compact or standard....
  string Status_Available;
  string Status_Rented;
  string License_Plate_Rented;  // this will come from the 2nd input file that will be searched   
};

int search(Car_Information[], int, string);
void read_apt_data(Car_Information[],int&, int&);
void print_report(Car_Information[], int, int);
void fixname(string&);
void fixnameCapital(string&);
void sort(Car_Information[],int);

int main()
{
  Car_Information Table_of_Cars[MAX]; //list players
                                  
  int n=0;               //# of cars in inventory 
  int m=0;  		//# of license plates to search. 

  read_apt_data(Table_of_Cars,n,m);
  sort(Table_of_Cars,n);
  print_report(Table_of_Cars,n,m);

  return 0;
}
void fixname(string& name)
{
  name[0] = toupper(name[0]);
  for (int i=1;i<name.length();i++)
    name[i]=tolower(name[i]);
}
void fixnameCapital(string& name)
{
  for (int i=0;i<name.length();i++)
    name[i]=toupper(name[i]);
}
void read_apt_data(Car_Information Table_Info[],int& n, int& m)
{
  ifstream infile;
  string filename;
  ofstream out;

// this will create the first table of info from input data file one. 

  cout << "Enter the Name of the Inventory file" << endl;
  cin >> filename;

  infile.open(filename.c_str());
  string cartypename;
  string Status_Available = "Available";
  n = 0;
  if(infile.is_open())
    {
      infile >>Table_Info[n].License_Plate;
      while (infile)
        {
          fixnameCapital(Table_Info[n].License_Plate);
          infile >> Table_Info[n].Make_of_Car;
          fixname(Table_Info[n].Make_of_Car);
          infile >> Table_Info[n].Model_of_Car;
          fixname(Table_Info[n].Model_of_Car);
          infile >> Table_Info[n].cartype;
          if (toupper(Table_Info[n].cartype) == 'S')
            Table_Info[n].cartypename = "Standard";
          else if (toupper(Table_Info[n].cartype) == 'C')
            Table_Info[n].cartypename = "Compact";
          else if (toupper(Table_Info[n].cartype) == 'P')
            Table_Info[n].cartypename = "Premium";
          Table_Info[n].Status_Available  = "Available";
          n++;
          infile >>Table_Info[n].License_Plate;
        }
      infile.close();


    }
  else
    cout<<"Error: file\"" <<filename<<"\" was not opened\n";
 
// this will open file 2 that will had the plate numbers to search. 

  ifstream infile2;
  string filename2;
  ofstream out2;


  cout << "Enter the Name of the Rental file" << endl;
  cin >> filename2;

  infile2.open(filename2.c_str());
  m = 0;

  if(infile2.is_open())
    {
      infile2 >>Table_Info[m].License_Plate_Rented;
      while (infile2)
        {
          fixnameCapital(Table_Info[m].License_Plate_Rented);
          Table_Info[m].Status_Rented  = "Rented";
          m++;
          infile2 >>Table_Info[m].License_Plate_Rented;
        }
      infile2.close();


    }
  else
    cout<<"Error: file\"" <<filename2<<"\" was not opened\n";





}

void print_report(Car_Information Table_Info[],int n, int m)
{
  ofstream out;
  string outfilename;
  
  cout <<"Enter the name of the output file \n";
  cin >> outfilename;
  
  out.open(outfilename.c_str(),ios::app);
  
  out<< "==========================================="<< endl;
  out<< "==========================================="<< endl<<endl;

  out<<endl<<left<<setw(N)<<"License#"
     <<setw(N)<<"Make"
     <<setw(N)<<"Model"
     <<setw(N)<<"Car Type"
     <<setw(N)<<"Status_Available"
     <<endl;

  for (int i=0; i<n; i++)
    {
      out<<left<<setw(N)<<Table_Info[i].License_Plate
         <<setw(N) <<Table_Info[i].Make_of_Car
         <<setw(N)<<Table_Info[i].Model_of_Car
         <<setw(N)<< Table_Info[i].cartypename
         <<setw(N)<<Table_Info[i].Status_Available<<endl;
    }

  out<<endl<< "Processing Rentals"<<endl;

  int  place=0;
  string target = "";

  for(int j=0; j<n; j++)
    {
      int x=0;
      while ( x<m)
        {

          Table_Info[x].License_Plate_Rented = target;
          place = search(Table_Info, n, target);
          x++;
        if (place >= 0)
             out<<left<<setw(N)<<"License# "
                <<Table_Info[place].License_Plate
                <<setw(N) <<Table_Info[place].Make_of_Car
                <<setw(N)<<Table_Info[place].Model_of_Car
                <<setw(N)<<Table_Info[place].Status_Rented <<endl;
        else

          out<<"Error: "<< target<< "could not be found."<<endl;
     

        }
    }

  out.close();

}

void sort(Car_Information Table_of_Cars[],int n)
{
  Car_Information temp; //place holder when values are interchanged                                              

  for (int i=0; i < n-1; i++)
    for (int j=0; j < n-(i+1); j++)
      if (Table_of_Cars[j].Model_of_Car < Table_of_Cars[j+1].Model_of_Car)
        {
          temp = Table_of_Cars[j];
          Table_of_Cars[j] = Table_of_Cars[j+1];
          Table_of_Cars[j+1] = temp;
        }
}

int search(Car_Information Table_of_Cars[], int n, string target)
{
 
  for(int i = 0; i < n; i++)
    if (Table_of_Cars[i].License_Plate == target)
      return i;
  return -1;
}

Last edited on
line 175 should be target = Table_Info[x].License_Plate_Rented;

that will make it "work", but you still have some issues
1
2
3
4
5
6
7
8
9
struct Car_Information
{
  string Make_of_Car;
  string Model_of_Car;
  string License_Plate;
  char cartype;
  string cartypename; // this will hold the name of compact or standard....
  string Status; //may hold "Rented" or "Available", could also be just a bool
};
that's all you need to make the inventory

1
2
3
4
Car_Information Table_of_Cars[MAX]; //this will hold the data from input 1, later will be updated with rent operations
std::string to_rent[MAX]; //filled with input 2
//void read_apt_data(Car_Information Table_Info[],int& n, std::string to_rent[], int& m);
read_apt_data(Table_of_Cars, n, to_rent, m);
keep in mind that the files may have different length.
also, the second file has only license_numbers.

then in line 169 you have
1
2
3
4
  for(int j=0; j<n; j++)
    {
      int x=0;
      while ( x<m)
¿why nested loop? you only need to traverse the license plates to_rent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int x = 0; x < m; ++x) {
  target = to_rent[x];
  place = search(Table_Info, n, target);
  if (place >= 0)
    Table_Info[place].Status = "Rented";
  else
    out << "Error: " << target << "could not be found." << endl;
}
//then you print the final status
for (int i = 0; i < n; i++) {
  out << left << setw(N) << apts[i].LicenseNum << setw(N) << apts[i].Make
      << setw(N) << apts[i].Model << setw(N) << apts[i].cartypename << setw(N)
      << apts[i].Status << endl;
}
Thank you, but my last table wont update some of the "Available" to Rented.



===========================================
===========================================


License#  Make      Model     Car Type  Status    
RMJ441    Nissan    Versa     Compact   Available 
ZMW562    Hyundai   Tucson    Standard  Available 
987ABCD   Ford      Fiesta    Compact   Available 
CXF341    Toyota    Camry     Standard  Available 

Processing Rentals
License#  987ABCD   Ford      Fiesta    Rented    
Error: RMZDDDcould not be found.
License#  RMJ441    Nissan    Versa     Rented    
License#  CXF341    Toyota    Camry     Rented    

License#  Make      Model     Car Type  Status    
987ABCD   Ford      Fiesta    Compact   Rented    
RMJ441    Nissan    Versa     Compact   Rented    
CXF341    Toyota    Camry     Standard  Rented    

Daily Rental Due is $43.85
3 car(s) is(are) Currently Rented.
1 car(s) is(are) Currently Available.

License#  Make      Model     Car Type  Status    
RMJ441    Nissan    Versa     Compact   Available 
ZMW562    Hyundai   Tucson    Standard  Available 
987ABCD   Ford      Fiesta    Compact   Available 
CXF341    Toyota    Camry     Standard  Available 



this is what the final out put should be.

===========================================
===========================================


License#  Make      Model     Car Type  Status    
RMJ441    Nissan    Versa     Compact   Available 
ZMW562    Hyundai   Tucson    Standard  Available 
987ABCD   Ford      Fiesta    Compact   Available 
CXF341    Toyota    Camry     Standard  Available 

Processing Rentals
License#  987ABCD   Ford      Fiesta    Rented    
Error: RMZDDDcould not be found.
License#  RMJ441    Nissan    Versa     Rented    
License#  CXF341    Toyota    Camry     Rented    

License#  Make      Model     Car Type  Status    
987ABCD   Ford      Fiesta    Compact   Rented    
RMJ441    Nissan    Versa     Compact   Rented    
CXF341    Toyota    Camry     Standard  Rented    

Daily Rental Due is $43.85
3 car(s) is(are) Currently Rented.
1 car(s) is(are) Currently Available.

License#  Make      Model     Car Type  Status    
RMJ441    Nissan    Versa     Compact   Rented
ZMW562    Hyundai   Tucson    Standard  Available 
987ABCD   Ford      Fiesta    Compact   Rented
CXF341    Toyota    Camry     Standard  Rented


Here is what code i have working at the moments.

Last edited on
Code is listed here due to size.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;

const int MAX=40;                                           
const int N=10;
const int L=20;

struct CarListInfo 
{
  string CarMake;         /                                      
  string CarModel;        //                           
  string PlateNumber;      //                                      
  char cartype; 
  string cartypename;
  string Status;
  string Status2;
  string PlateNumberSearch;   
};

int search(CarListInfo[], int, string);
void read_apt_data(CarListInfo[],int&, int&);
void print_report(CarListInfo[], int, int);
void fixname(string&);
void fixnameCapital(string&);
void sort(CarListInfo[],int);

int main()
{
  CarListInfo CarTable[MAX]; //list players
                                  
  int n=0;               //# of players  
  int m=0;  

  read_apt_data(CarTable,n,m);
  sort(CarTable,n);
  print_report(CarTable,n,m);

  return 0;
}
void fixname(string& name)
{
  name[0] = toupper(name[0]);
  for (int i=1;i<name.length();i++)
    name[i]=tolower(name[i]);
}
void fixnameCapital(string& name)
{
  for (int i=0;i<name.length();i++)
    name[i]=toupper(name[i]);
}
void read_apt_data(CarListInfo CarTableArray[],int& n, int& m)
{
  ifstream infile;
  string filename;
  ofstream out;


  cout << "Enter the Name of the Input file" << endl;
  cin >> filename;

  infile.open(filename.c_str());
  string cartypename;
  string Status = "Available";
  n = 0;
  if(infile.is_open())
    {
      infile >>CarTableArray[n].PlateNumber;
      while (infile)
        {
          fixnameCapital(CarTableArray[n].PlateNumber);
          infile >> CarTableArray[n].CarMake;
          fixname(CarTableArray[n].CarMake);
          infile >> CarTableArray[n].CarModel;
          fixname(CarTableArray[n].CarModel);
          infile >> CarTableArray[n].cartype;
          if (toupper(CarTableArray[n].cartype) == 'S')
            CarTableArray[n].cartypename = "Standard";
          else if (toupper(CarTableArray[n].cartype) == 'C')
            CarTableArray[n].cartypename = "Compact";
          else if (toupper(CarTableArray[n].cartype) == 'P')
            CarTableArray[n].cartypename = "Premium";
          CarTableArray[n].Status = "Available";
          n++;
          infile >>CarTableArray[n].PlateNumber;
       }
      infile.close();


    }
  else
    cout<<"Error: file\"" <<filename<<"\" was not opened\n";
 

  ifstream infile2;
  string filename2;


  cout << "Enter the Name of the Inventory file" << endl;
  cin >> filename2;

  infile2.open(filename2.c_str());
  m = 0;

  if(infile2.is_open())
    {
      infile2 >>CarTableArray[m].PlateNumberSearch;
      while (infile2)
        {
          fixnameCapital(CarTableArray[m].PlateNumberSearch);
          CarTableArray[m].Status2 = "Rented";
          m++;
          infile2 >>CarTableArray[m].PlateNumberSearch;
        }
      infile2.close();

    }
  else
    cout<<"Error: file\"" <<filename2<<"\" was not opened\n";





}

void print_report(CarListInfo CarTableArray[],int n, int m)
{
  ofstream out;
  string outfilename;
  
  cout <<"Enter the name of the output file \n";
  cin >> outfilename;
  
  out.open(outfilename.c_str(),ios::app);
  
  out<< "==========================================="<< endl;
  out<< "==========================================="<< endl<<endl;

  out<<endl<<left<<setw(N)<<"License#"
     <<setw(N)<<"Make"
     <<setw(N)<<"Model"
     <<setw(N)<<"Car Type"
     <<setw(N)<<"Status"
     <<endl;

  int AvailableCount = 0;

  for (int i=0; i<n; i++)
    {
      out<<left<<setw(N)<<CarTableArray[i].PlateNumber
         <<setw(N) <<CarTableArray[i].CarMake
         <<setw(N)<<CarTableArray[i].CarModel
         <<setw(N)<< CarTableArray[i].cartypename
         <<setw(N)<<CarTableArray[i].Status;
      out<<endl;
      AvailableCount++;
    }

  out<<endl<< "Processing Rentals"<<endl;

  int  place=0;
  string target = "";
  int  RentalCount=0;
  double Cost_S, Cost_C, Cost_P;
  int Cost_S_Count=0,Cost_C_Count=0,Cost_P_Count=0;

  for(int x=0; x<m; x++)
    {
      fixnameCapital( CarTableArray[x].PlateNumberSearch);
      target =CarTableArray[x].PlateNumberSearch;
          place = search(CarTableArray, n, target);
       
        if (place >= 0)
          {
            CarTableArray[m].Status2=CarTableArray[place].Status ;

             out<<left<<setw(N)<<"License# "
                <<setw(N)<<CarTableArray[place].PlateNumber
                <<setw(N)<<CarTableArray[place].CarMake
                <<setw(N)<<CarTableArray[place].CarModel
                <<setw(N)<<CarTableArray[place].Status2<<endl;

                RentalCount++;
                if(CarTableArray[place].cartypename == "Standard")
                  {
                  Cost_S= 19.95;
                  Cost_S_Count++;
                  }
                else if(CarTableArray[place].cartypename == "Compact")
                  {
                   Cost_C= 11.95;
                   Cost_C_Count++;
                  }
                else if(CarTableArray[place].cartypename == "Premium")
                  {
                  Cost_P= 39.95;
                  Cost_P_Count++;
                  }
          }
        else

          out<<"Error: "<< target<< "could not be found."<<endl;

    }

  out<<endl<<left<<setw(N)<<"License#"
     <<setw(N)<<"Make"
     <<setw(N)<<"Model"
     <<setw(N)<<"Car Type"
     <<setw(N)<<"Status"
     <<endl;

  for(int w=0; w<m; w++)
    {

      fixnameCapital( CarTableArray[w].PlateNumberSearch);
      target =CarTableArray[w].PlateNumberSearch;
          place = search(CarTableArray, n, target);

        if (place >= 0)
          {
            CarTableArray[m].Status2=CarTableArray[place].Status ;

            out<<left <<setw(N)<<CarTableArray[place].PlateNumber
               <<setw(N)<<CarTableArray[place].CarMake
               <<setw(N)<<CarTableArray[place].CarModel
               <<setw(N)<<CarTableArray[w].cartypename
               <<setw(N)<<CarTableArray[place].Status2<<endl;
          }

      
    }

double sum = (Cost_S * Cost_S_Count) + (Cost_C * Cost_C_Count) + (Cost_P * Cost_P_Count);
  int NotRented = AvailableCount - RentalCount;

  out<<endl<<"Daily Rental Due is $"<< sum << endl;
  out<<RentalCount<<" car(s) is(are) Currently Rented."<<endl;
  out<<NotRented<<" car(s) is(are) Currently Available."<<endl;

  out<<endl<<left<<setw(N)<<"License#"
     <<setw(N)<<"Make"
     <<setw(N)<<"Model"
     <<setw(N)<<"Car Type"
     <<setw(N)<<"Status"
     <<endl;

  for (int a=0; a<n; a++)
    {
      out<<left<<setw(N)<<CarTableArray[a].PlateNumber
         <<setw(N) <<CarTableArray[a].CarMake
       <<setw(N)<<CarTableArray[a].CarModel
         <<setw(N)<< CarTableArray[a].cartypename
         <<setw(N)<<CarTableArray[a].Status <<endl;
    }
  out.close();

}

void sort(CarListInfo CarTable[],int n)
{
  CarListInfo temp; //place holder interchanged                                              

  for (int i=0; i < n-1; i++)
    for (int j=0; j < n-(i+1); j++)
      if (CarTable[j].CarModel < CarTable[j+1].CarModel)
        {
          temp = CarTable[j];
          CarTable[j] = CarTable[j+1];
          CarTable[j+1] = temp;
        }
}
int search(CarListInfo CarTable[], int n, string target)
{
 
  for(int i = 0; i < n; i++)
    if (CarTable[i].PlateNumber == target)
      return i;
  return -1;
}
Topic archived. No new replies allowed.