Hello everyone,

Like the headline says, my computer goes crazy when I run this program.

The problem did not occur until I added lines 80-83.

When the program is run, my computer starts beeping and weird characters show up on the console.

Any thoughts on the problem and why it causes my computer to go nuts?

#include <iostream>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <cstddef>
#include <fstream>
#include <cctype>
#include <ctime>
#include <string>
#include <iomanip>
#include <queue>
using namespace std;

struct masterFile                                         // master file record
{
  int employeeID;
  string employeeName;
  string departmentID;
  int YTDhoursRegular;
  int YTDhoursOvertime;
  double regularPayRate;
  double overtimeFactor;
  double ytdPay;
};

struct transactionFile                                    // transaction file record
{
  int employeeID;
  int regularHoursWorked;
  int overtimeHoursWorked;
};

struct workingStorage                                     // working storage 
{
  //copy master record
  int employeeID;
  string employeeName;
  string departmentID;
  int YTDhoursRegular;
  int YTDhoursOvertime;
  double regularPayRate;
  double overtimeFactor;
  double YTDPay;

  //new values for computations
  double overtimePayRate;
  int hoursRegular;
  int hoursOvertime;
  double weeksPay;
  double newYTDPay;
  int newYTDhoursRegular;
  int newYTDhoursOvertime;
};

void readMasterFile(masterFile &myStruct, ifstream &finMaster);
void readTransactionFile(transactionFile &transactionFile, ifstream &finTransaction);
void writeNewMasterFile();
void writeNewTransactionFile();
void saveStorage(masterFile &master, workingStorage &storage);
void saveStorage(transactionFile &transaction, workingStorage &storage);

int main()
{	
	// Open input files
	const string fileNames[2] = {"employee.txt", "TimeCard.txt"}; 
	ifstream finMaster(fileNames[0].c_str());
	if (!finMaster.is_open())
	{
		cout << endl << "Unable to open input file" << fileNames[0]	<< endl;
		return 1;
	} // End if (!finMaster.is_open())

	ifstream finTransaction(fileNames[1].c_str());
	if (!finTransaction.is_open())
	{
		cout << endl << "Unable to open input file" << fileNames[1]	<< endl;
		return 1;
	} // End if (!finMaster.is_open())

	cout << fileNames[0] << endl;
cout << fileNames[1] << endl;
cout << fileNames[2] << endl;
//system("pause");

	// Read an old master file record
	masterFile master;
	readMasterFile(master, finMaster);
	transactionFile transaction;
	readTransactionFile(transaction, finTransaction);
	workingStorage storage;		
	int activeKey = 0;

	while(true)
	{	
		// If both master key and transaction key = "infinity", exit the loop.
		if(master.employeeID == 2147483647 && transaction.employeeID == 2147483647)
		{
			break;  // uncomment this. I had to comment because I had not set up the loop.
		}
		// Save the smallest key value from the master and transaction key as the "activeKey".
		if(master.employeeID < transaction.employeeID)
		{
			activeKey = master.employeeID;
		}
		else
		{
			activeKey = transaction.employeeID;
		}
		//If master key > active key, signal an error.
		if(master.employeeID > activeKey)
		{
			cout << "Error: Master key is greater than activeKey" << endl;
			return 1;
		}
		//If master key matches active key move the old master values to working storage
		if(master.employeeID == activeKey)
		{

			saveStorage(master, storage);
		}
		//While transaction key matches active key 
		while(transaction.employeeID == activeKey)
		{
			saveStorage(transaction, storage);
			readTransactionFile(transaction, finTransaction);
		}
		//Write a record in the report file, reporting either the requested details or 
		//"NO TRANSACTION".
		string file_name = "Report.txt";
		ofstream outstreame;
		outstreame.open(file_name.c_str());

		outstreame << "DSFA" << endl;





		//Make new master record and write it to the new master file.


		//Read another old master record, checking for EOF as before.
		readMasterFile(master, finMaster);
	
		int n = 0;
		n++;
		cout << n << endl;
	}

}

//Post: Next record of master file is read into 
//masterFile struct
void readMasterFile(masterFile &myStruct, ifstream &finMaster)
{
	if(!finMaster.end)
	{
		myStruct.employeeID = 2147483647;
	}
	else
	{
		finMaster >> myStruct.employeeID;
		finMaster >> myStruct.employeeName;
		finMaster >> myStruct.departmentID; 
		finMaster >> myStruct.YTDhoursRegular;
		finMaster >> myStruct.YTDhoursOvertime;
		finMaster >> myStruct.regularPayRate;
		finMaster >> myStruct.overtimeFactor;
		finMaster >> myStruct.ytdPay;
	}
}


//Post: Next record of transaction file is read into 
//transactionFile struct
void readTransactionFile(transactionFile &transactionFile, ifstream &finTransaction)
{
	if(finTransaction.end)
	{
		transactionFile.employeeID = 2147483647;
	}
	else
	{	
		finTransaction >> transactionFile.employeeID;
		finTransaction >> transactionFile.regularHoursWorked;
		finTransaction >> transactionFile.overtimeHoursWorked;
	}
}

void saveStorage(masterFile &master, workingStorage &storage)
{
  storage.employeeID = master.employeeID;
  storage.employeeName = master.employeeName;
  storage.departmentID = master.departmentID;
  storage.YTDhoursRegular = master.YTDhoursRegular;
  storage.YTDhoursOvertime = master.YTDhoursOvertime;
  storage.regularPayRate = master.regularPayRate;
  storage.overtimeFactor = master.overtimeFactor;
  storage.YTDPay = master.ytdPay;
}

void saveStorage(transactionFile &transaction, workingStorage &storage)
{
  storage.hoursRegular = transaction.regularHoursWorked;
  storage.hoursOvertime = transaction.overtimeHoursWorked;
}

I've narrowed the problem down to line 82, which is trying to read an array with an index out of the array range. I know this is a problem, but I would expect to see a garbage number displayed on the console and not my computer losing it.

I've narrowed the problem down to line 82, which is trying to read an array with an index out of the array range. I know this is a problem, but I would expect to see a garbage number displayed on the console and not my computer losing it.

Your computer isn't "losing it". It's outputting exactly what you asked it to do, filename[2] -- which doesn't exist. So it's outputting the stuff that's where the variable should be anyway, which is probably other data in your program space which can be just about anything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.