*When I compile the program it works. However, when I try to run the program, it outputs the first message then gives me the following error-*

CSCI112LAB2.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

*And when I tried to debug it*

Unhandled exception at 0x7855b690 in CSCI112LAB2.exe: 0xC0000005: Access violation reading location 0x00000000.
There is no source code available for the current location.

*And this appears in the output box*

'CSCI112LAB2.exe': Loaded 'C:\Documents and Settings\Tina\My Documents\Visual Studio 2008\Projects\CSCI112LAB2\Release\CSCI112LAB2.exe', Symbols loaded.
'CSCI112LAB2.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'CSCI112LAB2.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'CSCI112LAB2.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_d08d0375\msvcp90.dll'
'CSCI112LAB2.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_x-ww_d08d0375\msvcr90.dll'
First-chance exception at 0x7855b690 in CSCI112LAB2.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x7855b690 in CSCI112LAB2.exe: 0xC0000005: Access violation reading location 0x00000000.
The program '[5520] CSCI112LAB2.exe: Native' has exited with code 0 (0x0).

*So…I have no idea how to fix this. I played around with it for a while and still can’t get it to work. I don’t understand what I can do to fix an error when it says that there is no source code at the location to fix.*


*New error message! – I just got this one this morning when I tried to run the debug. I have no idea what it means, except for the invalid null pointer part, but I don’t think I was using any pointers that I myself deliberately put there.*

Debug Assertion Failed!

Program: …Visual Studio 2008\Projects\CSCI112LAB2\Debug\CSCI112LAB2.exe
File: f:\dd\vctools\crt_bld\self_x86\crt\src\xstring
Line: 1094

Expression: invalid null pointer

For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

*What do I do to fix this?*

Attachments
//***********************************************************
//Employee cpp file
//Author: Amanda Persons		CSCI 112-001
//Date written: 09 Oct 08
//Class Employee stores employee's salary info
//Requires Employee.h
//************************************************************

#include <string>
using std::string;
#include "Employee.h"

using namespace std;

						//defualt Employee constructor
Employee::Employee(string lastNameValue, string firstNameValue, float salaryValue)
{
	setLastName(lastNameValue);
	setFirstName(firstNameValue);
	setSalary(salaryValue);
}						//end Employee constructor

						//get lastName value
string Employee::getLastName() const
{
	return lastName;
}						//end function getLastName
						//set last-name value
void Employee::setLastName(string lastNameValue)
{
	lastName = lastNameValue;
}						//end fuction setLastName

						//get firstName value
string Employee::getFirstName() const
{
	return firstName;
}						//end function getFirstName
						//set first-name value
void Employee::setFirstName(string firstNameValue)
{
	firstName = firstNameValue;
}						//end fuction setFirstName

						//get salary value
float Employee::getSalary() const
{
	return salary;
}						//end function getSalary
						//set salary value
void Employee::setSalary(float salaryValue)
{
	salary = salaryValue;
}						//end function setSalary
//***********************************************************
//Employee header file
//Author: Amanda Persons		CSCI 112-001
//Date written: 06 Oct 08
//Class Employee definition
//************************************************************
#include <string>
using std::string;

class Employee
{
public:
						//default Employee constructor
	Employee(string = "", string = "", float = 0.0);
						//acc/essor functions for lastnName
	void setLastName(string);
	string getLastName() const;
						//accessor functions for firstName
	void setFirstName(string);
	string getFirstName() const;
						//accessor functions for salary
	void setSalary(float);
	float getSalary() const;
private:
	string firstName;
	string lastName;
	float salary;
};						//end class Employee
//***********************************************************
//Lab 2
//Author: Amanda Persons	CSCI 112-001
//Date written: 06 Oct 08
//Displays employee name and salary form a file
//Requires: Employee.h file and employee.dat file
//************************************************************

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include "Employee.h"

using namespace std;
						//prototypes
void outputLine( ostream&, const Employee &);
void userSearch( string& first, string& last);
void SEARCHFILE(string first, string last, string& salary);

int main()
{
	cout<<"This program seaches Employee data to output a name and salary."<<endl;
	bool lnfound = false;//last not found boolean
	char response;		//holds user choice for continue search
	string first;		//holds first name user input
	string last;		//holds last name user input
	string salary = 0;	//holds salary
	cout<<"Beginning File check."<<endl;
	ifstream employeeData("employee.dat",ios::in | ios::binary);
						//exit
	if (!employeeData)
	{
		cout<<"Error! File could not be opened." <<endl;
		exit(1);
	}					//end if
	Employee data;		//create record
						//read first record from file
	employeeData.read(reinterpret_cast<char*>(&data), sizeof(Employee));
						//read all records from file
	do
	{
		cout<<"Would you like to search for a name? (y or n): ";
		cin>>response;
		cout<<endl;
		if (response == 'y')
		{
						//display record
			userSearch(first, last);
			SEARCHFILE(first, last, salary);
		}
	} while (response != 'n');
						//end do-while loop
	char exit_char;		// Temporary for input           
	cout << "\nPress any key and <enter> to exit \n";
	cin >> exit_char;	//Wait for key response before exiting.
	return 0;
}						//end of main program


//***********************************************************
//Author: Amanda Persons		Date written: 06 Oct 08
//Precondition:the user has entered a yes to search name
//Postcondition:saves user input into two stings: first and last
//***********************************************************
void userSearch(string& first, string& last)
{
  cout <<"To search for a name, enter the name in the following format: "<<endl;
  cout <<"Firstname Lastname" <<endl;
  cout <<"Please enter the name you wish to find: ";
  cin >>first >>last;
}						//end of function userSearch

//***********************************************************
//Author: Amanda Persons		Date written: 06 Oct 08
//Precondition:the selected name exists
//Postcondition:prints desired employee data
//***********************************************************
void outputLine(ostream &output, const Employee &record)
{
	output <<left <<setw(16)<<record.getLastName()
		<<setw(11)<<record.getFirstName()
		<<setw(10)<<setprecision(2)<<right<<fixed<<showpoint<<record.getSalary()
		<<endl;
}						//end of function outputLine

//***********************************************************
//Author: Amanda Persons		Date written: 07 April 2008
//Precondition:the user has entered a first name and a last name to search for
//Postcondition:saves the found value for number
//***********************************************************
void SEARCHFILE(string first, string last, string& salary)
{
  string info[50][3];	//help determine how long the file is
  int count = 0;		//initiate count
  string currentLocation;//holds name at current location
  ifstream inFile;
  inFile.open("input.txt");//opening the file
  if(!inFile)			//checks to see if file exists
    cout <<"File-read error!" <<endl;
  else
  {
    while(inFile)		//determines number of rows in file using an array
    {
      inFile >> currentLocation;
      info[count][0] = currentLocation;
      inFile >> currentLocation;
	  info[count][1] = currentLocation;
      inFile >> currentLocation;
	  info[count][2] = currentLocation;
	  count++;
    }					//ends once count is equal to rows in file
    bool found = false;
    for (int i=0; i<count-1; i++)
    {
      if(first == info[i][0])//checks if first name matches
	  {
	    if(last == info[i][1])//checks if second name matches
		{
	      salary = info[i][2];//sets salary = to salary listed
		  found = true;	//found becomes true since name was located
		  cout<<last<<", "<<first<<": Salary: "<<salary<<endl;
		}
	  }
    }
    if(found==false)	//if found does not become true
    {
      cout <<"This name is not in the file" <<endl;
	  salary = "0";
    }
  }
}

>*What do I do to fix this?*
Figure out where you're trying to dereference a null pointer in your code.

Thanks for taking the time to reply!

But I'm not using pointers in the code...so, it's kinda hard for me to find it. Maybe it's just 'cause I'm not too great at C++, but I'm pretty sure I didn't do that since I try to avoid pointers at all costs. :)

> (Press Retry to debug the application)
Do this.

When the debugger starts, do something like
view->stack

That will tell you how the code got to that point.

Somewhere through the various stack levels will be your code. Click on the first one you recognise. It should open your source code. Examine really carefully all the parameters which are being passed. Chances are, one of them is NULL, and it shouldn't be.

It happens the instant the program begins to run. The program outputs the first cout line and then the error occurs before the next line. The lines involved are:

cout<<"This program seaches Employee data to output a name and salary."<<endl;
bool lnfound = false;	//last not found boolean
char response;		//holds user choice for continue search
string first;		//holds first name user input
string last;		//holds last name user input
string salary = 0;	//holds salary
cout<<"Beginning File check."<<endl;

Thanks for taking the time to reply!

Perhaps I should have mentioned this in the original post, but when I press Retry is goes back to the first error listed and shuts down...

Wait a minute... I think I see what I did...wow, stupid error. :(

I wrote string salary = 0 but that's impossible. wow. I can't believe I didn't see that.

Sorry to bother you!

This question has already been answered. Start a new discussion instead.