I'm trying to create a parking lot system where users can enter and exit a lot. This lot should be kept on a file(s). My code compiles but my data doesn't write to the actual file.

void SYSTEM::parkProcess()
{
	CARD IDcard;
	char lot;
	int id;
	string type;
	cout<<"Enter Parking Lot: ";
	cin>>lot;
	cout<<"Enter ID #: ";
	cin>>id;
	cout<<"Enter type: ";
	cin>>type;

	IDcard.setIdNo(id);
	IDcard.setType(type);
	ofstream newOccupant;

	if(AuthorizeEntry(IDcard,lot))
	{
		cout<<"Lot Accessed." <<endl;
		
		if(lot == 'A')
			ofstream newOccupant("lot_A.txt", ios::out);
		if(lot == 'B')
			ofstream newOccupant("lot_B.txt",  ios::out);
		if(lot == 'C')
			ofstream newOccupant("lot_C.txt",  ios::out);
		if(lot == 'D')
			ofstream newOccupant("lot_D.txt",  ios::out);
		if(lot == 'E')
			ofstream newOccupant("lot_E.txt",  ios::out);
		
		if(!newOccupant)
			cout << "Unable to create active lot file";
		else
			addToLot(newOccupant);
	}
	else
	{
		cout<<"You are not allowed to park in this parking lot." <<endl;	
	}
}

void SYSTEM::addToLot(ofstream &newOccupant)
{
	int dd, mm, yy;

	cout << "Enter the Current Date" << endl << "Day";
	cin >> dd;
	cout << "Month";
	cin >> mm;
	cout << "Year";
	cin >> yy;
	
	newOccupant << dd << mm << yy;
}

Does nothing at all get written to any of the files?

As written, you will not add to files, you will overwrite any existing content. You need to open the files with ios::app (append mode) if you want to add to the files, as in keeping a log.

the ofstream newOccupant inside the local scope of the if statement is hiding the ofstream newOccupant that you had declared earlier.

// ...
  ofstream newOccupant;
  if(AuthorizeEntry(IDcard,lot))
  {
    cout<<"Lot Accessed." <<endl;
		
    if(lot == 'A')
       ofstream newOccupant("lot_A.txt", ios::out);
       // this newOccupant is a different variable inside
       // the local scope of the if statement. it hides 
       // the variable newOccupant declared on line 2 
     // etc
		
    // here the newOccupant referred to is the one 
    // declared on line 2 ; no file has been opened for this.
    // the newOccupant inside the local scope of the 
    // if statement has already been destroyed by now.
    if(!newOccupant) 
      // ...

instead, you need to do something like this

// ...
if( AuthorizeEntry(IDcard,lot) )
{
  cout<<"Lot Accessed." <<endl;
  string file_name = "lot_" ;
  file_name += lot + ".txt" ; 

  // append? see vmanes' post
  ofstream newOccupant( file_name.c_str(), /* ios::app ? */ ) ;

  if(!newOccupant)
    cout << "Unable to create active lot file";
  else
    addToLot(newOccupant);
}
else
// ...

there is a blunder in the last post (which unfortunately will not get caught by the compiler).

// ...
if( AuthorizeEntry(IDcard,lot) )
{
  cout<<"Lot Accessed." <<endl;
  string file_name = "lot_" ;
  // file_name += lot + ".txt" ; 
  file_name += lot ; file_name += ".txt" ;

  // append? see vmanes' post
  ofstream newOccupant( file_name.c_str(), /* ios::app ? */ ) ;

  if(!newOccupant)
    cout << "Unable to create active lot file";
  else
    addToLot(newOccupant);
}
else
// ...

the ofstream newOccupant inside the local scope of the if statement is hiding the
ofstream newOccupant that you had declared earlier.

True, but it doesn't really pose a problem here, as the only use of newOccupant occurs within that if block. It is rather poor style to declare the variable so many places.
Declare once, open where/when needed.

> ... but it doesn't really pose a problem here ...

this

ofstream newOccupant;

if(AuthorizeEntry(IDcard,lot))
{
  cout<<"Lot Accessed." <<endl;
  
  if(lot == 'A')
    ofstream newOccupant("lot_A.txt", ios::out);
  if(lot == 'B')
    ofstream newOccupant("lot_B.txt",  ios::out);
  if(lot == 'C')
    ofstream newOccupant("lot_C.txt",  ios::out);
  if(lot == 'D')
    ofstream newOccupant("lot_D.txt",  ios::out);
  if(lot == 'E')
    ofstream newOccupant("lot_E.txt",  ios::out);
  
  if(!newOccupant)
  // ...

is equivalent to this

ofstream newOccupant;

if(AuthorizeEntry(IDcard,lot))
{
  cout<<"Lot Accessed." <<endl;
  
  if(lot == 'A')
  {
    ofstream newOccupant("lot_A.txt", ios::out);
  }
  if(lot == 'B')
  {
    ofstream newOccupant("lot_B.txt",  ios::out);
  }
  if(lot == 'C')
  {
    ofstream newOccupant("lot_C.txt",  ios::out);
  }
  if(lot == 'D')
  {
    ofstream newOccupant("lot_D.txt",  ios::out);
  }
  if(lot == 'E')
  {
    ofstream newOccupant("lot_E.txt",  ios::out);
  }
  
  if(!newOccupant)
  // ...

> ... as the only use of newOccupant occurs within that if block ..
the only thing that happens in the each of the if blocks are that an ofstream is constructed and immediately destroyed. these ofstreams are not visible and do not exist at the place where an ofstream is used. the ofstream that is used is the one in the outer scope, to which a file has never been associated.

6.4 - Selection statements [stmt.select]
.... If the substatement in a selection-statement is a single statement and not a ompound-statement, it is as if it was rewritten to be a compound-statement containing the original substatement. [Example:
if (x)
int i;
can be equivalently rewritten as
if (x) {
int i;
}
Thus after the if statement, i is no longer in scope. ]

ISO/IEC 14882(E)

Comments
Good catch on subtle aspect

You're quite right. Invisible curly braces! How subtle.

All the more reason for my emphasis in lessons that all variables should be declared at the beginning of a function, not willy nilly all through it.

This article has been dead for over six months. Start a new discussion instead.