Cant figure out why this is not printing

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
const int MAX_CITIES    = 10;
const int MAXLEN        = 20;
const int MAXCOMMANDLEN = 9;
const int NOT_FOUND     = -1;
enum CommandType
{
   ADD, DELETE, UPDATE, OUTPUT, CLEAR, SUMMARIZE,
   TOTAL, SORT, QUIT, UNKNOWN
};
typedef char CityNameType[MAXLEN + 1];
CommandType GetCommand();

class Destination
{
private:
   CityNameType city;
   int package_count;
   float total_weight;

public:

   Destination()
   {
      strcpy( city, "PLATTEVILLE" );
      package_count = 0;
      total_weight  = 0;
   }

   Destination( const CityNameType name )
   {
      strcpy( city, name );
      package_count = 0;
      total_weight  = 0;
   }

   bool DestinationHasName( const CityNameType name ) const
   {
      if ( strcmp ( city, name ) )
         return true;
      else
         return false;
   }
   void ToUpper() const
   {
      CityNameType city;
      for ( int L = 0; L < strlen( city ); L++ )
      {
         if ( city[L] == '_' )
            city[L] = ' ';
         else
            city[L] = toupper ( city[L] );
      }
   }

   void PrintDest() const
   {
      PrintCityName(true);
      cout << setw(4)  << package_count << " packages weighing"; 
      cout << setw(11) << fixed << setprecision(2)
         << total_weight << " pounds" << endl;
   }

   void PrintCityName ( bool rightjustify ) const
   {
      CityNameType ucity;
      strcpy( ucity, city );
      ToUpper();
      if ( rightjustify == true )
         cout << setw(19) << ucity; 
      else 
         cout << ucity;
   }

   void RecordShipment( int numPackages, float packageWeight )
   {
      package_count += numPackages;
      total_weight  += packageWeight;
      SumInTotals ( package_count, total_weight );
   }

   void ClearShipment()
   {
      package_count = 0;
      total_weight  = 0;
   }

   float AverageWeight() const
   {
      float ave = 0.0;
      if ( package_count == 0 )
         ave = 0.0;
      else 
         ave = total_weight / package_count;
      return ave;
   }


   void SumInTotals ( int& packageCountSum, 
      float& packageWeightSum ) const
   {
      packageCountSum  += package_count;
      packageWeightSum += total_weight;
   }

   int CompareToWeight( const Destination & destB )
   {
      if ( destB.total_weight < total_weight )
         return 1;
      else if ( destB.total_weight > total_weight )
         return -1;
      else
         return 0;
   }
   bool LessThanByName ( const Destination& destB ) const
   {
      if ( strcmp ( destB.city, city ) > 0 )
         return true;
      else
         return false;
   }

   int GetPackageCount() const
   {
      return package_count;
   }

   float GetWeightTotal() const
   {
      return total_weight;
   }

};
Last edited on
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

class DestinationList
{
private:
   int numObjects;               // Number of objects in the list
   Destination list[MAX_CITIES]; // arry of destinations


   int Find( const CityNameType name ) const
   {
      for( int i = 0; i < numObjects; i++ )
         if ( list[i].DestinationHasName( name ) )
            return i;
      return NOT_FOUND;
   }

   void Swap( Destination& Dest1, Destination& Dest2 ) 
   {
      Destination temp = Dest1;
      Dest1 = Dest2;
      Dest2 = temp;
   }
   void SortByWeight()
   {
      for ( int pass = 0; pass < numObjects - 1; pass++ )
      {
         int maxIndex = pass;
         for ( int count = pass + 1; count < numObjects; count ++ )
            if ( list[count].CompareToWeight( list[maxIndex] ) )
               maxIndex = count;
         Destination weightTemp = list[maxIndex];
         list[maxIndex] = list[pass];
         list[pass] = weightTemp;
      }
   }   
   void SortByName() 
   {
      for ( int pass = 0; pass < numObjects - 1; pass++ )
      {
         int minIndex = pass;
         for ( int count = pass + 1; count < numObjects; count ++ )
            if ( list[count].LessThanByName( list[minIndex] ) )
               minIndex = count; 
         Destination nameTemp = list[minIndex];
         list[minIndex] = list[pass];
         list[pass] = nameTemp;
      }
   }

public:

   void NumberInDestinationList()
   {
      numObjects = 0;
   }


   void AddCity()
   {
      CityNameType city;
      cin >> city;

      Destination location( city );
      int index = Find(city);

      if ( numObjects < MAX_CITIES && index == NOT_FOUND )
      {
         list[numObjects] = location;
         list[numObjects].ToUpper();
         numObjects++;
         cout << city << " added to the list." << endl;
      }
      else if ( numObjects >= MAX_CITIES )
         cout << city << " not added. List is full." << endl;
      else
         cout << city << " is already in the list."  << endl;
   }

   void UpdateCity()
   {
      CityNameType city;
      cin >> city;

      int numPackages = 0;
      float weight    = 0;

      int index = Find(city);
      cin >> numPackages >> weight;

      if (index != NOT_FOUND)
      {
         list[index].RecordShipment( numPackages, weight);
         cout << "Destination " << city << " updated with "
            << numPackages << " packages weighing "
            << fixed << showpoint << setprecision(2) << weight
            << " lbs." << endl;
      }
      if ( index == NOT_FOUND )
         cout << "Cannot update. " << city << " is not in the list. "
         << endl;
   }

   void Output() const
   {
      CityNameType city;

      cin >> city;

      int index = Find( city );
      if ( index == NOT_FOUND )
         cout << "Cannot output. " << city << " is not in the list."
         << endl;
      else
      {
         if ( index != NOT_FOUND )
            cout << "Average weight of all packages to " << city << ": "
            << fixed << showpoint << setprecision(2)
            << list[index].AverageWeight() << endl; 
      }
   }

   void Total()
   {
      int packageCount    = 0;
      float packageWeight = 0;

      for ( int L = 0; L < numObjects; L++ )
      {
         packageCount  += list[L].GetPackageCount();
         packageWeight += list[L].GetWeightTotal();
      }
      cout << "Total package count for all cities is " << packageCount 
         << "." << endl;
      cout << "Total package weight for all citites is " 
         << packageWeight << "." << endl;
   }

   void sort()
   {
      CityNameType nameOrWeight;

      if ( strcpy ( nameOrWeight, "weight")  )
         SortByWeight();
      else if ( strcpy ( nameOrWeight, "name" ) )
         SortByName();
      else 
         cout << "Bad Option given: " << nameOrWeight
              << endl;
   }

   void Clear()
   {
      CityNameType city;
      cin >> city;

      int index = Find( city );

      if ( index != NOT_FOUND )
         list[index].ClearShipment();
      else if ( index == NOT_FOUND )
         cout << "Cannot clear. " << city << " is not in the list." 
         << endl;
   }

   bool Delete()
   {
      CityNameType city;
      cin >> city;
      for ( int L = 0; L < strlen( city ); L++ )
         city[L] = toupper ( city[L] );

      int deletingIndex = Find(city);
      if ( deletingIndex != NOT_FOUND )
      {
         --numObjects;
         for ( int W = deletingIndex; W < numObjects; W++ )
            list[W] = list[W + 1];
         return true;
      }
      else if ( deletingIndex == NOT_FOUND )
         cout << "Cannot delete. " << city << " is not in the list." 
              << endl;
         return false;
   }

   void Quit()
   {
      cout << "Normal termination.";
      cout << endl;
   }


   void Summarize()
   {
      for ( int i = 0; i < numObjects; i++ )
         list[i].PrintDest();
   }

   void Unknown()
   {
      cout << "Unrecognized command given.";
      cout << endl;
   }
};

int main()
{
   DestinationList list; //Uses the default constructor 
   CommandType command = GetCommand();
   switch (command)
   {
   case ADD: list.AddCity();
      break;
   case DELETE: list.Delete();
      break;
   case UPDATE: list.UpdateCity();
      break;
   case OUTPUT: list.Output();
      break;
   case SUMMARIZE: list.Summarize();
      break;
   case TOTAL: list.Total();
      break;
   case SORT: list.sort();
      break;
   case QUIT: list.Quit();
      break;
   case UNKNOWN: list.Unknown();
      break;
   }
   return 0;
}
   CommandType GetCommand()
   {
   char command [MAXCOMMANDLEN + 1];
   cin >> command;
   if ( strcmp ( command, "Add" ) == 0 )
      return ADD;
   else if ( strcmp ( command, "Update" ) == 0 )
      return UPDATE;
   else if ( strcmp ( command, "Clear" ) == 0 )
      return CLEAR;
   else if ( strcmp ( command, "Delete" ) == 0 )
      return DELETE;
   else if ( strcmp ( command, "Sort" ) == 0 )
      return SORT;
   else if ( strcmp ( command, "Total" ) == 0 )
      return TOTAL;
   else if ( strcmp ( command, "Output" ) == 0 )
      return OUTPUT;
   else if ( strcmp ( command, "Summarize" ) == 0 )
      return SUMMARIZE;
   else if ( strcmp ( command, "Quit" ) == 0 )
      return QUIT;
   else 
      return UNKNOWN;
   }
Topic archived. No new replies allowed.