Any suggestions as to what would cause my program to run properly until it hops out of while loops to print out a closing statement??? I listed the read in file also?? Thanks a bunch!!
Bashemin.in reads:
aVAX785
aPDP11
aIBM360
aMTHCSC
dPDP11
p
aMAC512
dVAX785
aPI314
aCSB-1
dCSB-1
dIBM360
p
aPDP11
a257ADT
dMAC512
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <stack>
#include<iomanip>
using namespace std;
int main()
{
stack<string> parkinglot;
stack<string> alley;
stack<string> hold;
/*Stack parkinglot; //Parking lot
Stack alley; //Alley
Stack hold; //Place to store while running print*/
string plate; //License plate info
char c; //Used for Printing pause to screen
char firstchar; //String in the in file
string s; //Hlds each car data
int f = 0;// Counter to determine if lot is full
int i = 0;//Keeps track of moved cars
string filename;//Bashemin
cout << " Welcome to the Bashemin Parking Lot." << endl << endl;
cout << "\nEnter filename to process (bashemin.in used for sample data): ";
cin >> filename;
ifstream cinf(filename.c_str()); //get data
if (!cinf){ //error message
cout << "Cannot open file " << filename << endl;
exit(1);
}
while (getline(cinf,s,'\n')){
firstchar = s[0];
plate = s.substr(1);
//if car arrives and lot is full do:
if (firstchar== 'a' && f==5){
cout << "Lot is full you may not park here!!"<< endl;
}
//if car arrives and lot isn't full do:
if(firstchar == 'a' && f!= 5) {
parkinglot.push(plate);
cout<< "Car "<< plate <<" arrived and parked."<<endl;
f ++;
}
//if car is departing do:
if (firstchar == 'd'){
int i=0;
while (!parkinglot.empty() && parkinglot.top() != plate ){
alley.push(parkinglot.top());
parkinglot.pop();
i++;
//car not found in lot do
if(parkinglot.empty() && f==5){
cout << "Car " << plate << " not in lot to depart." <<endl;
}
}
//car found, decrease lot count
if (!parkinglot.empty() && parkinglot.top() == plate){
f--;
cout << "Car " << plate << " departing; ";
cout << i << " cars moved to get out." << endl;
parkinglot.pop();
i=0;
}
}
while (!alley.empty()){
parkinglot.push(alley.top());
alley.pop();
}
//Print cars in parking lot:
if (firstchar == 'p'){
cout <<"Cars parked in Bashemin Lot are:\n";
while (!parkinglot.empty()){
cout << parkinglot.top() << endl;
alley.push(parkinglot.top());
parkinglot.pop();
}
while(!alley.empty()){
parkinglot.push(alley.top());
alley.pop();
}
}
}
//tow cars left in lot after data flow is done
cout << "Bashemin lot is closing and the following cars will be towed." << endl;
while (!parkinglot.empty()){
cout << parkinglot.top() << endl;
alley.push(parkinglot.top());
parkinglot.pop();
}
return 0;
}