Please Help.. -Infinite Loop
For some reason... I get infinite loop saying - "ALPHA is flying with parties" how to stop it? and make it work like it is to be???
Main.cpp
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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <string.h>
#include "Party.h"
#include "ReadFunctions.h"
#include "ReadString.h"
void FlyPlane (Planes);
char * PartyName;
bool LoadPeopleFromLounge (Planes);
Plane Alpha;
Plane Bravo;
Plane Lounge;
Party Temp;
Planes GetPlane ();
Commands Command;
Commands GetCommand ();
Planes WhichPlane;
int Size;
int STD_Alpha;
int STD_Bravo;
int STD_Lounge;
int A(0);
int B(0);
int L(0);
void main ()
{
// cout<<"Enter a Command. \n";
while (true)
{
// Get Command
Command = GetCommand ();
cout<<endl;
// Do Command
switch (Command)
{
case CmdALPHA:
Size = ReadInteger ();
STD_Alpha=Size;
cout << "Size of Alpha is " << Size << endl;
Alpha.NumSeats = Size;
Alpha.NumEmpty = Size;
Alpha.NumParties = 0;
Alpha.Parties = new Party [Size];
break;
case CmdBRAVO:
Size = ReadInteger ();
STD_Bravo=Size;
cout << "Size of Bravo is " << Size << endl;
Bravo.NumSeats = Size;
Bravo.NumEmpty = Size;
Bravo.NumParties = 0;
Bravo.Parties = new Party [Size];
break;
case CmdLOUNGE:
Size = ReadInteger ();
STD_Lounge=Size;
cout << "Size of Lounge is " << Size << endl;
Lounge.NumSeats = Size;
Lounge.NumEmpty = Size;
Lounge.NumParties = 0;
Lounge.Parties = new Party [Size];
break;
case CmdFLY:
cout << "FLY" << endl;
cout<<"Which Plane is Flying?"<<endl;
WhichPlane = GetPlane ();// read in the name of the plane
FlyPlane (WhichPlane);
break;
case CmdARRIVE:
cout << "ARRIVE" << endl;
cout<<"How many people are in this party?"<<endl;
Size = ReadInteger ();
cout<<endl<<"Which Plane did you want to go on?"<<endl;
WhichPlane = GetPlane ();
cout<<endl<<"What is your party's name?"<<endl;
PartyName = ReadString ();
Temp.NumPeople = Size;
Temp.pName = PartyName;
Temp.WhichPlane = WhichPlane;
switch (WhichPlane)
{
case ALPHA:
if(Size>Alpha.NumEmpty)
{
cout<<"No room on plane ALPHA"<<endl;
if(Size>Lounge.NumEmpty)
{
cout<<"No room in Lounge. Goodbye"<<endl;
delete [] PartyName;
break;
}
else
if(Size<=Lounge.NumEmpty)
{
Lounge.Parties+=1;
// L+=1;
Lounge.NumEmpty-=Size;
Lounge.Parties[L].pName = Temp.pName;
Lounge.Parties[L].NumPeople = Temp.NumPeople;
Lounge.Parties[L].WhichPlane= Temp.WhichPlane;
cout<<endl<<"Seats now available in the lounge: "<<Lounge.NumEmpty<<endl;
L+=1;
if(Lounge.NumEmpty==0)
cout<<"Lounge is FULL. Those who cannot board a plane will be turned away"<<endl;
}
}
else
if(Size<=Alpha.NumEmpty)
{
cout<<endl<<PartyName<<" boarding plane ALPHA"<<endl;
Alpha.Parties+=1;
// A+=1;
Alpha.NumEmpty-=Size;
Alpha.Parties[A].pName = Temp.pName;
Alpha.Parties[A].NumPeople = Temp.NumPeople;
Alpha.Parties[A].WhichPlane = Temp.WhichPlane;
cout<<endl<<"Seats now available on ALPHA: "<<Alpha.NumEmpty<<endl;
A+=1;
if(Alpha.NumSeats==0)
cout<<"Plane ALPHA is full and ready to fly"<<endl;
}
break;
case BRAVO:
if(Size>Bravo.NumEmpty)
{
cout<<endl<<"No room in plane BRAVO"<<endl;
if(Size>Lounge.NumEmpty)
{
cout<<"No room in Lounge. SEE YUH!"<<endl;
break;
}
else
if(Size<=Lounge.NumEmpty)
{
Lounge.Parties+=1;
// L+=1;
Lounge.NumEmpty-=Size;
Lounge.Parties[L].pName = Temp.pName;
Lounge.Parties[L].NumPeople = Temp.NumPeople;
Lounge.Parties[L].WhichPlane= Temp.WhichPlane;
cout<<endl<<"Seats now available in the lounge: "<<Lounge.NumEmpty<<endl;
L+=1;
if(Lounge.NumEmpty==0)
cout<<"Lounge is FULL. Those who cannot board a plane will be turned away"<<endl;
}
}
else
if(Size<=Bravo.NumEmpty)
{
cout<<endl<<PartyName<<" boarding plane BRAVO"<<endl;
Bravo.Parties+=1;
// B+=1;
Bravo.NumEmpty-=Size;
Bravo.Parties[B].pName = Temp.pName;
Bravo.Parties[B].NumPeople = Temp.NumPeople;
Bravo.Parties[B].WhichPlane = Temp.WhichPlane;
cout<<endl<<"Seats now available on BRAVO: "<<Bravo.NumEmpty<<endl;
B+=1;
if(Bravo.NumSeats==0)
cout<<"Plane BRAVO is full and ready to fly"<<endl;
}
break;
case UNKNOWN_PLANE:
cout << "Unknown plane name entered" << endl;
delete [] PartyName;
break;
default:
break;
}
break;
case CmdSHUTDOWN:
cout << "SHUTDOWN" << endl;
break;
// continue to fly planes until all passengers in lounge are taken care of
delete [] Alpha.Parties; // do the same for Bravo and the Lounge
delete [] Bravo.Parties;
delete [] Lounge.Parties;
exit (0);
case CmdUNKNOWN:
default:
cout << "UNKNOWN" << endl;
break;
}
}
}
Commands GetCommand ()
{
char * pCmd;
Commands Command;
cout << "Enter Command: \n";
pCmd = ReadString ();
if (_strcmpi(pCmd, "ALPHA") == 0)
Command = CmdALPHA;
else
if (_strcmpi(pCmd, "BRAVO") == 0)
Command = CmdBRAVO;
else
if (_strcmpi(pCmd, "LOUNGE") == 0)
Command = CmdLOUNGE;
else
if(_strcmpi(pCmd, "ARRIVE")==0)
Command=CmdARRIVE;
else
if (_strcmpi(pCmd, "EXIT")==0)
Command=CmdSHUTDOWN;
else
if(_strcmpi(pCmd,"SHUTDOWN")==0)
Command=CmdSHUTDOWN;
else
if(_strcmpi(pCmd,"FLY")==0)
Command=CmdFLY;
else
Command = CmdUNKNOWN;
delete [] pCmd;
return Command;
}
Planes GetPlane ()
{
char * pPlane;
Planes WhichPlane;
pPlane = ReadString ();
if (strcmp (pPlane, "ALPHA") == 0)
WhichPlane = ALPHA;
else
if (strcmp (pPlane, "BRAVO") == 0)
WhichPlane = BRAVO;
else
WhichPlane = UNKNOWN_PLANE;
delete [] pPlane;
return WhichPlane;
}
void FlyPlane (Planes WhichPlane)
{
int i;
int x;
bool Full;
switch (WhichPlane)
{
case ALPHA:
do {
cout << "ALPHA is flying with parties: " << endl;
for(i=0;i<Alpha.NumParties;i++)
{
cout<<"Party "<<i<<" "<<Alpha.Parties[i].pName<<endl;
cout<<"Number of People: "<<Alpha.Parties[i].NumPeople<<endl;
}
Alpha.NumParties=0;
Alpha.NumEmpty=STD_Alpha;
Full = LoadPeopleFromLounge (ALPHA);
} while (!Full);
break;
case BRAVO:
do {
cout << "BRAVO is flying with parties: " << endl;
for(i=0;i<Bravo.NumParties;i++)
{
cout<<"Party "<<i<<" "<<Bravo.Parties[i].pName<<endl;
cout<<"Number of People: "<<Bravo.Parties[i].NumPeople<<endl;
}
Bravo.NumParties=0;
Bravo.NumEmpty=STD_Bravo;
Full = LoadPeopleFromLounge (BRAVO);
} while (!Full);
break;
case UNKNOWN_PLANE:
cout << "Unknown name of the plane" << endl;
}
}
bool LoadPeopleFromLounge (Planes WhichPlane)
{
int i;
switch (WhichPlane)
{
case ALPHA:
for(i=0;i<L;i++)
{
if(Lounge.Parties[i].NumPeople<=Alpha.NumEmpty)
{
Alpha.Parties[i].pName=Lounge.Parties[i].pName;
Alpha.Parties[i].NumPeople=Lounge.Parties[i].NumPeople;
Alpha.NumEmpty-=Lounge.Parties[i].NumPeople;
if(Lounge.Parties[i+1].NumPeople>Alpha.NumEmpty)
return true;
else
return false;
}
}
break;
case BRAVO:
for(i=0;i<L;i++)
{
if(Lounge.Parties[i].NumPeople<=Bravo.NumEmpty)
{
Bravo.Parties[i].pName=Lounge.Parties[i].pName;
Bravo.Parties[i].NumPeople=Lounge.Parties[i].NumPeople;
Bravo.NumEmpty-=Lounge.Parties[i].NumPeople;
if(Lounge.Parties[i+1].NumPeople>Bravo.NumEmpty)
return true;
else
return false;
}
}
break;
case UNKNOWN_PLANE:
default:
return false;
}
}
|
Thanks.. please let me know.. why it does that.. when i do fly.... it creates a infinite loop showing the same message
this is Party.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
party.h
[code]#ifndef PARTY_H
#define PARTY_H
enum Commands {CmdPARTY,CmdHELP,CmdUNKNOWN, CmdALPHA, CmdBRAVO, CmdLOUNGE, CmdFLY, CmdARRIVE, CmdSHUTDOWN};
enum Planes {UNKNOWN_PLANE, LOUNGE, ALPHA, BRAVO};
struct Party
{
char * pName;
int NumPeople;
Planes WhichPlane;
};
struct Plane
{
int NumSeats;
int NumEmpty;
int NumParties;
Party * Parties;
};
#endif
|
[/code]
Do you think you could post your main on here? I have this same assignment, and I'm having some trouble with my "FLY" command.
Thanks
You have a statement While (true), where is the statement that returns (false)?
Topic archived. No new replies allowed.